XPath可以跨XML的两个子树进行外键查找吗?
题
说我有以下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将返回什么<!>“; bucket <!>”;包含<!>“; red <!>”;和<!> quot; blue <!>;?
解决方案
如果您使用的是XSLT,我建议您设置密钥:
<xsl:key name="tents" match="base/tent" use="@key" />
然后,您可以使用
获取特定<tent>
<base>
的key
$bucket
key('tents', $id)
然后你可以做
key('tents', /root/bucket/tent/@key)/@color
或者,如果<bucket>
是特定的<=>元素,
key('tents', $bucket/tent/@key)/@color
其他提示
我认为这会奏效:
/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 ='red']
JeniT在此处列出了相应的响应/代码。您需要在遍历XML Document之前创建密钥,然后对该密钥执行匹配。
不隶属于 StackOverflow