Search code examples
xmlxpathxpath-1.0

XPath 1.0 - Information from next node


I've got this partial XML:

 <Events>
   <Properties>
     <Property Descriptor="1">VALUE1</Property>
     <Property Descriptor="2">1</Property>
     <Property Descriptor="3">start</Property>
     </Properties>
   <Properties>
     <Property Descriptor="1">VALUE2</Property>
     <Property Descriptor="2">1</Property>
     <Property Descriptor="3">end</Property>
   </Properties>
 </Events>

If I'm in "VALUE1" how can I query "VALUE2" [next PROPERTIES node]?

I tried /next-sibling but I'm not sure how to apply it here


Solution

  • You probably want following-sibling.

    Demonstrating from the top of your document:

    //Property[@Descriptor="1"][1]/../following-sibling::Property[1]
    

    If you're already at the property with descriptor 1:

    ./following-sibling::Property[1]
    

    The above was based on a misreading of the question. See instead:

    xmlstarlet sel -t -m '//Property[@Descriptor="1"][1]' \
      -v '../following-sibling::Properties[1]/Property[@Descriptor=./@Descriptor][1]' \
      -n <foo.xml
    

    The first -m argument simply finds the first Property; the magic is in the -v, which looks for the sibling Properties and finds a Property within it with a matching Descriptor.