Using Predicates with XQuery - The position and last functions
(Page 4 of 4 )
The position and last functions are also useful when writing predicates based on position. The position function returns the position of the context item within the context sequence (the current sequence of items being processed). The function takes no arguments and returns an integer representing the position (starting with 1, not 0) of the context item. For example:
doc("catalog.xml")/catalog/product[position() < 3]
returns the first two product children of catalog. You could also select the first two children of each product, with any name, using:
doc("catalog.xml")/catalog/product/*[position() < 3]
by using the wildcard *. Note that the predicate [position() = 3] is equivalent to the predicate [3], so the position function is not very useful in this case.
When using positional predicates, you should be aware that the to keyword does not work as you might expect when used in predicates. If you want the first three products, it may be tempting to use the syntax:
doc("catalog.xml")/catalog/product[1 to 3]
However, this will raise an error* because the predicate evaluates to multiple numbers instead of a single one. You can, however, use the syntax:
doc("catalog.xml")/catalog/product[position() = (1 to 3)]
You can also use the subsequence function to limit the results based on position, as in:
doc("catalog.xml")/catalog/subsequence(product, 1, 3)
The last function returns the number of nodes in the current sequence. It takes no arguments and returns an integer representing the number of items. The last function is useful for testing whether an item is the last one in the sequence. For example, catalog/product[last()] returns the last product child of catalog.
Table 4-7 shows some examples of predicates that use the position of the item. The descriptions assume that there is only one catalog element, which is the case in the catalog.xml example.
Table 4-7. Position in predicates (examples start with doc("catalog.xml")/catalog/)
| Example | Description |
| product[2] | The second product child of catalog |
| product[position() = 2] | The second product child of catalog |
| product[position() > 1] | All product children of catalog after the first one |
| product[last()-1] | The second to last product child of catalog |
| product[last()] | The last product child of catalog |
| *[2] | The second child of catalog , regardless of name |
| product[3]/*[2] | The second child of the third product child of catalog |
In XQuery, it's very unusual to use the position or last functions anywhere except within a predicate. It's not an error, however, as long as the context item is defined. For example, a/last() returns the same number as count(a).
Please check back next week for the conclusion to this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the book XQuery, written by Priscilla Walmsley (O'Reilly, 2007; ISBN: 0596006349). Check it out today at your favorite bookstore. Buy this book now.
|
|