4

假设我有以下 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 是什么?

4

4 回答 4

5

如果您使用的是 XSLT,我建议您设置一个密钥:

<xsl:key name="tents" match="base/tent" use="@key" />

然后,您可以使用特定的方法获得<tent>内部<base>key

key('tents', $id)

然后你可以做

key('tents', /root/bucket/tent/@key)/@color

或者,如果$bucket是特定<bucket>元素,

key('tents', $bucket/tent/@key)/@color
于 2008-09-27T21:19:35.683 回答
2

我认为这会起作用:

/root/base/tent[/root/bucket/tent/@key = @key ]/@color
于 2008-09-26T21:21:51.583 回答
1

这不漂亮。与任何查找一样,您需要使用 current():

/root/bucket[/root/base/tent[@key = current()/tent/@key]/@color = 'blue' 或 /root/base/tent[@key = current()/tent/@key ]/@color = '红色']

于 2008-09-27T01:31:25.523 回答
0

JeniT 在此处列出了适当的响应/代码。您需要在遍历 XML 文档之前创建密钥,然后针对该密钥执行匹配。

于 2008-09-29T21:23:14.050 回答