9.6 General XPath Expressions


So far we've focused on the very useful subset of XPath expressions called location paths. Location paths identify a set of nodes in an XML document and are used in XSLT match patterns and select expressions. However, location paths are not the only possible type of XPath expression. XPath expressions can also return numbers , Booleans, and strings. For instance, these are all legal XPath expressions:

  • 3.141529

  • 2+2

  • ' Rosalind Franklin '

  • true( )

  • 32.5 < 76.2

  • position( )=last( )

XPath expressions that aren't node-sets can't be used in the match attribute of an xsl:template element. However, they can be used as values for the select attribute of xsl:value-of elements, as well as in the location path predicates.

9.6.1 Numbers

There are no pure integers in XPath. All numbers are 8-byte, IEEE 754 floating-point doubles, even if they don't have an explicit decimal point. This format is identical to Java's double primitive type. In addition to representing floating-point numbers ranging from 4.94065645841246544e-324 to 1.79769313486231570e+308 (positive or negative) and 0, this type includes special representations of positive and negative infinity and a special not a number (NaN) value used as the result of operations like dividing zero by zero.

XPath provides the five basic arithmetic operators that will be familiar to any programmer:

+ Addition
- Subtraction
* Multiplication
div Division
mod Taking the remainder

The more common forward slash couldn't be used for division because it's already used to separate location steps in a location path. Consequently, a new operator had to be chosen , div . The word mod was chosen instead of the more common % operator to calculate the remainder. Aside from these minor differences in syntax, all five operators behave exactly as they do in Java. For instance, 2+2 is 4, 6.5 div 1.5 is 4.33333333, 6.5 mod 1.5 is 0.5, and so on. The element <xsl:value-of select="6*7"/> inserts the string 42 into the output tree when the template is instantiated . More often, a stylesheet performs some simple arithmetic on numbers read from the input document. For instance, this template rule calculates the century in which a person was born:

 <xsl:template match="person">   <century>     <xsl:value-of select="((@born - (@born mod 100)) div 100) + 1)"/>th   </century> </xsl:template> 

9.6.2 Strings

XPath strings are ordered sequences of Unicode characters such as "Fred", "Ethel", " figs/u0644.gif figs/u0627.gif figs/u0647.gif figs/u0631.gif ", or "". String literals may be enclosed in either single or double quotes as convenient . The quotes are not themselves part of the string. The only restriction XPath places on a string literal is that it must not contain the kind of quote that delimits it. That is, if the string contains single quotes, it has to be enclosed in double quotes and vice versa. String literals may contain whitespace including tabs, carriage returns, and line feeds, as well as backslashes and other characters that would be illegal in many programming languages. However, if the XPath expression is part of an XML document, some characters may need to be escaped to satisfy XML's well- formedness rules.

You can use the = and != comparison operators to check whether two strings are the same. You can also use the relational < , > , <= , and >= operators to compare strings, but unless both strings clearly represent numbers (e.g., " -7.5 " or ' 54.2 '), the results are unlikely to make sense. In general, you can't define any real notion of string order in Unicode without detailed knowledge of the language in which the string is written.

Other operations on strings are provided by XPath functions and will be discussed shortly.

9.6.3 Booleans

A Boolean is a value that has exactly two states, true or false. Every Boolean must have one of these binary values. XPath does not provide any Boolean literals. If you use <xsl:value-of select="true"/> in an XSLT stylesheet, then the XSLT processor looks for a child element of the context node named true . However, the XPath functions true() and false( ) can substitute for the missing literals quite easily.

Most of the time, however, Booleans are created by comparisons between other objects, most commonly numbers. XPath provides all the usual relational operators including = , != , < , > , >= , and <= . In addition, the and and or operators can combine Boolean expressions according to the usual rules of logic.

Booleans are most commonly used in predicates of location paths. For example, in the location step person[profession=" physicist "] , profession="physicist " is a Boolean. It is either true or false; there is no other possibility. Booleans are also commonly used in the test attribute of xsl:if and xsl:when elements. For example, this XSLT template rule includes the profession element in the output only if its contents are "physicist" or "computer scientist":

 <xsl:template match="profession">   <xsl:if test=".='computer scientist' or .='physicist'">     <xsl:value-of select="."/>   </xsl:if> </xsl:template> 

This XSLT template rule italicizes the profession element if and only if its content is the string "computer scientist":

 <xsl:template match="profession">   <xsl:choose>     <xsl:when test=".='computer scientist'">       <i><xsl:value-of select="."/></i>     </xsl:when>     <xsl:otherwise>       <xsl:value-of select="."/>     </xsl:otherwise>   </xsl:choose> </xsl:template> 

Finally, there's a not( ) function that reverses the sense of its Boolean argument. For example, if .='computer scientist ' is true, then not(.='computer scientist') is false and vice versa.

XML in a Nutshell
XML in a Nutshell, Third Edition
ISBN: 0596007647
EAN: 2147483647
Year: 2003
Pages: 232

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