使用 XPathCompiler 引用用户定义的 xsl:function
referencing a user-defined xsl:function using XPathCompiler
我在 VB .NET 中使用 Saxon HE 的 XPathCompiler class 对 XML 文件进行 运行 XPath 查询,然后处理结果。我还有一个带有用户定义函数的 XSL。有没有一种方法可以在我的 XPath 查询中引用这些用户定义的函数?我从其他 XSL 中成功地引用了它们,但是当我只是从 Saxon XPathCompiler 对象自己执行 XPath 查询时不确定如何去做。
基本上,我希望在下面的代码中有一些方法可以指向我的 DocOpsFunctions.xsl 样式表并访问那里定义的函数。
代码:
Dim myProcessor As New Saxon.Api.Processor
Dim myCompiler As XPathCompiler = myProcessor.NewXPathCompiler()
myCompiler.XPathLanguageVersion = "3.1"
Dim myDocBuilder As DocumentBuilder = myProcessor.NewDocumentBuilder
Dim myDoc As XdmNode = myDocBuilder.Build(New Uri("input.xml"))
Dim myResults As XdmValue
myResults = myCompiler.Evaluate("dof:lefttrim('ISTeams/Team'", myDoc)
DocOpsFunctions.xsl:
<xsl:transform version="3.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:dof="http://docops.com">
<xsl:function name="dof:lefttrim" as="xs:string">
<xsl:param name="text"/>
<xsl:value-of select="replace($text, '^\s+', '')"/>
</xsl:function>
</xsl:transform>
Input.xml:
<ISTeams>
<Team>Team1 </Team1>
<Team>Team2 </Team2>
</ISTeams>
我知道使用 ExtensionFunction 接口定义扩展函数的能力,但我希望找到一种更动态地执行此操作的方法,以便可以在样式表而不是代码中实现新函数。
确保将函数声明为 public 和 visiblity="public"
,然后您可以在 XPath 3.1 中使用 fn:transform
(https://www.w3.org/TR/xpath-functions/#func-transform):
transform(map {
'stylesheet-location' : 'DocOpsFunctions.xsl',
'cache' : true(),
'delivery-format' : 'raw',
'initial-function' : QName('http://docops.com',
'lefttrim'),
'function-params' : [' foo']
})?output
在 Java API 中,您应该能够使用 http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XPathCompiler.html#addXsltFunctionLibrary-net.sf.saxon.s9api.XsltPackage- 将 XSLT package/stylesheet 中的函数公开给 XPath。
在 .NET 10.2 API 中,您应该可以使用 http://www.saxonica.com/html/documentation/dotnetdoc/Saxon/Api/XPathCompiler.html#AddXsltFunctionLibrary(XsltPackage) 在 .NET 中做同样的事情,即将用 XSLT 编写的函数库公开给 XPath。
我在 VB .NET 中使用 Saxon HE 的 XPathCompiler class 对 XML 文件进行 运行 XPath 查询,然后处理结果。我还有一个带有用户定义函数的 XSL。有没有一种方法可以在我的 XPath 查询中引用这些用户定义的函数?我从其他 XSL 中成功地引用了它们,但是当我只是从 Saxon XPathCompiler 对象自己执行 XPath 查询时不确定如何去做。
基本上,我希望在下面的代码中有一些方法可以指向我的 DocOpsFunctions.xsl 样式表并访问那里定义的函数。
代码:
Dim myProcessor As New Saxon.Api.Processor
Dim myCompiler As XPathCompiler = myProcessor.NewXPathCompiler()
myCompiler.XPathLanguageVersion = "3.1"
Dim myDocBuilder As DocumentBuilder = myProcessor.NewDocumentBuilder
Dim myDoc As XdmNode = myDocBuilder.Build(New Uri("input.xml"))
Dim myResults As XdmValue
myResults = myCompiler.Evaluate("dof:lefttrim('ISTeams/Team'", myDoc)
DocOpsFunctions.xsl:
<xsl:transform version="3.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:dof="http://docops.com">
<xsl:function name="dof:lefttrim" as="xs:string">
<xsl:param name="text"/>
<xsl:value-of select="replace($text, '^\s+', '')"/>
</xsl:function>
</xsl:transform>
Input.xml:
<ISTeams>
<Team>Team1 </Team1>
<Team>Team2 </Team2>
</ISTeams>
我知道使用 ExtensionFunction 接口定义扩展函数的能力,但我希望找到一种更动态地执行此操作的方法,以便可以在样式表而不是代码中实现新函数。
确保将函数声明为 public 和 visiblity="public"
,然后您可以在 XPath 3.1 中使用 fn:transform
(https://www.w3.org/TR/xpath-functions/#func-transform):
transform(map {
'stylesheet-location' : 'DocOpsFunctions.xsl',
'cache' : true(),
'delivery-format' : 'raw',
'initial-function' : QName('http://docops.com',
'lefttrim'),
'function-params' : [' foo']
})?output
在 Java API 中,您应该能够使用 http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XPathCompiler.html#addXsltFunctionLibrary-net.sf.saxon.s9api.XsltPackage- 将 XSLT package/stylesheet 中的函数公开给 XPath。
在 .NET 10.2 API 中,您应该可以使用 http://www.saxonica.com/html/documentation/dotnetdoc/Saxon/Api/XPathCompiler.html#AddXsltFunctionLibrary(XsltPackage) 在 .NET 中做同样的事情,即将用 XSLT 编写的函数库公开给 XPath。