XPath查询:如何引用“当前节点” (爪哇/撒克逊)
我正在使用基于 Saxon 的 Java 应用程序作为 XPath 解析器。请考虑以下
<import>
<record ref="abc">
<id>123</id>
<data>Value<data>
</record>
<record ref="def">
<parent>123</parent>
<data>Value2</data>
</record>
</import>
事项:我的用例是,我正在循环记录节点,并且需要查找属性 ref 等于子父节点下的值的另一个节点。
如果当前节点是
,如何编写查询以通过匹配 /query/record/ 返回第一个记录节点内的数据节点id/text() = 当前节点/父节点/text() ?
如果我执行这个查询:
/import/record[id/text() = '123']/data
那么我得到了正确的数据节点,但我似乎无法替换父节点下的值“123”?
我尝试将 '123' 替换为 ./parent/text()
并且在 XPath 编译时,没有返回任何结果。
谢谢,
杰比
I'm using a Java application based on Saxon for the XPath parser. Please consider the following:
<import>
<record ref="abc">
<id>123</id>
<data>Value<data>
</record>
<record ref="def">
<parent>123</parent>
<data>Value2</data>
</record>
</import>
My use case is, I'm looping through the record nodes and need to look up another with attribute ref equal to the value under the child parent node.
If the current node is <record ref="def">
, how can I write a query to return the data node within the first record node by matching on /query/record/id/text() = current node/parent/text()
?
If I execute this query:
/import/record[id/text() = '123']/data
Then I get the correct data node, but I can't seem able to replace '123' for the value under the parent node?
I've tried replacing '123' with ./parent/text()
and while the XPath compiles, no results are returned.
Thanks,
JB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于某种原因,current() 存在于 XSLT 中,但不适用于针对 DOM 的 XPath。您可以硬编码如下内容:
/import/record[id = /import/record[position()]/parent]/data
For some reason, current() exists in XSLT but not for XPath against a DOM. You can hard code something like this:
/import/record[id = /import/record[position()]/parent]/data
周期用于指代当前节点。但是,您的逻辑有一个缺陷:您的路径
定义的是一个
,它有一个parent
和 一个id
子元素必须具有相同的内容。The period is used to refer to the current node. However you have a flaw in your logic: What your path
defines is a
<record>
that has aparent
and aid
child which must have the same content.当您在路径上下文中引用节点时,您应该能够使用变量来解决您的问题:
编辑
您可能会得到类似于以下单个 xpath 表达式的内容:(
选择这些记录是别人引用的)
With the period you are referencing a node in the path context, you should be able to use variables to fix your problem:
edit
You might get away with something like the following single xpath expression:
(select those records that are referenced by others)