1

Say I have this XML:

<root>
    <A></A>
    <B></B>
    <C>one</C>
    <C>two</C>
    <C>three</C>
    <D></D>
</root>

Now I want to get all nodes, except for The C nodes 'two' and 'three'. Now I want to choose which C node can stay by index.

So this is the xpath that I already have:

 //*[not(ancestor-or-self::C)]

But this removes all C nodes, so now I have to add an index to which C node I want to stay

//*[not(ancestor-or-self::C)] exept for C[1]

How can I accomplsh this so my output would be if I select index 1:

<root>
   <A></A>
   <B></B>
   <C>one</C>
   <D></D>
</root>

Or if I select index 2:

<root>
   <A></A>
   <B></B>
   <C>two</C>
   <D></D>
</root>

Hope I made myself clear enough :p thx

1
  • Good question, +1. See my answer for a simple XPath expression that selects exactly the wanted nodes. Commented Nov 19, 2010 at 19:39

3 Answers 3

1

Why except? You have any element but those, then you add some of those:

//*[not(ancestor-or-self::C)]|//C[1]/descendant-or-self::*
Sign up to request clarification or add additional context in comments.

Comments

1

i think it depends on what you define as "index". If you define "index" as the C that has n-1 C siblings then you can just do:

to display one the first (get rid of all afterwards

/root/*[not(name() = 'C'  and count(preceding::C) >= 1)]

to display only the second

/root/*[not(name() = 'C' and (count(preceding::C) < 1 or count(preceding::C) > 1))]

(I tested this with: http://www.xmlme.com/XpathTool.aspx just fyi )

Comments

0

Use:

//*[not(self::c and not(count(preceding-sibling::c) = $ind - 1))]

where $ind is the wanted position (1-based) of the c element you want to select.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.