XPath

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

Java APIs for XML Kick Start
By Aoyon Chowdhury, Parag Choudhary

Table of Contents
Chapter 6.  XSLT and XPath


As discussed in the beginning of the chapter, XSLT is the language with which an XML document can be transformed into another XML document, HTML, or even plain text.

The XSLT stylesheet has two parts: a pattern that is matched against the node of an XML document, and a template that specifies what is to be done when the pattern is matched. When the pattern is found, the set of XML nodes that match the pattern are selected, and the XSLT templates apply the transformation on the selected XML nodes.

The pattern is specified using the XPath language. The pattern is an expression. An expression is defined in terms of either complete/relative location paths to the nodes or functions, or a combination of both.

An expression is evaluated with respect to a context, where context is the relative location of the node in respect to the document.

After an expression is evaluated, it yields one of the following:

  • An unordered collection of unique nodes, called a node-set

  • A Boolean (true or false) value

  • A floating-point number

  • A string

For example, an expression such as /carparts/* returns a node-set containing all the elements under the carparts element.

XPath Nodes

XPath models the XML document as a tree of nodes. The XPath specification describes seven different kinds of nodes:

  • Root nodes

  • Element nodes

  • Text nodes

  • Attribute nodes

  • Namespace nodes

  • Processing instruction nodes

  • Comment nodes

It is important to remember that in the XPath data model, the data is always normalized. Note that the meaning of normalized in the context of XPath is very different from its meaning in the context of DOM. Although in DOM normalized means the compressing of text nodes, in XPath it implies that all escaped data, or entity references, are dereferenced and appear as actual text. For example, consider the > character. It is represented in an XML document either as an entity reference (>) or through the CDATA section. However, in the XPath text node, it will always be the > character, regardless of whether the > character was defined in the CDATA section or as an entity reference (>) when it was included.

Also, for each of the nodes, you can determine the string-value for that node type. The string-value is either part of the node or a concatenation of string values of descendant nodes. For example, consider the following XML code:

<carparts>     <carengine> engine 1 </carengine>     <carengine> engine 2 </carengine> </carparts> 

Here the string-value for the carparts element node will be " engine 1 engine 2 ".

Root Nodes

The root node is the root of the tree. The root element of the XML document is a child element and is a child node of the root node. The root node also has the processing instruction node, a comment node for the processing instruction and other comments that appear in the prolog as the child nodes.

The string-value of the root node is the concatenation of the string-values of all text node descendants of the root node in document order, which is the order in which the node representations occur in the XML document after the entities are parsed. The element nodes are arranged in the order of their appearance in the XML document. The attribute and namespace nodes for an element always occur before the child nodes of the element, with the namespace node appearing before the attribute node.

Element Nodes

Every element in the XML document is represented by an element node. An element node can have the following as its child elements:

  • Element nodes

  • Comment nodes

  • Processing instruction nodes

  • Text nodes for its content

The string-value of an element node is the concatenation of the string-values of all text node descendants of the element node.

Attribute Nodes

Each element can have an associated set of attribute nodes. The element node is the parent of each of the attribute nodes, but the attribute nodes are not child nodes. The string-value of an attribute node is the normalized value of the attribute.

Namespace Nodes

Each element has an associated set of namespace nodes, one for each of the namespaces appearing in the document. The element node is the parent of these namespace nodes, but the namespace node is not the child of the element node. The string-value of a namespace node is the namespace URI that is bound to the namespace prefix.

Processing Instruction Nodes

A processing instruction is created for each processing instruction that occurs in the document. The only exception is the processing instruction specified in a DTD.

The string-value of a processing instruction node is the part of the processing instruction following the target. This includes all whitespaces and does not include the terminating ?>.

Comment Nodes

A comment node is created for each comment appearing in the document. The only exception is the comments that exist in the DTD. The string-value of the comment node is the content of the comment. This does not include the opening <!-- and the closing -->.

Text Nodes

The character data is grouped into text nodes. The text nodes always contain the text in the normalized form. By design, as much of the character data as possible is put in a text node, with the minimum being one character. A text node cannot have a text node as a child node.

Each character contained in the CDATA section is treated as a character data. For example, the <![CDATA[>]]> entry will appear as > in the text node. Therefore, when writing out XML from text nodes, it is important to remember to escape such characters, by either using entity references or CDATA sections. The string-value of the text node is the character data.

XPath Expressions

XPath expressions are evaluated to return one of the four data types discussed earlier. The expression itself can be a location path, which selects a set of nodes relative to the context node. An expression can also contain functions and wildcards that can be used to filter the set of nodes.

Using Location Paths

XPath models the XML as a tree of nodes. This hierarchical structure is analogous to the hierarchical directory structure that exists on your machines. The addressing mechanism of the XML tree is similar to that of the notation used for accessing a hierarchical directory structure. And, as when you're specifying a path to a directory, both absolute and relative location paths can be used.

Like directory addressing, the following notations hold true:

  • The forward slash (/) is used as the path separator.

  • The absolute path from the root of the document starts with /.

  • .. represents the parent of the current node.

  • . represents the current node.

For example, /carparts/supplier will be the path address to the supplier element under the carparts element. It is important to remember that while XPath provides the addressing mechanism, it is the XSLT specification that uses XPath addressing to match a pattern and select all occurrences for template implementation. For example, while XPath simply specifies the path to the supplier element, XSLT specification will ensure that all supplier elements under the carparts element are selected and returned as a node-set.

Here are some examples of location paths:

  • /carparts matches all top-level carparts entries.

  • supplier matches the supplier element under the current context node.

  • //supplier matches all supplier elements in a document, regardless of their location.

  • /carparts/carbody//supplier matches all supplier elements under the /carparts/carbody context.

An important point to remember is that the names in the XPath specification refer to elements. To access the attributes, you need to use an @ prefix. For example, to select the manufacturer attribute of the carstereo element, you will use //carstereo/@manufacturer.

Filtering XPath Expression Selections

The XPath specification provides functions, wildcards, operators, and index addressing mechanisms with which you can filter the pattern-matching expressions. Here are some examples:

  • * selects all child elements of the context node.

  • @* selects all the attributes of the context node.

  • supplier[1] selects the first supplier child element node under the current context node.

  • //engines/engine[last()] selects the last engine child element node of the engines element node.

  • */engines selects all the engines grandchildren nodes of the current context node.

  • /carparts/supplier[3]/suppliername[1] selects the first suppliername child element node of the third supplier element node under the carparts node.

  • engine[@type="Alpha37"] selects all engine child nodes of the current context node that have the attribute type with value Alpha37.

  • engine[@type="Alpha37"][5] selects the fifth engine child node of the current context node that has the attribute type with value Alpha37.

  • engine[5] [@type="Alpha37"] selects the fifth engine child node of the current context node if that child has a type attribute with value Alpha37.

  • engines[enginename] selects the engines child element node of the current context node that has one or more enginename child element nodes.

  • engines[enginename = "Engine 1"] selects the engines child element node of the current context node that has one or more enginename child element nodes with string-value equal to Engine 1.

  • engine[@id and @type] selects all engine child element nodes of the current context node that have both id and type attributes.

