Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between / and /root-node

Tags:

xpath

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?

like image 516
callow Avatar asked Mar 04 '14 10:03

callow


People also ask

What is the difference between node and root in C++?

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.

What is the difference between root and node pointer?

– 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.

What is a node tree and how to use it?

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.

What is a node in anatomy?

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.


2 Answers

The Document Node

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>
}

Effects on Queries

  • / 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 /..)

Why we Need a Document Node

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.

like image 185
Jens Erat Avatar answered Sep 27 '22 22:09

Jens Erat


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.

like image 29
nwellnhof Avatar answered Sep 27 '22 21:09

nwellnhof