XPath to find a node by expression specified within another node

混江龙づ霸主 提交于 2020-06-29 04:05:22

问题


I have to create an XML mapping by entering one single XPath (1.0) for each item; worked fine until I now got an XML document like this:

<PARTIES>       
        <PARTY>         
                <PARTY_ID type="supplier_specific">12345</PARTY_ID>
                <ADDRESS>
                        <NAME>A</NAME>
                </ADDRESS>
        </PARTY>        
        <PARTY>         
                <PARTY_ID type="buyer_specific">54321</PARTY_ID>
                <ADDRESS>
                        <NAME>B</NAME>
                </ADDRESS>
        </PARTY>        
        <PARTY>         
                <PARTY_ID>delivery</PARTY_ID>
                <ADDRESS>       
                        <NAME>C</NAME>
                </ADDRESS>      
        </PARTY>        

        <PARTIES_REFERENCE>
                <BUYER_IDREF type="supplier_specific">12345</BUYER_IDREF>
                <SUPPLIER_IDREF type="buyer_specific">54321</SUPPLIER_IDREF>
                <DELIVERY_IDREF>delivery</DELIVERY_IDREF>
        </PARTIES_REFERENCE>

</PARTIES>

The idea is: if you want the delivery addres, then look at <DELIVERY_IDREF>, take the identifier from there ("delivery") and look for a <PARTY><PARTY_ID> with the same identifier. Result is the <ADDRESS> at the very same level.

So, if I want to know the <NAME> of the delivery address - how do I get there with a single XPath 1.0 expression? I have done quite a number of mappings, but this seems to be the next level, if possible at all.


回答1:


Like this :

/PARTIES/PARTY[./PARTY_ID=/PARTIES/PARTIES_REFERENCE/DELIVERY_IDREF/text()]/ADDRESS/NAME/text()



回答2:


What comes to my mind :

//PARTY_ID[text()=string(//DELIVERY_IDREF)]/following::NAME[1]/text()

Output : C



来源:https://stackoverflow.com/questions/62292230/xpath-to-find-a-node-by-expression-specified-within-another-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!