XPathは、XMLの2つのサブツリー間で外部キー検索を実行できますか?

StackOverflow https://stackoverflow.com/questions/142010

質問

次の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>

... <!> quot; bucket <!> quot;を返すXPathはどうなりますか? <!> quot; red <!> quot;を含むおよび<!> quot; blue <!> quot;?

役に立ちましたか?

解決

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

他のヒント

これはうまくいくと思う:

/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ドキュメントを調べる前にキーを作成し、そのキーに対して一致を実行する必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top