4.3 Expressions

An XPath expression allows you to go beyond the basic location of an element or attribute in a document by name, as you have just seen. Expressions let you:

  • Specify location paths using names with either an abbreviated syntax, such as name/family, or unabbreviated syntax, such as child::name/child::family.

  • Use XPath axes such as parent, as in .. in abbreviated syntax, or parent::name in unabbreviated.

  • Perform basic arithmetic such as addition (+), subtraction (-), multiplication (*), division (div), and modulo (mod) using parentheses optionally such as 3 + (5 * 5).

  • Perform Boolean logic using the operators and, or, =, !=, <=, <, >= and > such as 2 &lt; 3 (because expressions occur in attribute values, you must use &lt; instead of <).

  • Reference variables defined elsewhere, such as $var = 3 (= in XPath tests for equivalence, and doesn't perform assignment; Chapter 7 describes variables).

  • Call functions such as current( ), local-name( ), or position( ) (Chapter 5 discusses functions).

  • Perform name and node tests such as rng:* (name test) or text( ) (node test).

When an XPath expression is evaluated, it can return an object of one of four types:


node-set

An unordered collection of zero or more nodes without duplicates.


boolean

A value of either true or false.


number

A floating-point number.


string

A string that is a sequence of legal Unicode characters.

By return, I mean that the XSLT processor hands back a node to the processing stream, in this case, one that has a particular type.

An XSLT processor can also return a type added by the XSLT spec called a result tree fragment. This is a portion of the result tree that may or may not be well-formed XML and is treated like a string. A result tree fragment is not an XPath type but was added to the four XPath types by the XSLT spec.

XPath, by the way, isn't locked into XSLT alone. Beyond XSLT, XPath is also used in other W3C specifications such as the XPointer scheme (see http://www.w3.org/TR/xptr-xpointer/), in XQuery (see http://www.w3.org/TR/xquery), and in XForms (see http://www.w3.org/TR/xforms/). The W3C is also working on integrating XPath with DOM, the Document Object Model (see http://www.w3.org/TR/DOM-Level-3-XPath/).


Expressions occur in certain attribute values in XSLT. These features will be explored later in the chapter, but before moving any further, it's important that you understand what patterns are and how they work.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net