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
12.54Mb size Format: txt, pdf, ePub
ads

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:

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

Other books

Deception by Ken McClure
Evil Intent by Robert Olsen
Slap Shot by Rhonda Laurel
FITNESS CONFIDENTIAL by Tortorich, Vinnie, Lorey, Dean
Striker by Michelle Betham
2010. Odisea dos by Arthur C. Clarke
Once Upon a Wicked Night by Jennifer Haymore