Use of preceding-sibling in XPath

In the previous sections of XPath tutorials you
must have well studied about preceding, following-sibling axes.
Now in this tutorial you are going to be familiar with one more axis,
"preceding-sibling". "preceding-sibling" will select all
the sibling elements before the context node.
In this example we have created an XML file "persons.xml"
as in our previous section, which is necessary to execute XPath query on
to it. This "persons.xml" contains information related to name,
age, gender of different persons.
Here is the full source code for persons.xml file
as follows :
persons.xml
<?xml version="1.0" ?>
<information>
<person id="1">
<name>Deep</name>
<age>34</age>
<gender>Male</gender>
</person>
<person id="2">
<name>Kumar</name>
<age>24</age>
<gender>Male</gender>
</person>
<person id="3">
<name>Deepali</name>
<age>19</age>
<gender>Female</gender>
</person>
<!-- more persons... -->
</information> |
Now we have declared a class XPathChildAxis and
in this class we are parsing the XML file with JAXP. First of all we need
to load the document into DOM Document object. We have put that
persons.xml file in that current working directory.
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("persons.xml"); |
Above lines of code parses "persons.xml"
file and creates a Document object. Next we have created XPath object
with the use of XPathFactory.
XPath xpath = XPathFactory.newInstance().newXPath(); |
XPath expression "//person/preceding-sibling::*"
will select all the preceding -sibling nodes excluding any ancestors element and
excluding attribute elements or nodes and namespace nodes.
Here is the full example code for XPathPrecedingSibling.java
as follows:
XPathPrecedingSibling.java
import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;
public class XPathPrecedingSibling {
public static void main(String[] args)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("persons.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr =
xpath.compile("//person/preceding-sibling::*");
// Selecting person element's preceding-sibling name
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeName());
}
expr = xpath.compile("//person/preceding-sibling::*/*/text()");
// Selecting all person element's preceding-siblings value
result = expr.evaluate(doc, XPathConstants.NODESET);
nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength();i++) {
System.out.print(nodes.item(i).getNodeValue()+", ");
}
}
}
|
Output:
To run this example follow these steps as :
- create and save persons.xml
- create and save XPathPrecedingSibling.java
- compile and execute XPathPrecedingSibling
class and you will get following output on your command prompt.

Download Source
Code

|