将 string-join 的结果类型转换为 NodeList
我需要处理评估 XPath 表达式并期望结果解析为 NodeList 的遗留代码。该代码使用 NodeList 来标识 XPath 表达式选择了多少个节点。仅当选择一个节点时,该代码才会继续。
现在我需要使用返回类型为 String 的字符串连接函数。有没有办法使用另一个 XPath 函数将 String 转换为 NodeList?理想情况下,它将把字符串转换为一个 NodeList,其中一个元素的文本内容就是字符串值。我尝试使用 exsl:node-set() 但没有成功(Saxon-HE 处理器)。
I need to deal with legacy code which evaluates XPath expressions expects the results to resolve to NodeList. The code uses NodeList to identify how many nodes are selected by the XPath expression. The code proceeds only if one node is selected.
Now I need to use string-join function which return type is String. Is there any way to convert String to NodeList using another XPath function? Ideally, it will convert the String to a NodeList with one element which text content would be the String value. I tried with exsl:node-set() but with no success (Saxon-HE processor).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,XPath 只是 XML 文档的一种查询语言——它不创建节点。
如果遗留代码期望 XPath 表达式计算为 NodeList 并且计算结果 g 您的 XPath 表达式只是一个字符串,那么要重用该遗留代码而不编写新代码,您必须更改您的 XPath 表达式,以便它选择一个或更多节点。
或者,编写您自己的代码,使用 XPath 表达式的计算结果。
No, XPath is just a query language for XML documents -- it doesn't create nodes.
If the legacy code expects the XPath expression to evaluate to a NodeList and the result of evaluationg your XPath expression is just a string, then to reuse that legacy code without writing new code, you must change your XPath expression, so that it selects one or more nodes.
Or, write your own code that uses the result of the evaluation of the XPath expression.
XPath(甚至 2.0)无法创建新节点,为此您需要 XQuery 或 XSLT。 Saxon 两者都支持,因此您可以选择。
XPath (even 2.0) can't create new nodes, for that you need XQuery or XSLT. Saxon supports both so you have the choice.
我建议在 Saxon XPath API 周围放置一个 Java 层,并在这个 Java 层中进行字符串到 NodeList 的转换。您的 Java 层可以实现 JAXP XPath 接口,从而“假装”为真正的 XPath 引擎。
I'd suggest putting a Java layer around the Saxon XPath API, and doing the string-to-NodeList conversion in this Java layer. Your Java layer can implement the JAXP XPath interface and thus "pretend" to be the real XPath engine.