XPath可以跨XML的两个子树进行外键查找吗?

dac*_*cot 4 xml xslt xpath subtree xslkey

说我有以下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>
Run Code Online (Sandbox Code Playgroud)

... XPath会返回"bucket"包含"red"和"blue"的内容?

Jen*_*niT 5

如果你正在使用XSLT,我建议你设置一个密钥:

<xsl:key name="tents" match="base/tent" use="@key" />
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用特定的内容获取<tent>内部<base>key

key('tents', $id)
Run Code Online (Sandbox Code Playgroud)

那你可以做

key('tents', /root/bucket/tent/@key)/@color
Run Code Online (Sandbox Code Playgroud)

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

key('tents', $bucket/tent/@key)/@color
Run Code Online (Sandbox Code Playgroud)