views:

418

answers:

3

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!

+1  A: 

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.

zedoo
I believe your right Zedoo. There is no way, so far, that I can select element "b" with child "i" and "ii". You will always get the extra children nodes ("iii"). Thanks for the help everyone!
Mutter
+1  A: 
/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.

Ron's Brain
A: 

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
Tomalak