Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath: find an element value from a match of id attribute to id anchor

Tags:

xpath

I would like to find the value of an element matched on id attribute for which I only have the ref - the bit with #, the anchor.

I am looking for the value of partyId:

 < party id="partyA" >
   < partyId >THEID< /partyId >

but to get there I only have the href from the following

  < MyData >
    < MyReference href="#partyA" />

Strip the # sign does not look good to me.

Any hints?

like image 986
DAg Avatar asked Sep 21 '10 10:09

DAg


1 Answers

Because you haven't provided complete XML documents, I have to use // -- a practice I strongly recommend to avoid.

Suppose that

$vDataRef

is defined as

//MyData/MyReference/@href

and its string value is "#partyA", then one possible XPath expression that selects the wanted node is:

//party[@id=substring($vDataRef,2)]

In case the XML document has a DTD in which the id attribute of party is defined to be of type ID, then it is more convenient and efficient to use the standard XPath function id():

id(substring($vDataRef,2))
like image 53
Dimitre Novatchev Avatar answered Sep 28 '22 19:09

Dimitre Novatchev