Web Services, La Trobe, Bendigo

DTD Lecture

Introduction to XPath

Preview: Our Example XML Document

We shall refer to this XML document thoughout today's lecture. You may also download this as weather.xml.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="weather.xsl"?>
<report>
   <date>Wed Aug 14 11:14:30</date>

   <weather-station>
      <location>
         <latitude>-36.82</latitude>
         <longitude>144.30</longitude>
         <place-name>Mandurang</place-name>
      </location>
      <rainfall unit="mm">5.0</rainfall>
      <max-temp>18.1</max-temp>
      <min-temp>6.2</min-temp>
    </weather-station>
  
  <weather-station>
      <location>
         <latitude>-36.77</latitude>
         <longitude>144.28</longitude>
         <place-name>Bendigo</place-name>
      </location>
      <rainfall unit="inches">0.20</rainfall>
      <max-temp>19.1</max-temp>
      <min-temp>7.0</min-temp>
  </weather-station>
   
  <weather-station>
    <location>
       <latitude>unknown</latitude>
       <longitude>unknown</longitude>
       <place-name>Swan Hill</place-name>
    </location>
    <rainfall unit="inches">0.0</rainfall>
    <max-temp>29.1</max-temp>
    <min-temp>7.0</min-temp>
  </weather-station>
</report>

Location Path Shortcuts

Predicates

Boolean Expressions in Predicates

Non-Boolean XPath Expressions

XPath Functions

Node-set Functions Reference

position() Returns a number corresponding to the current node.
last() Returns the number of nodes in the current (context) node-set. This is the same as the position of the last node in ths set.
count() Works a little like last() except it returns the number of nodes in its node-set argument. So count(//weather-station) gives the number of elements named "weather-station" in the document
id() Argument is a string containing one or more IDs, declared in the DTD as being attributes of type ID -- may also be named ID or id. The return value is a node-set of all nodes containing those IDs. The DTD must be present for this to work.
Others:
  • local-name()
  • namespace-uri()
  • name()
Each of these functions takes a node-set and returns (1) the "local part" of the first node, (2) the namespace URI of the first node and (3) the qualified name of the first node.

String Functions Reference

string() Converts its argument to a string. Booleans are converted to "true" or "false", node-sets are converted to the string value of the first node in the set (just like "xsl:value-of"), etc.
starts-with() Returns true if the first string argument "starts with" the second string argument. So "starts-with("Philip", "Phil")" is true.
contains() Returns true if the first string argument "contains" the second string argument. So "starts-with("service", "vice")" is true.
Extracting substrings:
  • substring()
  • substring-left()
  • substring-right
The first of these functions takes a string, and index value and an optional length and returns an extracted substring. The second two take two string and return the substring of the first string either before or after the second string. They're easier to use than to explain. :-)
string-length() Returns the length of its parameter in characters, or the length of the context node if no paramter.
concat() Concatenates multiple strings into a single longer string.
normalize-space() Compacts successive whitespace characters into a single SPACE.
Others:
  • lang()
  • translate()
The first of these returns true if the context node is written in its language code parameter. The second does character-by-character replacement (not language translation). If you need to use this function, read the manual first; it's not obvious.

Number Functions Reference

number() Converts its parameter (or the context node if no parameter) to a number . Makes reasonable assumptions (eg, Boolean true is converted to "1"), and returns NaN if conversion isn't possible.
sum() Takes a node-set argument, converts each of the elements to a number and adds them up, returning the result.
Integer conversions:
  • floor()
  • ceiling()
  • round()
Have their usual (POSIX-like) meanings, and all return the appropriate integer value.
Other functions that are also number functions:
  • position()
  • last()
  • count()
  • string-length()
All defined earlier. These return integer values.

XPointer