10.3 XPath Overview

   

Because JSTL XML actions use XPath to access XML data, it helps to have at least a bird's-eye view of XPath. The discussion in this section is by no means a comprehensive examination of XPath, but if you are unfamiliar with XPath, it will get you started. There are many articles on the Web and books that discuss XPath in detail; XSLT Programmer's Reference is one such resource that covers XPath extensively. [3]

[3] See Michael Kay, XSLT Programmer's Reference , Wrox Press Ltd., June 2000.

XPath is a language specifically designed to access information contained in XML documents. XPath grew out of a common need between two W3C initiatives ”XSLT and XPointer ”to access specific locations within XML documents. [4] XPath uses a tree model to represent XML documents; for example, Figure 10-1 shows the XPath tree model for the Rolodex XML file.

[4] You can find the XSLT and XPointer specifications at http://www.w3.org/TR/xslt and http://www.w3.org/TR/xptr, respectively.

Figure 10-1. An XPath Tree Model of the Rolodex XML Document [5]

graphics/10fig01.jpg

[5] The code for the Swing XML Viewer is included with the code for this book; see "The Book's Web Site" on page xiv for more information about that code.

For brevity, Figure 10-1 shows child nodes only for the first contact in the Rolodex XML file and does not show attribute nodes for phone numbers .

XPath expressions operate on the XPath tree model of an XML document. The next section introduces XPath expressions.

XPath Expressions and Types

Unlike other XML- related technologies, such as XSLT, XPath does not use XML syntax; instead, it uses a syntax similar to that used for directory paths. That syntax is familiar to anyone who has used a command prompt for a file system or typed URLs in a browser; for example, the following XPath expression evaluates to a node-set that contains all of the nodes corresponding to the first names of all contacts in the Rolodex XML file:

 /rolodex/contact/firstName 

The preceding expression is known as a location path . Location paths always resolve to a node-set , which is an unordered collection of nodes without duplicates. There are two types of location paths: absolute and relative. Absolute location paths begin with a forward slash, whereas relative location paths do not. Notice that this convention is the same as the convention used for UNIX filesystems and URLs; for example, the path /etc/profile is relative to the filesystem root, whereas etc/profile is relative to the current working directory.

The preceding XPath expression is an absolute location path; here's a relative location path:

 contact/firstName 

The XPath expression shown above is relative to the node at which the expression was applied, known as the context node. If the context node for the preceding expression was /rolodex , that expression would produce the same result as the first XPath expression shown in this section.

Location paths are composed of location steps separated by forward slashes ; for example, the location path /rolodex/contact/firstName is composed of three location steps. Each location step can be qualified with a predicate. A predicate is a boolean expression contained within square brackets at the end of a location step; for example, the following expression specifies a predicate:

 /rolodex/contact[firstName = "Lynn"] 

The preceding expression evaluates to a node-set of all contacts in the Rolodex XML file whose first names are equal to Lynn .

All XPath expressions resolve to one of four XPath types, listed below:

  • boolean ( true or false )

  • number (floating-point)

  • node-set (an unordered collection of nodes without duplicates)

  • string (a sequence of Unicode characters ) [6]

    [6] See "Unicode and Charsets" on page 260 for more information about Unicode.

XPath Type Coercion

XPath performs type coercion as necessary; for example, for the expression "2" = 2 the string is coerced to a number before the comparison is made. One of the most common coercions converts node-set s to one of the other three XPath types ( boolean , number , or string ).

For example, in the XPath expression ”

 /rolodex/contact[1]/lastName = "Keeney" 

”the location path /rolodex/contact[1]/lastName resolves to a node-set containing a single node that represents the last name of the first contact in the Rolodex XML file. That node-set is converted to a string, which is compared to the string Keeney , resulting in a true value for the XML file listed in Listing 10.1 on page 424.

Table 10.2 shows how each of the four XPath types is coerced to a boolean , number , or string .

Table 10.2. XPath Type Coercion

convert to ”>

boolean

number

string

node-set

true if the node-set is not empty; false otherwise

The node-set is first coerced to a string , which is subsequently coerced to a number

The string value of the first node in the node-set

boolean

” ” ”

true is converted to 1 ; false to

true is converted to "true" ; false to "false"

number

