XPath and node sets

SQL allows for predicate base queries returning a set of data records:

Figure 720. SQL query result Slide presentation Create comment in forum
Query Result set
SELECT name, email
from Customer
WHERE dept='finance'
   or category = 12
{
  ('John Yates', 'yates@company.com'),
  ('Sandra Barnes', 'barnes@testcenter.org'),
...
}

Figure 721. Predicate based queries acting on XML documents Slide presentation Create comment in forum
  • XPath allows for predicate based queries on XML documents.

  • Result: Node set.

  • SQL DML equivalent.

  • Hierarchical queries extending flat SQL.


Figure 722. Comparing SQL and XPath Slide presentation Create comment in forum

Predicate based queries:

SELECT name, email
from Customer
WHERE dept='finance'
   or category = 12
<database>
  ...
    <customer dept='finance'>...

</database>
/database//customer[@dept='finance'
        or category='7']

Figure 723. Similarities and differences Slide presentation Create comment in forum

Predicate base queries:

SQL XPath
  • Predicate based queries using boolean algebra

  • Joins

  • Flat data

  • Hierarchical data support

  • Polymorphic results


Figure 724. Simple XPath queries Slide presentation Create comment in forum
Simple XPath queries

Figure 725. Relative file system search using find Slide presentation Create comment in forum

Starting from relative directory context:

find . -type d -name Gyre-P\*

Result:

./HTML-CSS/fonts/Gyre-Pagella
./SVG/fonts/Gyre-Pagella

Figure 726. find starting from absolute path Slide presentation Create comment in forum

Starting from absolute directory context:

find /etc/ -type f -and -name pac\*

Result:

/etc/brltty/Input/fs/pacmate.ktb
/etc/gnome-app-install/packages-whitelist

Figure 727. XPath and context nodes Slide presentation Create comment in forum
Context node <top>, XPath: b/a
<top>
  <b id="first">
    <a>One</a>
    <a>Two</a>
    <a>Three</a>
    <a>Four</a>
  </b>
  <c>Mixed<a>content></a>Example</c>
</top>

Figure 728. Union Slide presentation Create comment in forum
Context node <top>, XPath: b|c
<top>
  <b id="first">
    <a>One</a>
    <a>Two</a>
    <a>Three</a>
    <a>Four</a>
  </b>
  <c>Mixed<a>content></a>Example</c>
</top>

Figure 729. Union / descent Slide presentation Create comment in forum
Context node <top>, XPath: (b|c)/a
<top>
  <b>
    <a>One</a>
    <a>Two</a>
    <a>Three</a>
    <a>Four</a>
  </b>
  <c>Mixed <a>content</a> Example</c>
</top>

Figure 730. Recursive search for element name Slide presentation Create comment in forum
//a
<top>
  <b>
    <a>One</a>
    <a>Two</a>
    <a>Three</a>
    <a>Four</a>
  </b>
  <c>Mixed <a>content</a> Example</c>
</top>

Figure 731. Recursive + index search Slide presentation Create comment in forum
//a[2]
<top>
  <b>
    <a>One</a>
    <a>Two</a>
    <a>Three</a>
    <a>Four</a>
  </b>
  <c>Mixed<a>content></a>Example</c>
</top>

Figure 732. Surprise! Slide presentation Create comment in forum
//a[1]
<top>
  <b>
    <a>One</a>
    <a>Two</a>
    <a>Three</a>
    <a>Four</a>
  </b>
  <c>Mixed<a>content></a>Example</c>
</top>

Figure 733. Axes / siblings Slide presentation Create comment in forum
preceding-sibling::*

All nodes having the same parent node as the content node and appearing before the context node.

following-sibling::*

All nodes having the same parent node as the content node and appearing after the context node.

Before and after with respect to depth-first tree traversal.


Figure 734. Following sibling Slide presentation Create comment in forum
Context node <a>Two</a>, XPath: following-sibling::*
<top>
  <b>
    <a>One</a>
    <a>Two</a>
    <a>Three</a>
    <x>Four</x>
    <a>Five</a>
  </b>
  <c>Mixed<a>content></a>Example</c>
</top>

XPath provides different axis definitions:

Figure 735. Disjoint XPath axis definitions. Slide presentation Create comment in forum
Disjoint XPath axis definitions.

The sets defined by ancestor, descendant, following, preceding and self are disjoint. Their union equals the set of all document nodes.


Figure 736. Element restricted following sibling Slide presentation Create comment in forum
Context node <a>Two</a>, XPath: following-sibling::a
<top>
  <b>
    <a>One</a>
    <a>Two</a>
    <a>Three</a>
    <x>Four</x>
    <a>Five</a>
  </b>
  <c>Mixed<a>content></a>Example</c>
</top>

Figure 737. Elements containing non-whitespace text Slide presentation Create comment in forum
//*[text()[0 < string-length(normalize-space(.))]]
<top>
  <b id="first">
    <a>A house</a>
    <a>Rent a car</a>
    <a>Two houses</a>
    <a>Five</a>
  </b>
  <c>Building a house</c>
</top>

Figure 738. Elements containing text containing 'house' Slide presentation Create comment in forum
//*[text()[contains(., 'house')]]
<top>
  <b id="first">
    <a>A house</a>
    <a>Rent a car</a>
    <a>Two houses</a>
    <a>Five</a>
  </b>
  <c>Building a house</c>
</top>

Figure 739. Elements containing text of minimal length Slide presentation Create comment in forum
//*[text()[7 < string-length(normalize-space(.))]]
<top>
  <b id="first">
    <a>A house</a>
    <a>Rent a car</a>
    <a>Two houses</a>
    <a>Five</a>
  </b>
  <c>Building a house</c>
</top>

Figure 740. Searching for attribute presence Slide presentation Create comment in forum
//*[@id]
<top>
  <b id="start">
    <a>A house</a>
    <a>Rent a car</a>
    <a>Two houses    </a>
    <a>Five</a>
  </b>
  <c id="build">Building a house</c>
</top>

Figure 741. Searching for attribute value Slide presentation Create comment in forum
//*[@id='build']
<top>
  <b id="first">
    <a>A house</a>
    <a>Rent a car</a>
    <a>Two houses    </a>
    <a>Five</a>
  </b>
  <c id="build">Building a house</c>
</top>

Figure 742. Boolean «or» Slide presentation Create comment in forum
//*[@id or @align='top']
<top>
    <a>
        <b id="5"/>
    </a>  
    <c align='middle'/>
    <e align='top'/>
</top>