XSLT处理在Firefox和Chrome上提供了不同的结果

时间:2022-05-12 23:35:41

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.

我有一个由XSLT处理的XML,但Mozilla Firefox 24 ESR正确解析我的XML,而Google Chrome 42无法从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

我试图通过XSLT逻辑从xmlInput获取 Value 1 for first block 但是当我使用时

<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.

得到结果变量Chrome没有解析结果标记并在我的结果div中给出一个空标记。 Safari也是如此。

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

您可以在HTML文件中尝试整个代码,并查看不同的浏览器行为。

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

任何人都可以告诉我,我做错了什么?这与一些webkit行为有关吗?

1 个解决方案

#1


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.

出于安全考虑,Chrome无法运行本地加载的XSLT。您必须从服务器加载它。 Firefox并不是那么特别,因此在开发过程中最好使用它。

Related: XSLT not working in web browser

相关:XSLT无法在Web浏览器中运行


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...

Martin Honnen在下面的评论中正确地指出,因为OP是从嵌入在Javascript中的字符串加载XSLT,所以在这种情况下Chrome应该运行XSLT。挖掘更深,当然还有其他问题......

  • XML is not well formed: Closing tag for F1-RetBOMs has an internal space: F1- RetBOMs
  • XML格式不正确:关闭F1-RetBOMs的标签有一个内部空间:F1-RetBOMs

  • XSLT XPath has similar issue: F1- RetBOMs/selectBOM/results[2]/bom should be F1-RetBOMs/selectBOM/results[2]/bom
  • XSLT XPath有类似的问题:F1- RetBOMs / selectBOM / results [2] / bom应该是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
  • XSLT XPath还依赖于未定义的上下文:F1-RetBOMs / selectBOM / results [2] / bom应为/ 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.)

应用上述修补程序后,您应该会在Chrome和Firefox中看到相同的结果。 (坦率地说,我怀疑你以前会在Firefox中看到正确的结果。)

#1


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.

出于安全考虑,Chrome无法运行本地加载的XSLT。您必须从服务器加载它。 Firefox并不是那么特别,因此在开发过程中最好使用它。

Related: XSLT not working in web browser

相关:XSLT无法在Web浏览器中运行


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...

Martin Honnen在下面的评论中正确地指出,因为OP是从嵌入在Javascript中的字符串加载XSLT,所以在这种情况下Chrome应该运行XSLT。挖掘更深,当然还有其他问题......

  • XML is not well formed: Closing tag for F1-RetBOMs has an internal space: F1- RetBOMs
  • XML格式不正确:关闭F1-RetBOMs的标签有一个内部空间:F1-RetBOMs

  • XSLT XPath has similar issue: F1- RetBOMs/selectBOM/results[2]/bom should be F1-RetBOMs/selectBOM/results[2]/bom
  • XSLT XPath有类似的问题:F1- RetBOMs / selectBOM / results [2] / bom应该是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
  • XSLT XPath还依赖于未定义的上下文:F1-RetBOMs / selectBOM / results [2] / bom应为/ 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.)

应用上述修补程序后,您应该会在Chrome和Firefox中看到相同的结果。 (坦率地说,我怀疑你以前会在Firefox中看到正确的结果。)