XPath Operators

The XPath specification provides a number of operators with which you can create or modify expressions. These operators are listed in Table 6.1 in their order of precedence.

Table 6.1. XPath Operators

Operators

Operator Function

|

This operator is used to match multiple elements. For example, carengine|supplier selects all carengine and supplier elements.

or, and

These operators return the or/and of two Boolean values.

=, !=

These operators return the equal or not equal for Booleans, strings, and numbers.

<, >, <=, >=

These operators return the less than, greater than, less than or equal to, and greater than or equal to for numbers.

+,-,*, div, mod

These are mathematical operators and return the result of the operation on two numbers.

The operators are all left-associative. For example, 5>4>3 is the same as (5>4)>3, evaluating to false. Also, the expressions can be grouped in parentheses. Note that this operation is not supported in Java. In Java, you cannot use a Boolean result with a relational operator. For example, trying to evaluate (5>4)> 3 will throw an error in a Java program. However, XPath behaves more like C in this case.

XPath Functions

XPath provides a number of functions. These functions can be used to select a collection of nodes, a string, a number, or a Boolean value. The functions are a convenient way of selecting data types.

For example, carengine/text() will select the string-value of the carengine nodes.

The XPath specification divides the functions into four categories:

  • Node-set functions

  • String functions

  • Boolean functions

  • Number functions

Node-Set Functions

The node-set functions select and return a node-set, a number, or a string. The functions are listed in Table 6.2.

Table 6.2. Node-Set Functions

Node-Set Function

Description

last()

This function returns the index of the last element. For example, /carengine[last()] selects the last carengine element.

Position()

This function returns the index position. For example, /carengine[position() = 2] will select the second carengine element of the context node. The context node is the point of reference node from where the XSLT parser counts or looks for the elements.

