2

I have an XML processed by an XSLT, but Mozilla Firefox 24 ESR parses my XML correctly while Google Chrome 42 is failing to get the node from XML.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>

var xslInput = "<xsl:stylesheet  xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\"> <xsl:output   method=\"xml\"/> <xsl:variable name=\"outcome\" select=\"F1- RetBOMs/selectBOM/results[2]/bom\"/><xsl:variable name=\"SINGLE_QUOTE\"  select=\"&quot;'&quot;\"/> <xsl:template match=\"/*\"> <result> <xsl:copy-of  select=\"$outcome\"/> </result></xsl:template> </xsl:stylesheet>" ;

var xmlInput = "<root><F1-RetBOMs><selectBOM><results><bom>Value 1 for first  block</bom><description>Description 1 for first block</description></results> <results><bom>Value 2 for second block</bom><description>Description 2 for  second block</description></results><rowCount>2</rowCount></selectBOM></F1- RetBOMs></root>";

function createXMLDoc(xmlText){
var parser = new DOMParser();
    if (xmlText) {
        xmlDoc = parser.parseFromString(xmlText, 'text/xml');
    } else {
        xmlDoc = document.implementation.createDocument('', '', null);
    }

    return xmlDoc;
}

function convertXSL(){
    var xmldoc = createXMLDoc(xmlInput);
    var xsldoc = createXMLDoc(xslInput);
    var oProcessor = new XSLTProcessor();
    oProcessor.importStylesheet(xsldoc);
    try{
        var outputDoc =   oProcessor.transformToDocument(xmldoc.documentElement, document);
    }catch(e){
        //console.log(e);
    }


    document.getElementById('result').innerHTML = new XMLSerializer().serializeToString(outputDoc);
}

</script>
</head>
<title></title>
<body>
<div id="result" style="min-height:300px; min-width: 400px; border: 1px solid blue"></div>
<span id="clickme" style="min-height:30px; min-width: 40px; border: 1px  solid red" onclick="convertXSL()">Click Me</span>
</body>
</html>

I am trying to get <bom>Value 1 for first block</bom> from xmlInput through XSLT logic but when I am using

<xsl:copy-of select=\"$outcome\"/>

to get the outcome variable Chrome is not parsing through results tag and giving an empty tag in my result div. Same thing happens with Safari.

You can try the whole code in an HTML file and see different browser behavior.

Can anyone please tell me, what am I doing wrong? Is this related with some webkit behavior?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • What is it that you want to achieve? The whole code is rather odd, for instance `transformToDocument` takes a single parameter and not two so I wonder what you want to achieve with `oProcessor.transformToDocument(xmldoc.documentElement, document)`. – Martin Honnen May 07 '15 at 16:24
  • I can tell you what happens when [the fixes I identify](http://stackoverflow.com/a/30105896/290085) are made and the file is loaded into a browser: An empty blue box and a "Click Me" red box appear. Clicking on "Click Me" adds "Value 2 for second block" into the blue box. There are rough edges, but it seems like typical basic exploration/prototyping. – kjhughes May 07 '15 at 16:32
  • The spaces were not a problem, they were introduced by pasting the code here.The root cause was 'root' as the xpath was relying on undefined context. Adding root to the xpath fixes the issue. Thanks a lot for the comments. – Manas Andhare May 08 '15 at 07:22

1 Answers1

0

Chrome won't run locally loaded XSLT due to security concerns. You'll have to load it from a server instead. Firefox is not so particular and ends up being better to use during development for this reason.

Related: XSLT not working in web browser


Update:

Martin Honnen correctly notes in the comments below that because OP is loading XSLT from a string embedded in the embedded Javascript, Chrome should be running the XSLT in this case. Digging deeper, sure enough, there are other problems...

  • XML is not well formed: Closing tag for F1-RetBOMs has an internal space: F1- RetBOMs
  • XSLT XPath has similar issue: F1- RetBOMs/selectBOM/results[2]/bom should be F1-RetBOMs/selectBOM/results[2]/bom
  • XSLT XPath also relies on undefined context: F1-RetBOMs/selectBOM/results[2]/bom should be /root/F1-RetBOMs/selectBOM/results[2]/bom

When you apply the above fixes, you should see the same results in Chrome and Firefox. (Frankly, I doubt that you would have been seeing correct results even in Firefox previously, though.)

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • The sample in the question loads the XSLT and XML source code from a string embedded in the Javascript code embedded in a HTML document so I don't think the security issue related to loading XML documents from the file system with Chrome can be the reason for the differences. – Martin Honnen May 07 '15 at 15:53
  • I have tried removing the spaces, I converted these files from Fiddler text wizard. the actual XML and XSL are different which I can't post here for business reasons. The problem is with the XPATH, if I replace '$outcome' with the complete XPATH of the 'outcome' variable, it works and gives the same result on chrome as FF. Both the xsl and xml are coming from server in my application and this is just an attempt to recreate the behavior. – Manas Andhare May 08 '15 at 07:08