My document looks like the following:
<a>
whatever
</a>
If I run /
or /a
on the entire document is returned(at least effectively).
If I run /a/..
the entire document is returned.
But /..
returns an empty sequence
Considering /
and /a
are returning the same node how come /a/..
and /..
are different?
They both are the same and what they mean is that root is a pointer which is pointer towards a Node type. It means the root from now onwards will be able to store the address of a variable whose data type is also Node.
– n. 1.8e9-where's-my-share m. They both are the same and what they mean is that root is a pointer which is pointer towards a Node type. It means the root from now onwards will be able to store the address of a variable whose data type is also Node.
All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created. The node tree shows the set of nodes, and the connections between them. The tree starts at the root node and branches out to the text nodes at the lowest level of the tree: The image above represents the XML file books.xml.
Nodes are small, individual anatomical structures scattered mainly within the connective tissue. The main characteristic feature of a node is that they are not encapsulated by a connective tissue layer. Also, two common examples of nodes are lymph nodes and sinus nodes.
The XML code you provided as document is actually wrapped in another node, the "document node". The document is another node kind, others are elements, attributes, text nodes, comments and processing instructions. Using XQuery/XPath 2.0 notation, it would look something like this:
document{
<a>
whatever
</a>
}
/
selects the document node/a
selects the root element, which is the only child of the document node/..
returns the empty sequence, as the document node has no parent node/a/..
again selects the parent node of the root element, which again is the document node/../a
has no results, as we "stepped out of the tree" (compare with /..
)The document node is important, as the XML specification allows other nodes to follow the root node, namely processing instructions and comments (and whitespace). From the XML grammar:
document ::= prolog element Misc*
Misc ::= Comment | PI | S
Without a document node, these elements wouldn't be reachable for XPath, as they are no elements of the "root element subtree".
So, this would also be a valid XML document (*):
document {
<a>
whatever
</a>
<!-- Just do nothing -->
<?php foo(); ?>
}
(*) This isn't valid XPath 2.0 any more, as we would have to give a node sequence. I omitted the commas ,
after each node necessary for XPath 2.0, as this is only for demonstration purpose.
The expressions /
and /a
are not the same and don't return the entire document. /
selects a node set containing the document root. The root node in XPath (or document node in XPath 2.0) is kind of a virtual node which sits above the document element./a
selects a node set containing the document element.
The expression /a/..
selects the parent of the document element which is the root node. The expression /..
selects the parent of the root node. Since the root node has no parent, it returns the empty node set. This expression is also a common idiom to select the empty node set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With