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]
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.
Figure 10-1. An XPath Tree Model of the Rolodex XML Document [5]
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 TypesUnlike 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:
XPath Type CoercionXPath 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
For each of its types, XPath defines a set of functions. Those functions are the topic of the next section. XPath FunctionsXPath 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]
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]
The previous expression evaluates to true if the number of contacts in the Rolodex is not equal to zero. [7]
The XPath boolean functions are listed in Table 10.5. Table 10.5. Boolean Functions
Finally, XPath also defines a handful of number functions, listed in Table 10.6. Table 10.6. Number Functions
Now that we have a basic understanding of XPath, let's see how we can use the JSTL XML actions to manipulate XML documents. |