XPath and node sets

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

Figure 650. 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 651. 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 652. 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 653. 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 654. Simple XPath queries Slide presentation Create comment in forum
Simple XPath queries

Figure 655. 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 656. 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 657. 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 658. 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 659. 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 660. 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 661. 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 662. 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 663. Axes / siblings Slide presentation Create comment in forum
preceeding-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 664. 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 665. 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 666. 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 667. 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 668. 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 669. 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 670. 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 671. 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 672. 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>