This is a paragraph
``), +- or **attribute nodes** (``href="page.html"`` inside an ```` tag), +- or **text nodes** (``"I have something to say"``), +- or **comment nodes** (````), +- (or root nodes, or namespace nodes, or processing instructions nodes + but we will not cover them here.) + +In XPath's data model, everything is a node : elements, attributes, +comments... (**but not all nodes are elements.**) + +And nodes have an order, the **document order**: the order in which they +appear in the XML/HTML source. + +In effect, this data model allows you to represent everything inside an +XML or HTML document, in a structured, ordered and hierarchical way. + +Throughout this tutorial, we'll use the following sample HTML page to +illustrate how XPath works: + +:: - -This is the first paragraph
. - - + ++ | | | | +-- #20--(TXT): 'This is a paragraph.' + | | | +-- #21--(TXT): '\n ' + | | | +-- #22--
+ | | | | +-- #23--(TXT): 'Is this '
+ | | | | +-- #24--
+ | | | | | +-- #25--(ATTR): href: 'page2.html'
+ | | | | | +-- #26--(TXT): 'a link'
+ | | | | +-- #27--(TXT): '?'
+ | | | +-- #28--(TXT): '\n '
+ | | | +-- #29-- ``: these are element nodes
+- ``(TXT)`` represent text nodes
+- ``(ATTR)`` represent attribute nodes
+- ``(COMM)`` represent comment nodes
+
+The ``# This is the first paragraph Is this a link? `` and `` `` paragraph elements
+inside the `` This is a paragraph. Is this a link? This is a paragraph. Is this a link? This is a paragraph. Is this a link? `` under each `` This is a paragraph. Is this a link? This is a paragraph. Is this a link?
+ | +-- #22--
+ | | +-- #24--
+ | +-- #29--
+ | | +-- #22--
+ | | | +-- #24--
+ | | +-- #29-- This is a paragraph. Is this a link? Is this a link? This is a paragraph.
+ | |
+ | |
+ self::* -----------------------> +-- #22--
+ | | |
+ | | +-- #24--
+ | |
+ | |
+ following-sibling::* ----------> +-- #29-- This is a paragraph. This is a paragraph. Is this a link? This is a paragraph.
+ | | | +-- #30--(TXT): '\n Apparently.\n '
+ | | +-- #31--(TXT): '\n '
+ | | +-- #32--Welcome to my page
-
`` being selected? It's an empty element (i.e. with node children)
+but it is there nonetheless.
+
+Selecting text nodes
+~~~~~~~~~~~~~~~~~~~~
+
+If we stay around these ``
`` elements, you may have noticed
+that the ASCII tree representation from the beginning also shows some text after the
+``
`` break: the string ``"Apparently."``. It is a text node.
+
+Selecting text nodes is a bit different than selecting elements:
+you use the special ``text()`` syntax. Let's try it by replacing the last
+part of our last XPath expression, forming ``/html/body/div/div[1]/text()``:
+
+.. xpathdemo:: /html/body/div/div[1]/text()
+
+
+
+ +-- #32--
+ +-----------> +-- #32--
'
+ (6) '
']
+
+
+
+Again, let's see which elements were selected in our ASCII tree
+representation:
+
+::
+
+ # 0--(ROOT)
+ +-- # 1--
+ +-- # 3--
+ | +-- # 5--
+ |
+ |
+ +-- #32--
'
+ (3) '
+ Apparently.
+ ']
+
+
+
+Nodes before and after, in document order
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+``preceding`` and ``following`` are 2 special axes that do not look at
+the tree hierarchy, but work on the document order of nodes.
+
+Remember, all nodes in XPath data model have an order, called the
+*document order*. Node 1 is the first node in the HTML source, node 2 is
+the node appearing next etc.
+
+::
+
+ #1 #2 #3 ...
+
'
+ (2) '