Definition of XPath parent
In XPath, the parent node of the current node selected in the web page is retrieved using the Parent method. It comes in handy when we choose an element and need to utilise Xpath to fetch the parent element. This method can also be used to find out who the parent’s parents are and abbreviated as (..). XPath parent is one of the methods of Xpath Axes. XPath axes aid in the discovery of elements in an XML document based on their relationships with other components.
The following is the syntax for the parent’s axis and the XPath expression will be given as:
How does XPath parent work?
Axes in XPath are techniques for identifying dynamic elements that are impossible to detect using standard XPath methods such as class. Axes are named after the axis on which elements are arranged relative to one another. Each element and attribute has a single parent. As a result, the parent axis is empty when the current node is the root node. The parent axis can only have one node for all other element nodes. So We need to traverse through the HTML tags down to desired target object. Next from a Parent Node starts with an element and down from that is child nodes then sub-nodes and lastly a child leaf node. When we consolidate all together, we would get XPath derived from which is shown below:
Let’s take input as
The XPath expression goes like this:
A Parent of a context node is selected Flat element.
The result is
To find a parent / getting nodes relative to the present node. Lets have a scenario like
The Xpath Expression goes like this
This gives the result as:
A string of elements is normally separated by a slash in an XPath statement. You can pick the parent element by inserting two periods “..” where an element would typically be. The parent of the element to the left of the double period will be selected.
Selecting a parent with Xpath Expression: For example, if we need to select all the sale elements like leather, cotton normally we would give a few expressions like
Goods/Leather/bags or Goods/Leather/cotton.
This can be simplified by selecting the parent element of the amount to yield the same three sale elements. It is because all the products have the same element ‘amount’. Therefore, an expression goes like this:
This expression picks all of the needed elements in our XML document using a relative path location. It then selects the parent element of each amount element using the parent sequence “..”
Though XPath finds an element using different functions in many situations, few complex functions like ancestors, parent helps to retrieve an element simply.
Following examples can be considered to use the parent axes to traverse to the parent of an element. Let’s begin with the implementation of the parent Xpath axis along the path.
As an example, consider the XML file below. A Tuition element is represented by a number of nodes in the File:
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
Using XSLT the above parent axes is retrieved as shown below:
Xpath parent is written as above using a specified Syntax format. Next, the Xpath expression retrieves the element as shown below:
It displays the parent node. If it is single it takes up the parent element of the time hour which is clocks.
The XML code above retrieves the Parent node in the tester by representing like parent: *and the result is given as a
public class Parent
public static void main(String args) throws Exception
DocumentBuilderFactory docB = DocumentBuilderFactory.newInstance();
DocumentBuilder db = docB.newDocumentBuilder();
Document dc = db.parse("D:/parser/shu.xml");
XPathFactory xpf = XPathFactory.newInstance();
XPath xpa = xpf.newXPath();
XPathExpression ex = xpa.compile("//Shuttler/parent::*");
Node parent = (Node) ex.evaluate(dc, XPathConstants.NODE);
System.out.println("The Shutters in the team are: ");
System.out.println("The Parent element is "+parent.getNodeName());
The above java code accesses any tag of the parent using the parent axis. Here the path expression returns a list of nodes and the parent axis is represented as a parent::. The output displays as
Coming to an end, this article simply explains XPath parents. And we have also seen a few implementations of how to access a parent node effectively.
This is a guide to XPath parent. Here we discuss the Introduction, syntax, How does XPath parent works? examples with code implementation. You may also have a look at the following articles to learn more –
- XPath Expressions
- XPath Relative
- XPath Axes
- XPath Nodes
The post XPath parent appeared first on EDUCBA.