Hector
Hector

Reputation: 5356

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

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

Upvotes: 35

Views: 70293

Answers (4)

Michael Kay
Michael Kay

Reputation: 163322

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

Upvotes: 5

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

Use this single XPath expression:

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

Upvotes: 1

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

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>

Upvotes: 37

Emiliano Poggi
Emiliano Poggi

Reputation: 24826

You can use a single template to match the required group, and then you get the value of the required attributes:

<xsl:template match="/*/a/b[c[@d='text1'] and c[@d='text4']]">
    <xsl:value-of select="c[@d='text5']/@e"/>
</xsl:template>

assuming:

<root>
    <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>
</root>

the output will be text15 and text35.

Upvotes: 4

Related Questions