Xpath Query to match closest node nodes in sequence

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

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

Thanks!

+2


a source to share


2 answers


Here's one possible solution :

The first:

I want to map consecutive siblings in an XML tree with certain predefined rules, for example if there are three siblings occurring immediately one after the other and they have 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 sequential sequence of nodes from an 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]

      

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

alt text

Finally:

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

    //*[@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]

      

+3


a source


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

To query them, use them position()=1 or position()=last()

as a predicate, possibly in conjunction with other predicates that you use to select nodes.

This is how it looks in the XpathVisualizer :



alt text

"Extracting them" is irrelevant to the xpath. To do this, you can list the selected items and then do whatever you like is up to you.

+1


a source







All Articles