xslt - 编译样式表时的未知函数 saxon:parse-html
问题描述
我正在使用 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 内部没有问题。
解决方案
您可能需要调用 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: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>
您可以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 代码。
推荐阅读
- java - MaterialButton 与 Button 的大小差异
- java - 在两台不同的机器上创建 RMI 应用程序时客户端和服务器我们应该在哪里定义我们的接口客户端或服务器端?
- featuretools - 您如何搜索特定功能?
- vagrant - vagrant ssh-config 非常慢
- r - R 字符到日期时间导致 NA
- angular - Angular 7注入服务未设置
- ios - IQKeyboardManagerSwift“共享”不可用:在适当的情况下使用基于视图控制器的解决方案
- r - 在 R 的逻辑回归中使用权重的正确方法是什么?
- shell - 将转录本 ID 和基因符号从 rna fasta 打印到新的文本文件
- php - 如果 in_array 未找到,则使用 else 语句