is converted to false ; all other numbers are true

” ” ”

All numbers are converted to a string representation of the number

string

nonempty strings are true ; empty strings are false

string representations of numbers are converted to number s; otherwise, they are converted to NaN [a]

” ” ”

[a] NaN , defined by the IEEE 754 Floating-Point Specification, stands for Not a Number.

For each of its types, XPath defines a set of functions. Those functions are the topic of the next section.

XPath Functions

XPath expressions can contain function calls; for example, the following expression returns a number that represents the number of nodes in a node-set :

 count(/rolodex/contact) 

The preceding expression evaluates to a number that represents a count of the number of contacts ” 3 ”contained in the Rolodex XML file. The count function is one of seven node-set functions defined by XPath. Table 10.3 lists the XPath node-set functions.

Table 10.3. Node-set Functions [a]

Function Prototype

Description

number count (node-set)

Returns the number of nodes in a node-set

node-set id (object)

Selects a node with the specified id [b]

number last ()

Returns the size of the current node-set

string local-name (node-set?)

Returns the local part of a node's qualified name

string name (node-set?)

Returns the node's qualified name

string namespace-uri (node-set?)

Returns the namespace URI associated with the specified node

number position ()

Returns the position within the current node-set

[a] ? = optional argument; if omitted, defaults to the context node

[b] IDs are required to be unique

XPath also defines string functions; for example, you can use the string-length function to determine the length of a string, like this:

 string-length(/rolodex/contact[1]/lastName) 

The preceding XPath expression returns the string length of the first contact's last name in the Rolodex XML file listed in Listing 10.1 on page 424. Notice that type coercion takes place in that expression; the location path /rolodex/contact[1]/lastName is converted to a string according to the rules specified in Table 10.2 on page 429.

The XPath string functions are listed in Table 10.4.

XPath also defines a handful of boolean functions; for example, you can use the not function like this:

 not(count(/rolodex/contact) = 0) 
Table 10.4. String Functions [a]

Function Prototype

Description

 string  concat  (string,string,string*) 

Concatenates the specified strings

 boolean  contains  (string,string) 

Returns true if the first string contains the second; false otherwise

 string  normalize-space  (string?) 

Returns a string with leading and trailing whitespace stripped and multiple spaces collapsed into a single space

 boolean  starts-with  (string,string) 

Returns true if the first string starts with the second; false otherwise

  number    string-length    (string?)  

Returns the number of characters in the specified string

 string  substring  (string,number,number?) 

Returns a substring of the specified string, with the first and last numbers representing the start and end, respectively, of the substring; the numbers are 1-based, meaning the position of the first character is 1 , not .

 string  substring-after  (string,string) 

Returns a substring of the first string, starting with the first character after the first occurrence of the second string

 string  substring-before  (string,string) 

Returns a substring of the first string that precedes the first occurrence of the second string

 string  translate  (string,string,string) 

Returns the first string with characters in the second string replaced with characters from the third string

[a] ? = optional; if omitted, defaults to the context node converted to a string. * = one or more.

The previous expression evaluates to true if the number of contacts in the Rolodex is not equal to zero. [7]

[7] That expression is equivalent to count(/rolodex/contact) != 0

The XPath boolean functions are listed in Table 10.5.

Table 10.5. Boolean Functions

Function Prototype

Description

boolean boolean (object)

Converts the specified object to a boolean value; nonempty node-sets , nonzero numbers and strings with a length > evaluate to true

boolean false ()

Returns false

boolean lang (string)

Returns true if the xml:lang attribute of the context node matches the specified string

boolean not (boolean)

Returns true if the boolean argument is false ; otherwise, returns true

boolean true ()

Returns true

Finally, XPath also defines a handful of number functions, listed in Table 10.6.

Table 10.6. Number Functions

Function Prototype

Description

number ceiling (number)

Returns the smallest integer that is not less than the specified number

number floor (number)

Returns the largest integer that is not greater than the specified number

number number (object)

Converts the specified object to a number

number round (number)

Returns an integer that's closest to the specified number; if there are two such numbers, the larger is returned

number sum (node-set)

Returns the sum of each node, converted to a number, in the node-set

Now that we have a basic understanding of XPath, let's see how we can use the JSTL XML actions to manipulate XML documents.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

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