0

I have an XSL object. I want to extract a select group of nodes. Another person or team wrote the XSL document and the line of code which selects those nodes, but it only works in IE and I'm trying to make it cross-browser compatible.

XSL file

<!-- snippet of XSL file -->
<xsl:variable name="title">aaa</xsl:variable>

<xsl:variable name="col1">CSV00001</xsl:variable>
<xsl:variable name="col2">CSV00002</xsl:variable>
<xsl:variable name="col3">CSV00003</xsl:variable>
<xsl:variable name="col4">CSV00004</xsl:variable>
<xsl:variable name="col5">CSV00005</xsl:variable>
<xsl:variable name="col6">CSV00006</xsl:variable>
<xsl:variable name="col7">CSV00007</xsl:variable>
<xsl:variable name="col8">CSV00008</xsl:variable>
<xsl:variable name="col9">CSV00009</xsl:variable>

JS file

// Extract title and column nodes
var varNodes = $(csvXsl).find("xsl\\:variable");

I tried referencing this article for ideas, Handling a colon in an element ID in a CSS selector, but that is for CSS. But I did attempt to replace the \: with the \\3A, \\3a, 3a, 3A and none of those worked.

Any suggestions?

As requested, here is a litle more info on how the XSL object gets created.

function loadXml(xmlFilePath) {
    var retObj = null ;
    var timeUniq = (new Date()).getTime();

    // check protocol
    var protocol = document.location.protocol;
    if(protocol.toLowerCase().indexOf("http")>=0){
        $.ajax({
            url: xmlFilePath + "?t=" + timeUniq,
            type: 'get',
            dataType: 'xml',
            async: false,
            timeout: 1000
        })
           .done(
               function(xml, status){
                   if( status != 'success' ){
                       return;
                   }
                   retObj = xml;
               })
           .fail(
               function(xhr, textStatus){
                   return;
               });
    }

    return retObj;
}

csvXsl = loadXml("./xsl/OrderListCSV.xsl");

Thank you.

Update

As requested, I have added a fiddle.

http://jsfiddle.net/Q3jNN/

Update2

Working JSFiddle routine with all the credit to commentator Niet.

http://jsfiddle.net/dNDN6/2/

In case the link is broken, here is the relevant portion:

// Workaround for JQuery bug, primarily using Vanilla JS.

function serializeXmlNode(xmlNode) {
    if (typeof window.XMLSerializer != "undefined") {
        return (new window.XMLSerializer()).serializeToString(xmlNode);
    } else if (typeof xmlNode.xml != "undefined") {
        return xmlNode.xml;
    }
    return "";
}

var tmpStr = serializeXmlNode(csvXsl); // Converts XML/XSL object to String
var tmp = document.createElement('div');

tmp.innerHTML = csvXsl; // Does not work
tmp.innerHTML = tmpStr; // This one works, had to serialize object first

var varNodes = tmp.getElementsByTagName('xsl\:variable'); // Works
var varNodes2 = tmp.getElementsByTagName('xsl:variable'); // Also works
Community
  • 1
  • 1
user717236
  • 4,959
  • 19
  • 66
  • 102

3 Answers3

1

I would do a number of steps to debug this issue:

  1. Check the content of the csvXls variable, you can try for example to look for: console.log(csvXsl) then $(csvXsl).find("xsl\\:variable"); or $("xsl\\:variable",csvXsl);

  2. If that variable contains the correct nodes, for example <xsl:variable name="col9">CSV00009</xsl:variable> then it could be a bug in jQuery, which I feel to exclude in such a basic selector.

  3. Post how do you get the csvXsl with a bit more context, so we can help you better.

Mimo
  • 6,015
  • 6
  • 36
  • 46
  • 3
    Shouldn't you explain why this would be any different? – jfriend00 Jan 16 '14 at 23:05
  • I updated my response with more details about how I would debug this issue. – Mimo Jan 17 '14 at 00:03
  • Thank you. Yeah, I can view the contents of the csvXsl object in the console and see that the object is correct and appears exactly the same as it does in IE. I tried both forms of the find method and it returns `[]`. However, if I use `$(csvXsl).find("variable")`, it finds the nodes. So, it might be a bug in jQuery. Again, I have not seen this problem occur in IE. – user717236 Jan 17 '14 at 03:31
  • Correction: It seems to work in Firefox, at least most of the time. It's definitely not working in Chrome, however. – user717236 Jan 17 '14 at 03:41
  • can you perhaps create a fiddle? – Mimo Jan 17 '14 at 03:52
  • No problem. I just did. I'm leaning towards a jQuery bug, at this point. – user717236 Jan 17 '14 at 05:05
1

This is indeed a bug in jQuery.

Try using Vanilla JS instead:

var tmp = document.createElement('div');
tmp.innerHTML = csvXsl;
var varNodes = tmp.getElementsByTagName('xsl:variable');
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Thank you very much. I LOL'd from that Vanilla JS link. Please take a look at the JSFiddle routine here --> http://jsfiddle.net/dNDN6/2/. I had to serialize the XSL object and then set it to tmp.innerHTML, though, to get it to work. Thanks for your help! – user717236 Jan 17 '14 at 20:19
0

must comment by 'Answer' since not enough reps

the line:

 var inputTag = $("#test\\:1").val()); 

in the fiddle supposedly showing what works : http://jsfiddle.net/dNDN6/2/

actually does not work in Chrome / webkit

the correct method is to use plain JS and getElementsByTagName first on the tag without the namespace (after the colon) and if no element then the full tag like so:

var x = xmlDoc.getElementsByTagName("variable");
if (!x[0]) x = xmlDoc.getElementsByTagName('xsl:variable');

x will contain your array [cross-browser]

mrauto
  • 91
  • 1
  • 7