编译样式表时出现未知函数 saxon:parse-html

Unknown function saxon:parse-html when compiling stylesheet

我正在使用 Saxon-EE 10.3 转换器对 Oxygen 进行 XSL 转换。我想稍后在我的网站上使用 Saxon-JS 2 编译的样式表 (sef.json)。 在 XSL 转换内部,我使用 saxon:parse-html 函数和声明如下的 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">

函数是这样调用的:

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

我尝试通过此命令编译样式表:

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

但是我遇到了以下错误:

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()

尽管在 Oxygen 内部转换没有问题。

您可能需要致电 JavaScript,例如设置一个 script 元素

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

在您的 HTML 文档中,然后在浏览器中使用 Saxon JS 2 的 XSLT 内部,您应该可以使用例如

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

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

除了 XSLT 代码之外不需要您设置脚本代码的另一种方法是直接从 Saxon-JS 中的 XSLT 使用 ixsl:eval 到 运行 JavaScript 2;我在 https://martin-honnen.github.io/saxon-js-parse-html-test/html/test-saxon-parse-html2.html 中设置了一个示例,它基本上使用了一个实现

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

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

您可以 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(.).

我设法将该代码编译为具有设置 xslt3 -xsl:test-override-saxon-parse.xsl -nogo -export:test-override-saxon-parse. -sef.json -ns:"##html5" 的 SEF 文件,这样 HTML 页面 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')

作为替代方案,您可以将 David Carlisle 在 GitHub 某处的纯 XSLT 2 HTML 解析器导入您的 XSLT 代码。