XPath and node sets

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

Figure 713. 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 714. 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 715. 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 716. 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 717. Simple XPath queries Slide presentation Create comment in forum
Simple XPath queries

Figure 718. 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 719. 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 720. 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 721. 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 722. 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 723. 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 724. 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 725. 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 726. 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 727. 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 728. 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 729. 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 730. 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 731. 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 732. 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 733. 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 734. 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 735. 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>