1

我正在使用 Saxon-EE 10.3 变压器对 Oxygen 进行 XSL 转换。我想稍后在我的网站上使用已编译的样式表 (sef.json) 和 Saxon-JS 2。在 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 内部没有问题。

4

1 回答 1

3

您可能需要调用 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])

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

除了 XSLT 代码之外,另一种不需要您设置脚本代码的方法是使用ixsl:evalSaxon-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

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

我设法将该代码编译为带有设置的 SEF 文件,xslt3 -xsl:test-override-saxon-parse.xsl -nogo -export:test-override-saxon-parse. -sef.json -ns:"##html5"并且 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 代码。

于 2021-06-08T15:00:07.167 回答