简体   繁体   English

编译样式表时未知 function saxon:parse-html

[英]Unknown function saxon:parse-html when compiling stylesheet

I am working on an XSL transformation on Oxygen using the Saxon-EE 10.3 transformer.我正在使用 Saxon-EE 10.3 变压器对 Oxygen 进行 XSL 转换。 I want to use the compiled stylesheet (sef.json) later on my website with Saxon-JS 2. Inside of the XSL transformation I am using the saxon:parse-html function with the saxon namespace declared as following:我想稍后在我的网站上使用已编译的样式表 (sef.json) 和 Saxon-JS 2。在 XSL 转换内部,我使用 saxon:parse-html function 和声明如下的 saxon 命名空间:

<xsl:stylesheet xmlns:prop="http://saxonica.com/ns/html-property"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:style="http://saxonica.com/ns/html-style-property" 
    xmlns:saxon="http://saxon.sf.net/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT"
    xmlns:js="http://saxonica.com/ns/globalJS" 
    exclude-result-prefixes="xs prop ixsl js style saxon xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="3.0"
    xpath-default-namespace="http://www.tei-c.org/ns/1.0" 
    xmlns="http://www.tei-c.org/ns/1.0">

and the function is called this way: function 是这样调用的:

          <xsl:call-template name="nameTemplate">
              <xsl:with-param name="html">
                  <xsl:copy-of select="saxon:parse-html(var)"></xsl:copy-of>
              </xsl:with-param>
          </xsl:call-template>

I tried to compile the stylesheet through this command:我尝试通过以下命令编译样式表:

xslt3 -xsl:test.xsl -export:test.sef.json -t

but I encounter the following error:但我遇到以下错误:

Failed to compile stylesheet: Static error in XPath on line 147 in Oxygen/Test.xsl {saxon:parse-html(?Text)}: Unknown function Q{http://saxon.sf.net/}parse-html()
Error Q{http://www.w3.org/2005/xqt-errors}XPST0017 at xpath.xsl#963
    Failed to compile stylesheet
Error Q{http://www.w3.org/2005/xqt-errors}XPST0017 at xpath.xsl#963
    Static error in XPath on line 147 in Oxygen/Test.xsl {saxon:parse-html(?Text)}: Unknown function Q{http://saxon.sf.net/}parse-html()

The transformation works without problem inside Oxygen though.但是,这种转换在 Oxygen 内部没有问题。

You might need to call into JavaScript eg set up a script element您可能需要调用 JavaScript 例如设置script元素

<script>
function parseHTML(html) { return new DOMParser().parseFromString(html, 'text/html'); }
</script>

in your HTML document and then inside of XSLT with Saxon JS 2 in the browser you should be able to use eg在您的 HTML 文档中,然后在浏览器中使用 Saxon JS 2 的 XSLT 内部,您应该能够使用例如

ixsl:window() => ixsl:get('parseHTML') => ixsl:apply([var])

instead of saxon:parse-html(var) , with a namespace declaration of xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT" in your XSLT.而不是saxon:parse-html(var) ,在 XSLT 中使用xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT"的命名空间声明。

Another way to not require you to set up the script code in addition to the XSLT code is to use ixsl:eval to run the JavaScript directly from XSLT in Saxon-JS 2;除了 XSLT 代码之外,另一种不需要您设置脚本代码的方法是使用ixsl:eval在 Sax 中直接从 XSLT 运行 JavaScript; I have set up an example at https://martin-honnen.github.io/saxon-js-parse-html-test/html/test-saxon-parse-html2.html which basically uses an implementation我已经在https://martin-honnen.github.io/saxon-js-parse-html-test/html/test-saxon-parse-html2.ZFC35FDC70D5269D256988中设置了一个示例

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:saxon="http://saxon.sf.net/"
  xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT"
  exclude-result-prefixes="#all"
  expand-text="yes">

  <xsl:function name="saxon:parse-html" as="document-node()" use-when="system-property('xsl:product-name') = 'Saxon-JS'">
    <xsl:param name="html" as="xs:string"/>
    <xsl:sequence select="ixsl:eval('new DOMParser()') => ixsl:call('parseFromString', [$html, 'text/html'])"/>
  </xsl:function>

</xsl:stylesheet>

of the XSLT 3 module https://github.com/martin-honnen/saxon-js-parse-html-test/blob/master/xslt/override-saxon-parse-html2.xsl . XSLT 3 模块https://github.com/martin-honnen/saxon-js-parse-html-test/blob/master/xslt/override-saxon-parse-html2.xsl的。

You can xsl:import that in your other XSLT code, as done in https://github.com/martin-honnen/saxon-js-parse-html-test/blob/master/xslt/test-override-saxon-parse.xsl with eg <xsl:import href="override-saxon-parse-html2.xsl"/> and call eg saxon:parse-html(.) .您可以xsl:import在您的其他 XSLT 代码中,如https 中所做的那样://github.com/martin-honnen/saxon-js-parse-html-test/blob/master/xslt/test-override-saxon-parse .xsl与例如<xsl:import href="override-saxon-parse-html2.xsl"/>并调用例如saxon:parse-html(.)

I managed to compile that code to an SEF file with the settings xslt3 -xsl:test-override-saxon-parse.xsl -nogo -export:test-override-saxon-parse. -sef.json -ns:"##html5"我设法使用设置xslt3 -xsl:test-override-saxon-parse.xsl -nogo -export:test-override-saxon-parse. -sef.json -ns:"##html5" xslt3 -xsl:test-override-saxon-parse.xsl -nogo -export:test-override-saxon-parse. -sef.json -ns:"##html5" and that way the HTML page https://martin-honnen.github.io/saxon-js-parse-html-test/html/test-saxon-parse-html2.html can simply run that XSLT with xslt3 -xsl:test-override-saxon-parse.xsl -nogo -export:test-override-saxon-parse. -sef.json -ns:"##html5" and that way the HTML page https://martin-honnen.github.io/saxon-js-parse-html-test/html/test-saxon-parse-html2. html可以简单地运行 XSLT 与

           SaxonJS.transform({
                stylesheetLocation: '../xslt/test-override-saxon-parse.sef.json',
                sourceLocation: '../xml/sample2.xml',
                destination: 'appendToBody'
            }, 'async')

As an alternative, you could import the pure XSLT 2 HTML parser that David Carlisle has somewhere on GitHub into your XSLT code.作为替代方案,您可以将 David Carlisle 在 GitHub 代码中某处的纯 XSLT 2 HTML 解析器导入您的 Z139E247C6BE049EBF23C 代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM