Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath select all elements between two specific elements

Tags:

xml

xslt

xpath

I have a following xml:

<doc>     <divider />     <p>text</p>     <p>text</p>     <p>text</p>     <p>text</p>     <p>text</p>     <divider />     <p>text</p>     <p>text</p>     <divider />     <p>text</p>     <divider /> </doc> 

I want to select all p nodes after first divider element until next occurrence of divider element. I tried with following xpath:

//divider[1]/following-sibling::p[following::divider] 

but the problem is it selects all p elements before last divider element. I'm not sure how to do it using xpath 1.

like image 284
Mirko Avatar asked Jun 02 '12 04:06

Mirko


People also ask

What is an XPath query?

XPath (XML Path Language) is a query language that can be used to query data from XML documents. In RUEI, XPath queries can be used for content scanning of XML documents. A complete specification of XPath is available at http://www.w3.org/TR/xpath .

What is XPath expression in XML?

XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.

What is syntax for XPath?

Syntax of XPath Below is the syntax for Xpath: Xpath =//tagname[@Attribute='value'] Wherein: //: Used to select the current node. tagname: Name of the tag of a particular node.


1 Answers

Same concept as bytebuster, but a different xpath:

/*/p[count(preceding-sibling::divider)=1] 
like image 187
Daniel Haley Avatar answered Sep 20 '22 07:09

Daniel Haley