user243264
user243264

Reputation:

Using XPATH, how do I select multiple elements while retaining the path

Assuming I have a XML like so:

<a>
  <b>
    <i>data</i>
    <ii>data</ii>
    <iii>data</iii>
  </b>
  <b>
    <i>data<i>
    <ii>data<ii>
    <iii>data</iii>
  </b>
</a>

Using XPath, how would I select the above XML to create a structure like so:

  <b>
    <i>data</i>
    <ii>data</ii>
  </b>
  <b>
    <i>data<i>
    <ii>data<ii>
  </b>  

In this scenario I'm only interested in i and ii and, but want to retain the outer element. I also cannot use XSLT, only XPATH statements.

Thanks!

Upvotes: 2

Views: 9582

Answers (3)

Tomalak
Tomalak

Reputation: 338376

To select all nodes and including their parent, outer nodes:

/a[i or ii]|/a/i|/a/ii|/b[i or ii]|/b/i|/b/ii

Upvotes: 1

Ron&#39;s Brain
Ron&#39;s Brain

Reputation: 271

/a/*/i/..|/a/*/ii/..

"From a, select all children, then select all "i" elements, then select the parent, OR from a select all children then select all "ii" elements, then select the parent.

Upvotes: 3

zedoo
zedoo

Reputation: 11287

Maybe I'm wrong, but I thought XPATH only selects sequences of "nodes", in its own abstract model. I would be lost without XSLT here.

Upvotes: 1

Related Questions