Good Place. Good Things. Good Time.

XPath parent

XPath parent

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.

Syntax

The following is the syntax for the parent’s axis and the XPath expression will be given as:

/parent::tagName

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:

/Home/Flat[21]/A[21]/parent::Flat

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

//XX/parent::node()
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.



$5.50 80

$2.50 100


$6.50 90
800


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:

Amount/…

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.

Examples

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.

Example #1

As an example, consider the XML file below. A Tuition element is represented by a number of nodes in the File:

pp.xml






Dinpam
Var
Nickil
81


Edwin
Khoen
Vennice
97


Jaysh
Singh
Donald
94

pp.xslt



xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">



Initial Delegate:




Explanation

Using XSLT the above parent axes is retrieved as shown below:

Output:

Xpath parent 1

Example #2



Harry Potter
fiction
Rose XXX


Comic battle
Adventerous
Mark XXX
1-453-2251-3


Mummy returns
Action
Tom ZZZ


Family man
Family
Rajesh jay
2-365-18465-8


Hero
Pink

Explanation

Xpath parent is written as above using a specified Syntax format. Next, the Xpath expression retrieves the element as shown below:

Output:

Xpath parent 2

Example #3

It displays the parent node. If it is single it takes up the parent element of the time hour which is clocks.



10
30
45
a.m.

Explanation

The XML code above retrieves the Parent node in the tester by representing like parent: *and the result is given as a

Result:

Xpath parent 3

Example #4

Parent.java

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
public class Parent
{
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory docB = DocumentBuilderFactory.newInstance();
docB.setNamespaceAware(true);
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());
}
}

shu.xml




Howard Shu
Captain
USA


Loh Kean Yew
XXX
Singapore


Johanney
Substitute
German


Sindhu
Captain
India

Explanation

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

Output:

output

Conclusion

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.

Recommended Articles

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 –

  1. XPath Expressions
  2. XPath Relative
  3. XPath Axes
  4. XPath Nodes

The post XPath parent appeared first on EDUCBA.

Read original article here: XPath parent

Read original article here: XPath parent



Disclaimers and Denial of responsibility..!

Denial of responsibility! TheTopMag.com is an automatic aggregator of all media around the world. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – jamesonline999@gmail.com . The content will be deleted within 72 hours.
You might also like
Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.