XPath可以跨XML的两个子树进行外键查找吗?
时间:2020-03-06 14:48:34 来源:igfitidea点击:
说我有以下XML ...
<root> <base> <tent key="1" color="red"/> <tent key="2" color="yellow"/> <tent key="3" color="blue"/> </base> <bucket> <tent key="1"/> <tent key="3"/> </bucket> </root>
...返回的"存储桶"包含"红色"和"蓝色"的XPath是什么?
解决方案
我认为这会起作用:
/root/base/tent[/root/bucket/tent/@key = @key ]/@color
不好看与任何查找一样,我们需要使用current():
/ root / bucket [/ root / base / tent [@key = current()/ tent / @ key] / @ color ='blue'或者/ root / base / tent [@ key = current()/ tent / @ key ] / @ color ='红色']
如果我们使用的是XSLT,建议我们设置一个密钥:
<xsl:key name="tents" match="base/tent" use="@key" />
然后,我们可以使用特定的"键"在" <base>"内获取" <tent>"。
key('tents', $id)
那你可以做
key('tents', /root/bucket/tent/@key)/@color
或者,如果$ bucket是特定的<bucket>元素,
key('tents', $bucket/tent/@key)/@color
JeniT在此处列出了适当的响应/代码。在遍历XML文档之前,我们需要先创建密钥,然后再对该密钥进行匹配。