XPath and node sets

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

Figure 687. 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 688. 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 689. 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 690. 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 691. Simple XPath queries Slide presentation Create comment in forum
Simple XPath queries

Figure 692. 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 693. 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 694. 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 695. 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 696. 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 697. 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 698. 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 699. 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 700. 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 701. 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 702. 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 703. 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 704. 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 705. 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 706. 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 707. 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 708. 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 709. 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>