4.2. Abbreviations

Throughout the preceding sections, we have touched upon various abbreviations used for XPath axes, names, and functions. The most common is the abbreviation, by way of omission, for the child:: axis. That abbreviation, of course, is simply an absence. The presence of a node name, such as an element-type name, indicates the child axis by default.

Another fairly “minimalist” abbreviation is that for the self:: axis. This derives directly from UNIX directory syntax and is simply “.” a period by itself.

Not surprisingly, then, the common referent for the parent directory in UNIX is also seen as the abbreviation for the parent:: axis in XPath location expressions: “..” two periods.

The descendant:: axis can be abbreviated with //. The double-forward slashes represent any possible number of intervening nodes between the context node and the node being selected by the expression. Thus, the <month> descendants of the year can be equally referenced, as follows:

year/descendant::month

or

year//month

Another useful abbreviation, is the asterisk, *. When used alone, the * is short for “all element children of the context node.” We could use the following expression to select all elements in the document.

/*

The attribute:: axis can be abbreviated using @:

@period

Abbreviated attributes can also be used with * to indicate all attributes in the context node:

@*

Another very important abbreviation, which bears upon the frequency and importance of the numerical value of the position of any node in the XML data instance, is for the predicate [position() = X], where X is a numerical value or the function last(). This entire predicate can be abbreviated using only the square brackets, [ ], and the number or last(). For example: [2] is the same as [position() = 2]. Similarly, you could have [last()] to select the last node of the context node.

Combining some of these abbreviations, we can select the last <month> in the <season> by using:

season/month[last()]

We can use the element name wildcard to get the last element in the season:

season/*[last()]

We can select any element that has an attribute whose value is winter:

//*[@* = 'winter']

Notice that this expression matches any attribute regardless of the name of the attribute.

Abbreviations for axes, names, and functions are the common way XPath expressions are written. The abbreviations are summarized in Table 4-2.

Table 4-2. Abbreviations for axes and functions
Abbreviation Function
. the self:: axis, or the context node
.. the parent:: axis
* a “wild card” for element and attribute names
@ attribute, or the attribute:: axis
@* a combination of the attribute abbreviation and the “wild card” for name which selects all attributes of the context node
// the descendant:: axis
../the following-sibling:: axis
[2] short-cut for [position() = 2]

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset