Creating Primary Expressions

The most basic expression type is the primary expression type. This type can be one of these:

  • A literal

  • A variable

  • A function call

  • A context item

  • A parenthesized expression, which is used to control precedence of operators

  • A comment

To understand these in detail, we'll take a look at them one by one.


A literal is just a value that is of an atomic type. As we saw in Chapter 7, "What's New in XPath 2.0," this means a value of a primitive simple type defined by the XML Schema specification, or a value of a type derived from them by restriction in a schema.

XPath 2.0 has built-in support for two types of literalsnumeric literals and string literals. You can create a string literal simply by enclosing a string of characters in quotes, and numeric literals simply by writing the numeric value. You can see a simple example in ch08_01.xsl (Listing 8.1), which uses a string literal as an XPath expression.

Listing 8.1 An XSLT Example Using an XPath Literal ( ch08_01.xsl )
 <xsl:stylesheet version="2.0" xmlns:xsl=""     xmlns:xs="">     <xsl:template match="/">  <xsl:value-of select="'No worries!'"/>  </xsl:template> </xsl:stylesheet> 

A string literal's value is an xs:string atomic type. You can simply enclose a string of characters in quotes, but be careful about nested quotes. For example, this literal is okay: "This is fine." You can also use single quotes like this: 'This is also fine.' But what if you want to use quotation marks inside a string literal? In that case, you can nest single and double quotes, like this:

 "I said, 'This literal is OK.'" 

You can also use double quotation marks to indicate quoted text. For example, this string literal works just as the previous one did, except that the internal quoted text is delimited by two double quotes:

 "I said, ""This literal is OK.""" 

Doubling single quotation marks works the same way:

 'I said, ''This literal is OK.''' 

By default, numeric literals that don't contain a "." and contain no "e" or "E" (used to specify exponents), are of type xs:integer . For example, here are some integer numeric literals:

 345 1 -9999 

On the other hand, if your numeric value includes a ".", but no "e" or "E", XPath 2.0 assumes the literal is of the xs:decimal data type. Here are a few examples:

 3.14159 -1.000 2.7128 

If you include an "e" or an "E" character, XPath 2.0 assumes your numeric literal is an xs:double type. Here are a few examples:

 3.14159E10 55.00e45 1.9e-5 

Besides string and numeric literals, you can also use Boolean values, representing them with the built-in functions true() and false() .

You can also create literals of any of the XML schema types using their constructors. We saw an example of how that works in Chapter 7, where we created an xs:date literal:

 <xsl:stylesheet version="2.0" xmlns:xsl=""     xmlns:xs="">     <xsl:template match="/">  <xsl:value-of select="xs:date('2004-09-02')"/>  </xsl:template> </xsl:stylesheet> 

Here are a few other examples of creating literals based on XML schema types using the appropriate constructors:

 xs:integer(45) xs:decimal(3.14159) xs:string("No worries!") 


You can also create literals of a desired data type using the cast expression, which we'll see later in this chapter.


Variables are also expressions in XPath 2.0the value of the expression is the value stored in the variable. In XPath 2.0, variables are valid XML names (that is, an XML QName), preceded by a $.

There are two sources of variables in XPath 2.0the host language (such as XSLT), and those variables that you can define in XPath itself. For example, we've seen in Chapter 7 that you can create variables in XSLT using the <xsl:variable> element, and use them in XPath 2.0 like this:

 <xsl:stylesheet version="2.0" xmlns:xsl="">  <xsl:variable name="rightNow" select="current-dateTime()" />  <xsl:template match="/">         The date and time is:  <xsl:value-of select="$rightNow"/>  </xsl:template> </xsl:stylesheet> 

You can also use support for variables in XPath 2.0, as in these for and some expressions:

 for $variable in /planets/planet return $variable/mass some $variable in /planets/planet[1]/name satisfies $variable = "Mars" 

Parenthesized Expressions

When you're using multiple operators, such as multiple arithmetic operators, there can be some confusion about which operator is applied first. For example, what does 1 + 2 * 3 equal3 * 3 or 1 + 6? It turns out that the multiplication operator is evaluated first, so 1 + 2 * 3 = 1 + 6 = 7.


As in various programming languages, variables in XPath 2.0 also have scope , which is the region in which the XPath processor can access them. For example, in the for expression in the preceding code, the variable named $variable is in scope only inside the body of the return clauseoutside that body, you can't use this variable. In the some expression in the preceding code, $variable is in scope in the satisifies clause's body.

On the other hand, if you wanted to add the 1 and 2 before multiplying the result by 3, you could use parentheses to indicate the order of execution you want:

 (1 + 2) * 3 

In XPath 2.0, parenthesized expressions like these are valid. (And don't forget that you can use an empty set of parentheses () to represent an empty sequence.)

Function Calls

Function calls are also considered expressions in XPath 2.0, and their value is simply the value returned by the function. For example, we took a look at the XPath 2.0 built-in max function in Chapter 7:

 <xsl:stylesheet version="2.0" xmlns:xsl="">     <xsl:template match="planets">         <HTML>             <HEAD>                 <TITLE>                     The Largest Planetary Radius                 </TITLE>             </HEAD>             <BODY>                 <H1>                     The Largest Planetary Radius                 </H1>                 <BR/>  The largest planetary radius is   <xsl:value-of select="max(//planet/radius)"/>   miles.  </BODY>         </HTML>     </xsl:template> </xsl:stylesheet> 

Here, the value of the function call is the sequence it returns.

Note that you can pass not only atomic values to functions, but also sequences. For example, this function call passes the values 4, 5, and 6 to a function:

 function(4, 5, 6) 

But this example passes a sequence consisting of 4 and 5, and then a value of 6:

 function((4, 5), 6) 

Context Item Expressions

The context item expression is simply a dot, ".", and as in XPath 1.0, it stands for the current context item. This can be a context node (the node from which evaluation of an expression starts), as in the path expression //planet[count(./name) > 1] .

In XPath 2.0, context items can also be atomic values, as in this expression: (1 to 100)[. mod 10 eq 0] , which returns the sequence (10, 20, 30, 40, 50, 60, 70, 80, 90, 100).

XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: