views:

142

answers:

2

Hi,

I'm new to XPATH and I'd like to know if there is a nice way to do this using XPATH queries.

I want to match consecutive sibling nodes in a XML tree to certain predefined rules for example if there are three sibling nodes occurring immediately one after the other and they have the attributes value="A", value="B" and value="C", I want the XPATH query to match the first node/last node in such a sequence and I want to extract all such consecutive sequence of nodes from the XML tree so that I can process them later on.

Thanks!

A: 

Note: This doesn't answer the question. I misunderstood it when writing this.

To query them, use position()=1 or position()=last() as a predicate, possibly along with other predicates you use to select the nodes.

This is what it looks like in XpathVisualizer:

alt text

"Extracting them" is not a matter for xpath. For that you can enumerate the selected elements and then do whatever you like, it's up to you.

Cheeso
Seems you didn't grok the question?
Dimitre Novatchev
This answer is quite astray from a correct solution for the well-defined problem. Also, be aware that the first and real XPathVisualizer was produced and used by many thousands of people long before Google became a company. As I see from your pic, the fake one doesn't provide even half of the functionality of the real XPathVisualizer, that people have been using for 10 years.
Dimitre Novatchev
Dimitre, i sense some negative vibes, not sure what that's all about. I have no problem with you. If you have a problem or concern with me, maybe you should contact me directly, rather than indirectly like this. I'm not sure why you feel the need to call my project the "fake" xpath visualizer. It's real, it's not imaginary. Best to you.
Cheeso
@Cheeso: I always have "negative vibes" about wrong answers that aren't corrected and mislead people. Anyone who knows that their answer is wrong and keep it uncorrected, willingly propagates and supports ignorance.
Dimitre Novatchev
Dmitre - Sometimes I am wrong. Sometimes I misunderstand the question. It happens. It happened twice yesterday. Why are you so up in arms about it?
Cheeso
@Cheeso: Intentionally leaving wrong information is harmful to all people, who read it and are misled by it. It is very much like lying. The only correct and responsible action in such case is for the responder to correct or delete the answer.
Dimitre Novatchev
Grind your axe somewhere else, please.
Cheeso
+2  A: 

Here is one possible solution:

First:

I want to match consecutive sibling nodes in a XML tree to certain predefined rules for example if there are three sibling nodes occurring immediately one after the other and they have the attributes value="A", value="B" and value="C",

//*[@value='A' 
  and 
   following-sibling::*[1]/@value='B' 
  and 
   following-sibling::*[2]/@value='C']

Then:

I want to extract all such consecutive sequence of nodes from the XML tree ...

    //*[@value='A' 
      and 
       following-sibling::*[1]/@value='B' 
      and 
       following-sibling::*[2]/@value='C']
   |
    //*[@value='A' 
      and 
       following-sibling::*[1]/@value='B' 
      and 
       following-sibling::*[2]/@value='C']

      /following-sibling::*[position() = 1 or position()=2]

Here is how the selection looks like in the true XPathVisualizer (_http://www.topxml.com/xpathvisualizer/ -- this link has a trojan -- click only if you have good malware protection. Alternatively, contact me for the app.):

alt text

Finally:

I want the XPATH query to match the first node/last node in such a sequence

    //*[@value='A' 
      and 
       following-sibling::*[1]/@value='B' 
      and 
       following-sibling::*[2]/@value='C']
   |
    //*[@value='A' 
      and 
       following-sibling::*[1]/@value='B' 
      and 
       following-sibling::*[2]/@value='C']

      /following-sibling::*[position()=2]
Dimitre Novatchev