Introduction | XML Data Setup | XSL and XSLT | XPath | Conclusion

XPath - Practically a Query Language

The XML TreeXPath, as its name implies, is a syntax for defining the parts of an XML document. Just like the "path" of a document on your computer, an element of an XML database can have its place in the hierarchy defined using a combination of slashes and dots.

But XPath is a lot more than that. Using math, Boolean expressions ("and," "or" etc) and string-handling functions, XPath becomes a way to pull complex results from piles of data.

Basic terminology
Anyone who does geneology for a hobby will have no problem recognizing some of the basic XPAth terminology:

  • Self::
  • Parent::
  • Grandparent::
  • Child::
  • Grandchild::
  • Sibling::
  • Ancestors:: (all parents, grandparents, etc.)
  • Descendants:: (all children, grandchildren, etc.)

Abbreviated Terms
In addition, there are the following designations as well. NOTE: Node means something similar to "folder" in file management terminology. If the meanings below make no sense, try substituting the word "folder" for "node" and see if that helps. Similarly, try substituting the word "things" in place of the word "elements."

Abbreviation Meaning Comment
. Current node Same as self::
/ Root node Absolute references start with /
// All nodes Inclusive reference: Selects all elements in the document that fulfill the criteria (even if they are at different levels in the XML tree)!
.. One node closer to the root than the current node. Same as parent::
(none) One node further from the root than the current node Same as child::
@ Attribute Attributes of selected node. (Sometimes the XML tag will have an attribute in it, like <Teacher employee_number="80039"> instead of creating a separate child node for it.
* Wildcard Select all elements of designated node, including the node and its descendants
//* Inclusive Wildcard Selects everything in the database
axis, plural axes relationship of current node to selected node Where are we? Where are we going?
[] further specs square brackets further specify which elements of a node are to be selected, for example, [3] (third child), [last()] (last child), or [price<14.99]

Sorting and Sifting
Xpath does a lot more than just locate elements, however. Once it finds them, it can compare and sort them, performing all sorts of mathematical and logical functions to help you sift through haystacks to find just the golden straws.

Numerical Expressions

Operator Description Example Result
+ Addition 7 + 4 11
- Subtraction 7 - 4 3
* Multiplication

8 * 5

40
div Division 12 div 4 3
mod Modulus (remainder after division) 7 mod 2 1

Comparisons

Operator Description Example Result
= Like (equal) price=9.80 true (if price is 9.80)
!= Not like (not equal) price!=9.80 false

Relational expressions

Operator Description Example Result(if price is 12.95)
< Less than price<12.95 false
<= Less than or equal to price<=12.95 true
> Greater than price>12.95 false
>= Greater than or equal to price>=12.95 true

Boolean expressions

Operator Description Example Result (if price is 12.95)
or value satisfies either one condition or the other price=12.95 or price=12.00 true (if price is 12.95)
and value satisfies both one condition and another one price<=12.95 and price=12.00 false

Functions
In addition to all of these operators, XPath contains string-handling, number, and boolean functions, like contains(), starts-with(), ceil(), sum(), not() or true(), which turn XPath into a fully-functional query language for your sticks-and-stones database.

==>Next - Conclusion