XPath Nodes
Nodes
Nodes
There are seven kinds of nodes in XPath:
- Root: it is the topmost element of the document with child elements below it.
- Element (
<tagname> ... </tagname>
): these type follows the root node and can contain attribute in it. - Attribute (
<tagname attribute="value">
): this can be assigned below the element or root node. - Text: it represents the text written inside an element node.
- Namespace: it matches namespace for all the nodes. This is defined at the beginning of the document in which the root element is declared.
- Processing-instruction (
<? ?>
): this specifies the instruction for the application and can be used anywhere in the XML document. - Comment (
<comment> </comment>
): this node specifies the comment part for the particular code that is not processed by any programming language compiler.
An XML document can be specified as a tree of nodes. The highest element in the tree is called the root element.
Consider this example:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<bookstore>
<book>
<title lang="en">A Great Book</title>
<author>Tutorial Reference</author>
<year>2022</year>
</book>
</bookstore>
<comment>This is a comment</comment>
We have that:
<bookstore>
is the root element node.<author>Tutorial Reference</author>
is an element node.Tutorial Reference
is a text node.lang="en"
is an attribute node.<?xml-stylesheet type="text/css" href="style.css"?>
is a processing-instruction node.<comment>This is a comment</comment>
is a comment node.
Atomic values
Atomic values are nodes with no children or parent.
For example:
Tutorial Reference
"en"
Relationship of Nodes
Parent
Each element and attribute has one parent which is a top element of the respective element or attribute.
For example, the <book>
element is the parent of <title>
, <author>
and <year>
elements
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">A Great Book</title>
<author>Tutorial Reference</author>
<year>2022</year>
</book>
</bookstore>
Children
Element nodes can have zero, one or more children.
For example, <title>
, <author>
and <year>
elements are children of the <book>
element
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">A Great Book</title>
<author>Tutorial Reference</author>
<year>2022</year>
</book>
</bookstore>
Siblings
Nodes that have the same parent are known as siblings.
For example, <title>
, <author>
and <year>
elements are siblings because they have the same parent element <book>
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">A Great Book</title>
<author>Tutorial Reference</author>
<year>2022</year>
</book>
</bookstore>
Ancestors
The parent of a node or the parent of the parent, etc. is specified as the ancestor.
For example, the <book>
element is the parent of <title>
, <author>
and <year>
elements
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">A Great Book</title>
<author>Tutorial Reference</author>
<year>2022</year>
</book>
</bookstore>
Descendants
The children of a node or the children of children, etc. are specified as the descendants.
For example, <book>
, <title>
, <author>
and <year>
elements are descendents of the <bookstore>
element
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">A Great Book</title>
<author>Tutorial Reference</author>
<year>2022</year>
</book>
</bookstore>