有没有办法将文档节点作为参数传递给Edge和Chrome中的XSLTProcessor?

发布于 2025-02-06 13:39:50 字数 3058 浏览 1 评论 0原文

我正在尝试在边缘进行XSL转换,并使用文档节点作为XSLT参数(通过XSLTProcessor.setParameter)。 transformtoffment在边缘(和铬)中返回零,但在Firefox中运行不错。

有没有办法使下面的代码工作(至少在边缘)或不支持文档节点作为XSLT参数? 以下是提出问题的最小代码。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>

        <script type="text/javascript">

            function example() {
                let xsldocp = new DOMParser();
                const xsldoc = xsldocp.parseFromString(`<?xml version='1.0'?>
                <xsl:stylesheet version='1.0'
                    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
                    xmlns="http://www.w3.org/1999/xhtml">
                    <xsl:output method='xml' omit-xml-declaration='yes'/>

                    <!-- Parameters -->
                    <xsl:param name='ntParam'/>
                    <xsl:param name='strSpaces' select='"&#32;&#32;&#32;&#32;"'/> <!-- Default to 4 spaces -->

                    <!-- Global variables -->
                    <xsl:variable name='numTransformLevel' select='$ntParam/PARAMETER/@VALUE'/> <!-- does not work in Edge & Chrome -->
                    <!--xsl:variable name='numTransformLevel' select='2'/--><!-- works OK -->

                    <!-- Handles a generic element node. -->
                    <xsl:template match='*'>
                        <div>
                            <b><xsl:value-of select='$numTransformLevel'/></b>
                            <i><xsl:value-of select='//GROOT/@VALUE'/></i>
                        </div>
                    </xsl:template>

                </xsl:stylesheet>
                `,"application/xml");



                let xsltProcessor = new XSLTProcessor();

                // use the xsl sheet
                xsltProcessor.importStylesheet(xsldoc);

                // create xml
                let parser = new DOMParser();
                const docData = parser.parseFromString("<ROOT><GROOT VALUE='IAM'/></ROOT>", "application/xml");
                

                // create param xml and set the parameter
                parser = new DOMParser();
                const docParam = parser.parseFromString("<PARAM><PARAMETER VALUE='5'/></PARAM>", "application/xml");
                xsltProcessor.setParameter(null, "ntParam", docParam.documentElement);

                // do the transformation
                var targetElm = document.getElementById("trCont5");
                let resultFrag = xsltProcessor.transformToFragment(docData, targetElm.ownerDocument);

                targetElm.appendChild( resultFrag );
            }

        </script>
    </head>
    <body>
        <input type="button" onclick="example();" value="XLSTParam Test"/><!-- Outputs 5IAM -->
        <div id="trCont5"></div>
    </body>
</html>

I am trying to do XSL transformation in Edge and using a document node as xslt parameter(via XSLTProcessor.setParameter).
The transformToFragment returns null in Edge (and Chrome), but works ok in Firefox.

Is there a way to make the code below working (at least in Edge) or is there no support of document-node as a xslt parameter?
Below is a minimal code to present the issue.

https://jsfiddle.net/BennyHilarious/nxzLf9g3/1/

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>

        <script type="text/javascript">

            function example() {
                let xsldocp = new DOMParser();
                const xsldoc = xsldocp.parseFromString(`<?xml version='1.0'?>
                <xsl:stylesheet version='1.0'
                    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
                    xmlns="http://www.w3.org/1999/xhtml">
                    <xsl:output method='xml' omit-xml-declaration='yes'/>

                    <!-- Parameters -->
                    <xsl:param name='ntParam'/>
                    <xsl:param name='strSpaces' select='"    "'/> <!-- Default to 4 spaces -->

                    <!-- Global variables -->
                    <xsl:variable name='numTransformLevel' select='$ntParam/PARAMETER/@VALUE'/> <!-- does not work in Edge & Chrome -->
                    <!--xsl:variable name='numTransformLevel' select='2'/--><!-- works OK -->

                    <!-- Handles a generic element node. -->
                    <xsl:template match='*'>
                        <div>
                            <b><xsl:value-of select='$numTransformLevel'/></b>
                            <i><xsl:value-of select='//GROOT/@VALUE'/></i>
                        </div>
                    </xsl:template>

                </xsl:stylesheet>
                `,"application/xml");



                let xsltProcessor = new XSLTProcessor();

                // use the xsl sheet
                xsltProcessor.importStylesheet(xsldoc);

                // create xml
                let parser = new DOMParser();
                const docData = parser.parseFromString("<ROOT><GROOT VALUE='IAM'/></ROOT>", "application/xml");
                

                // create param xml and set the parameter
                parser = new DOMParser();
                const docParam = parser.parseFromString("<PARAM><PARAMETER VALUE='5'/></PARAM>", "application/xml");
                xsltProcessor.setParameter(null, "ntParam", docParam.documentElement);

                // do the transformation
                var targetElm = document.getElementById("trCont5");
                let resultFrag = xsltProcessor.transformToFragment(docData, targetElm.ownerDocument);

                targetElm.appendChild( resultFrag );
            }

        </script>
    </head>
    <body>
        <input type="button" onclick="example();" value="XLSTParam Test"/><!-- Outputs 5IAM -->
        <div id="trCont5"></div>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与清晨阳光 2025-02-13 13:39:50

