开发者

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

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="par开发者_开发知识库tyA" >
   < 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?


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))


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 attibute is in the 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