问题
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