XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (537 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
8.38Mb size Format: txt, pdf, ePub

. What is happening is that because
ch
and
ll
are being treated as single characters for collation purposes, they are also treated as single characters for the purpose of substring matching.

These rules for substring matching using a collation apply not only to the
contains()
function, but also to
ends-with()
,
starts-with()
,
substring-before()
, and
substring-after()
.

Examples

These examples assume that the default collation is the Unicode codepoint collation, which compares strings codepoint by codepoint.

Expression
Result
contains(“Shakespeare”, “spear”)
true
contains(“”, “a”)
false
contains(“Shakespeare”, “”)
true
contains(“”, “”)
true
contains((), “a”)
false

Usage

The
contains()
function is useful mainly for very simple matching; for example, testing whether a string contains a space. For more complex matching of strings, the
matches()
function is available in XPath 2.0 with full support for regular expressions.

See Also

ends-with()
on page 773

matches()
on page 828

starts-with()
on page 875

substring()
on page 883

substring-after()
on page 885

substring-before()
on page 887

count

The
count()
function takes a sequence as its argument, and returns the number of items in the sequence. For example, the expression
count((4,5,6))
returns 3.

Changes in 2.0

The function is generalized in XPath 2.0 so that it can return the number of items in any sequence.

Signature

Argument
Type
Meaning
sequence
item()*
The sequence whose items are to be counted
Result
xs:integer
The number of items in the supplied sequence

Effect

The
count()
function takes any sequence as its argument, and returns the number of items present in the sequence.

If the sequence contains nodes, each node counts as one item. The function does not count the number of atomic values contained in the node's typed value, and it does not count the children or descendants of the node.

Examples

Consider the source document:


  

  


and assume that this has been validated using a schema that defines the
colors
attribute as a sequence of strings.

Expression
Result
count(//obs)
2
count(//obs/@colors)
2
count(data(//obs/@colors))
5
count(//@*)
4
count(//obs/@date)
0
count((5 to 10))
6

Usage

Avoid using
count()
to test whether a sequence of nodes is empty; for example, by writing:

if (count(book[author=‘Hemingway’]) != 0) then …

This can be better expressed as:

if (book[author=‘Hemingway’]) then …

or, if you prefer:

if (exists(book[author=‘Hemingway’])) then …

A good processor will optimize the first expression so as to avoid counting all the books (it can stop counting books and take the
then
path as soon as it finds the first one that matches), but it's always best to avoid relying on such optimizations if you can.

The
count()
function is a useful way of finding the position of a node within a source document. In XSLT it can provide an effective alternative to using

, and in non-XSLT environments, it may be the only way of doing numbering. For example, if the context node is a

element, then
count(preceding-sibling::bullet)+1
returns the number of this

within the sequence of

elements. The advantages of using
count()
over

, apart from the fact that it's available in non-XSLT environments, are that it is rather more flexible in defining what you want to count, and it can be used directly in expressions. However,

gives a simple way of obtaining the sequence number, formatting it, and inserting it in the result tree in a single operation; it may also in some cases be easier for the processor to optimize.

Avoid using
count()
where
last()
would do the job just as well. This situation arises in XSLT when you are processing a sequence of nodes using

or

; the number of nodes in that sequence is then available from the
last()
function. For example, it is probably inefficient to write:


   Book  of

             

   

   …


because—unless the XSLT processor is rather clever—it will have to reevaluate the expression
../book[author=‘Hemingway’]
each time round the loop.

Other books

Honor Found (The Spare Heir) by Southwick, Michael
Brick (Double Dippin') by Hobbs, Allison
All That He Wants by Olivia Thorne
A Taste for Scandal by Erin Knightley
The Last Dance by Angelica Chase
For Your Sake by Elayne Disano
Stretching Anatomy-2nd Edition by Arnold Nelson, Jouko Kokkonen