Can XPath do a foreign key lookup across two subtrees of an XML?

后端 未结 4 1689
不思量自难忘°
不思量自难忘° 2020-12-18 09:07

Say I have the following XML...


  
    
    
    

        
相关标签:
4条回答
  • 2020-12-18 09:34

    I think this will work:

    /root/base/tent[/root/bucket/tent/@key = @key ]/@color
    
    0 讨论(0)
  • 2020-12-18 09:45

    It's not pretty. As with any lookup, you need to use current():

    /root/bucket[/root/base/tent[@key = current()/tent/@key]/@color = 'blue' or /root/base/tent[@key = current()/tent/@key]/@color = 'red']

    0 讨论(0)
  • 2020-12-18 09:51

    If you're using XSLT, I'd recommend setting up a key:

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

    You can then get the <tent> within <base> with a particular key using

    key('tents', $id)
    

    Then you can do

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

    or, if $bucket is a particular <bucket> element,

    key('tents', $bucket/tent/@key)/@color
    
    0 讨论(0)
  • 2020-12-18 09:51

    JeniT has the appropriate response / code listed here. You need to create the key before you walk the XML Document, then perform matches against that key.

    0 讨论(0)
提交回复
热议问题