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

67,797

Solution 1

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>

Solution 2

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

Solution 3

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.

Solution 4

Use this single XPath expression:

/*/a/b[c/@d='text1' and c/@d='text4']
         /c[@d='text5']
             /@e
Share:
67,797
Hector
Author by

Hector

Updated on August 11, 2020

Comments

  • Hector
    Hector almost 4 years

    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