使用saxonjs( https://www.saxonica.com/saxonica.com/saxonica.com/saxon-js /documentation2/index.html ,在 https://www.saxonica.com /download/javascript.xml ):

            function example() {

                const xslt = `<?xml version='1.0'?>
                <xsl:stylesheet version='1.0'
                    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
                    xmlns="http://www.w3.org/1999/xhtml">
                    <xsl:output method='html'/>

                    <!-- Parameters -->
                    <xsl:param name='ntParam'/>
                    <xsl:param name='strSpaces' select='"    "'/> <!-- Default to 4 spaces -->

                    <!-- Global variables -->
                    <xsl:variable name='numTransformLevel' select='$ntParam/PARAMETER/@VALUE'/> <!-- does not work in Edge & Chrome -->
                    <!--xsl:variable name='numTransformLevel' select='2'/--><!-- works OK -->

                    <!-- Handles a generic element node. -->
                    <xsl:template match='*'>
                        <div>
                            <b><xsl:value-of select='$numTransformLevel'/></b>
                            <i><xsl:value-of select='//GROOT/@VALUE'/></i>
                        </div>
                    </xsl:template>

                </xsl:stylesheet>
                `;            

                // create xml
                let parser = new DOMParser();
                const docData = parser.parseFromString("<ROOT><GROOT VALUE='IAM'/></ROOT>", "application/xml");
                

                // create param xml and set the parameter
                const docParam = parser.parseFromString("<PARAM><PARAMETER VALUE='5'/></PARAM>", "application/xml");
                
                // do the transformation
                var targetElm = document.getElementById("trCont5");
                
                let result = SaxonJS.XPath.evaluate(`
                transform(map { 
                  'source-node' : .,
                  'stylesheet-text' : $xslt,
                  'stylesheet-params' : map {
                     QName('', 'ntParam') : $ntParam
                  }
                })?output`,
                docData,
                {
                  'params' : { 
                  ntParam : docParam.documentElement,
                  xslt : xslt 
                  }
                });

                targetElm.appendChild( result );
            }
        </script>
    </head>
    <body>
        <input type="button" onclick="example();" value="XLSTParam Test"/><!-- Outputs 5IAM -->
        <div id="trCont5"></div>
    </body>
<script src="https://martin-honnen.github.io/xslt3fiddle/js/SaxonJS2.js"></script>
 
 
 <input type="button" onclick="example();" value="XSLTParam Test"/><!-- Outputs 5IAM -->
        <div id="trCont5"></div>

请注意,对于示例的紧凑性和独立性,我使用了saxonjs.xpath.evaluate方法来调用XPATH 3.1 转换即时运行提供的XSLT。 saxonjs还允许您使用saxonjs.s.transform方法运行预编译XSLT,因为您需要使用saxonee或node.js xslt3 tool(来自saxonica/npm https://www.npmjs.coms.com/package/package/xslt3 ) JSON文件。

Using SaxonJS (docs at https://www.saxonica.com/saxon-js/documentation2/index.html, download at https://www.saxonica.com/download/javascript.xml):

            function example() {

                const xslt = `<?xml version='1.0'?>
                <xsl:stylesheet version='1.0'
                    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
                    xmlns="http://www.w3.org/1999/xhtml">
                    <xsl:output method='html'/>

                    <!-- Parameters -->
                    <xsl:param name='ntParam'/>
                    <xsl:param name='strSpaces' select='"    "'/> <!-- Default to 4 spaces -->

                    <!-- Global variables -->
                    <xsl:variable name='numTransformLevel' select='$ntParam/PARAMETER/@VALUE'/> <!-- does not work in Edge & Chrome -->
                    <!--xsl:variable name='numTransformLevel' select='2'/--><!-- works OK -->

                    <!-- Handles a generic element node. -->
                    <xsl:template match='*'>
                        <div>
                            <b><xsl:value-of select='$numTransformLevel'/></b>
                            <i><xsl:value-of select='//GROOT/@VALUE'/></i>
                        </div>
                    </xsl:template>

                </xsl:stylesheet>
                `;            

                // create xml
                let parser = new DOMParser();
                const docData = parser.parseFromString("<ROOT><GROOT VALUE='IAM'/></ROOT>", "application/xml");
                

                // create param xml and set the parameter
                const docParam = parser.parseFromString("<PARAM><PARAMETER VALUE='5'/></PARAM>", "application/xml");
                
                // do the transformation
                var targetElm = document.getElementById("trCont5");
                
                let result = SaxonJS.XPath.evaluate(`
                transform(map { 
                  'source-node' : .,
                  'stylesheet-text' : $xslt,
                  'stylesheet-params' : map {
                     QName('', 'ntParam') : $ntParam
                  }
                })?output`,
                docData,
                {
                  'params' : { 
                  ntParam : docParam.documentElement,
                  xslt : xslt 
                  }
                });

                targetElm.appendChild( result );
            }
        </script>
    </head>
    <body>
        <input type="button" onclick="example();" value="XLSTParam Test"/><!-- Outputs 5IAM -->
        <div id="trCont5"></div>
    </body>
<script src="https://martin-honnen.github.io/xslt3fiddle/js/SaxonJS2.js"></script>
 
 
 <input type="button" onclick="example();" value="XSLTParam Test"/><!-- Outputs 5IAM -->
        <div id="trCont5"></div>

Note that for compactness and self-containedness of the example I used the SaxonJS.XPath.evaluatemethod to call the XPath 3.1 transform function on the fly to run the provided XSLT. SaxonJS also allows you to run precompiled XSLT with the SaxonJS.transform method, for that you need to use either SaxonEE or the Node.js xslt3 tool (from Saxonica/NPM https://www.npmjs.com/package/xslt3) to compile the XSLT file to a JSON file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文