tags:

views:

53

answers:

2

Hi,

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?

+2  A: 

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))
Dimitre Novatchev
@Dimitre: +1 Another good explanation
Alejandro
A: 

Assuming you have your ID as a variable already (lets say $myId), then try using:

//party[contains($myId, @id)]

The contains() function will look to see on each matching node whether or not the partyId attibut is in teh value that you pass in.

Alternatively (as that could be considered 'ropey'), you can try:

//party[@id=substring($myId, 2, 1 div 0)]

the substring() function should be a little more precise.

mnield
@mnield: It's OK to use p/2 `fn:substring()`, you don't need that `INF` expression.
Alejandro