Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use xpath to select elements with a set of multiple attributes/values

Tags:

xml

xslt

xpath

I have an XML document that i need to strip out particular pieces of data

the xml document has a structure as follows:-

<a>    <b select='yes please'>        <c d='text1' e='text11'/>        <c d='text2' e='text12'/>        <c d='text3' e='text13'/>        <c d='text4' e='text14'/>        <c d='text5' e='text15'/>    </b>  </a> <a>    <b select='no thanks'>        <c d='text1' e='text21'/>        <c d='text3' e='text23'/>        <c d='text5' e='text25'/>    </b>  </a> <a>    <b select='yes please'>        <c d='text1' e='text31'/>        <c d='text2' e='text32'/>        <c d='text3' e='text33'/>        <c d='text4' e='text34'/>        <c d='text5' e='text35'/>    </b>  </a> <a>    <b select='no thanks'>        <c d='text4' e='text41'/>        <c d='text3' e='text43'/>        <c d='text5' e='text45'/>    </b>  </a> 

i need to select only those /a/b element groups that have d attribute = 'text1' and d attribute = 'text4', once i have identified these sub documents i want to get the value of the e attributes with d attribute value 'text5'

hope thats clear

Cheers

DD

like image 785
Hector Avatar asked Jul 16 '11 15:07

Hector


People also ask

What is text () in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.


2 Answers

You can use this XPath:

//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']/@e 

It will select e='text15' and e='text35' of 1st and 3rd a/b

XSLT:

<xsl:template match="//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']">   <xsl:value-of select="@e"/> </xsl:template> 
like image 84
Kirill Polishchuk Avatar answered Sep 21 '22 18:09

Kirill Polishchuk


i need to select only those /a/b element groups that have d attribute = 'text1' and d attribute = 'text4', once i have identified these sub documents i want to get the value of the e attributes with d attribute value 'text5'

hope thats clear

Yes, it's so clear the translation into XPath is almost mechanical

(: those /a/b element groups :) a/b  (: that have d attribute = 'text1' :) [c/@d='text1']  (: and d attribute = 'text4' :) [c/@d='text4']  (: and .. i want to get the value of the e attributes     with d attribute value 'text5' :) / c[@d='text5'] / @e 
like image 27
Michael Kay Avatar answered Sep 18 '22 18:09

Michael Kay