微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决编译样式表时未知函数 saxon:parse-html

我正在使用 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 代码之外,另一种不需要您设置脚本代码的方法是使用 ixsl:eval 在 Saxon-JS 2 中直接从 XSLT 运行 JavaScript;我在 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

您可以在其他 XSLT 代码中xsl:import,就像在 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 代码中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。