count(node-set)

This function returns the count of elements. For example, /carengine[count[supplier]] returns the number of supplier element nodes under the carengine node.

id(object)

This function returns the node with the specified ID. For example, id("E129") selects the node with the unique ID E129.

local-name(node-set)

This function returns the local part of the expanded name of the node specified in the argument.

namespace-uri(node-set)

This function returns the namespace URI of the expanded name of the node specified in the argument.

name(node-set)

This function returns the QName of the node specified in the argument.

String Functions

The functions in Table 6.3 work on strings.

Table 6.3. String Functions

String Function

Description

string(object)

This function converts an object to a string. The string returned will be as follows:

  • A string-value is returned for a node-set.

  • If the object is a special Not a Number numeric value, NaN is returned.

  • String "0" is returned for 0.

  • String "Infinity" is returned for positive infinity.

  • String "-Infinity" is returned for negative infinity.

  • Integer values are returned as their number equivalents, but in a string form. For example, -01 will be returned as "-1".

  • Decimal values are returned as their number equivalents in string form. For example, 02.02 will be returned as "-2.02".

  • "false" is returned for Boolean value false.

  • "true" is returned for Boolean value true.

concat(string, string, )

This function returns the concatenation of its arguments.

starts-with(string, string)

This function returns true if the first string starts with the second string. Otherwise, the function returns false.

contains(string, string)

This function returns true if the second string is contained in the first string. Otherwise, the function returns false.

substring-before(string,string)

This function returns the start of first string before the second string occurs in it. For example, substring-before("I am the best", "the") returns "I am ".

substring-after(string,string)

This function returns the remainder of the first string after the second string occurs in it. For example, substring-after("I am the best", "the") returns " best".

substring(string, number, number)

This function returns the substring from the numbered position to the end number. The number of the first character of the string is 1. For example, substring("abcdef", 2, 4) will return "bcd".

string-length(string)

This function returns the number of characters in the string. If there are no arguments specified, the size of the context node's string value is returned.

Normalize-space(string)

This function returns the normalized string-value of the specified string. This implies that leading and the trailing whitespaces are trimmed, and a sequence of whitespace characters is replaced by a single whitespace.

If no string is provided, then the string-value of the context node is normalized.

Translate(string,string,string)

This function converts the first string, replacing the occurrences of the characters in the second string with the corresponding characters from the third string. For example, translate("bar","abc","ABC") returns the string BAr.

Boolean Functions

The functions in Table 6.4 operate on Boolean values.

Table 6.4. Boolean Functions

Boolean Function

Description

boolean(object)

This function converts its arguments to Booleans according to the following rules:

  • A number is true if it is a non-zero number.

  • A node set is true if it is non-empty.

  • A string is true if it is non-empty.

not(Boolean)

This function returns true if its argument is false. Otherwise, it returns true.

true()

This function returns true.

false()

This function returns false.

lang(string)

This function returns true if the language of the context node is the same as (or a sublanguage of) the specified language. The language of the context node is specified by the xml:Lang attribute. For example, lang("en") is true for <para xml:lang="en"/>.

Numeric Functions

The functions in Table 6.5 work on numbers.

Table 6.5. Numeric Functions

Numeric Function

Description

Number(object)

This function converts the object to a number by the following rules:

  • A string that consists of optional whitespace, followed by an optional minus sign, followed by a number, followed by whitespace, is converted to the IEEE 754 number that is nearest (according to the IEEE 754 round-to-nearest rule) to the mathematical value represented by the string; any other string is converted to NaN.

  • True is converted to 1.

  • False is converted to 0.

  • A node-set is converted to a string, and then converted to a number by the IEEE 754 round-to-nearest rule. The conversion of a node-set to a string is done by applying the same rules as followed by the string() function when converting a node-set to a string.

Sum(node-set)

This function returns the sum of the numeric value of each node of the given node-set. The numeric value of each node is arrived at by the same rules as discussed in the previous item.

Floor(number)

This function returns the largest integer that is not greater than the specified number.

Ceiling(number)

This function returns the smallest integer that is greater than the specified number.

Round(number)

This function returns the integer closest to the specified number.


printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
Top

[0672324342/ch06lev1sec6]

 
 


JavaT APIs for XML Kick Start
JAX: Java APIs for XML Kick Start
ISBN: 0672324342
EAN: 2147483647
Year: 2002
Pages: 133

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