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

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

You can exploit this. I was experimenting recently with the new feature in XML Schema 1.1 to write XPath assertions in a schema, and I wanted to express the rule “if this element has a

child which itself has a

child, then the

in turn must have a

child”. I found I could write this as:

every $r in simpleContent/restriction satisfies $r/simpleType

If there isn't a
simpleContent/restriction
, this assertion is automatically true.

It is always possible to rewrite an
every
expression as a
some
expression, and vice versa. The expression:

every $s in $S satisfies C

is equivalent to:

not(some $s in $S satisfies not(C))

and of course:

some $s in $S satisfies C

is equivalent to:

not(every $s in $S satisfies not(C))

Alternatively, if there is only a single range variable, you can usually rewrite the expression:

some $s in $S satisfies $s/C

as:

exists($S[C])

which some people prefer, as it is more concise. If the sequence
$S
consists of nodes, you can also leave out the call on the
exists()
function; for example, you can rewrite:

if (some $i in //item satisfies $i/price * $i/quantity > 1000) …

as:

if (//item[price*quantity > 1000]) …

The difference is a matter of taste. The
some
expression, however, is more powerful than a simple predicate because (like the
for
expression) it can handle joins, using multiple range variables.

The XPath 2.0 specification describes the semantics of the
some
and
every
expressions in a rather complicated way, using a concept of “tuples of variable bindings”. This happened because the XPath 2.0 specification is generated by subsetting XQuery 1.0, whose core construct, the FLWOR expression, makes use of this concept already. It would have been possible to specify
some
and
every
in a much simpler way for XPath users. In fact, the expression:

Other books

River Road by Suzanne Johnson
Brandy Purdy by The Queen's Rivals
Bitter Sweet by Connie Shelton
Another Chance by Winstone, Rebecca.L.
The Boys of Summer by C.J Duggan
Parzival by Katherine Paterson
Nighthawk Blues by Peter Guralnick
Buckskin Run (Ss) (1981) by L'amour, Louis
Thieves in the Night by Arthur Koestler