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
4.51Mb 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

The Rogue Prince by Margo Maguire
The Sheikh's Prize by Lynne Graham
Cutting Edge by Carolyn Keene
More by Sloan Parker
If I Must Lane by Amy Lane
Too Wicked to Tame by Jordan, Sophie