Home > Software design >  XPath to select multiple elements?
XPath to select multiple elements?

Time:06-30

<?xml version="1.0" encoding="UTF-8"?>
<a>
   <b>
      <c>110</c>
      <d>periodendyear="2019"</d>
      <e>
         <f>
            <code>xyz</code>
            <value>54</value>
         </f>
      </e>
      <e>
         <f>
            <code>xy</code>
            <value>6</value>
         </f>
      </e>
   </b>
</a>

How would you select all the a, d and code and value elements that are children of b elements?

Basically, something like:

//b | //f

CodePudding user response:

If you want to select all the c, d, code, value nodes that are descendants of the b node with the single XPath expression try

//b//*[name()=('c', 'd', 'code', 'value')]

CodePudding user response:

How would you select all the a, d and code and value elements that are children of b elements?

Note that there are no code and value children of b elements, so I'll assume that you meant descendants, not just children.

This XPath,

//b//*[self::a or self::d or self::code or self::value]

will select all a, d and code and value elements that are descendants of all b elements anywhere in the document.

  • Related