RE: RE: [xsl] Selecting the first node set

Subject: RE: RE: [xsl] Selecting the first node set
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 15 Aug 2006 14:22:57 -0400

At 11:07 AM 8/15/2006, Charles wrote:
I find it easier to think of things visually (I always did well in geometry but stumbled in algebra). What we're really working with is the notion of sets, so I think of a Venn diagram. I picture this:

"x | y[1]"

as "The set of all x-things and the first y-thing."

Right. In XPath, "|" is the union operator: it takes two node-set operands and returns a node-set unifying them.

Note that since these are the nodes themselves, the union ($n | $n) is the same as $n.

These can lead to some funny stuff in XPath 2.0, where we no longer have node sets but instead, sequences of items. So in XPath 2.0 we can say ($s, $s) and have a sequence of all the items in $s (which may include nodes, or rather, references to nodes) followed by all the items in $s again. (There is no ',' operator in XPath 1.0.)

The union operator in XPath 2.0, also "|", is designed so that (a) it tosses duplicate references to the same nodes, and (b), it sorts the nodes into document order in the resulting sequence, so it effectively works the same as "|" in XPath 1.0 although it's technically a different thing.

and this:

"(x | y)[1]"

as "The first element of the set of all x-things and all y-things."

Right. But note that since sets don't formally have order, an order must be imposed for this to make sense. In XPath 1.0, document order is referred to except in certain special cases (the famous reverse axes).

In XPath 2.0, since sequences do have order, the expression ($s, $s)[1] will get you the first item of the sequence $s, just once, or nothing at all if $s is empty. Hence ($a, $b)[1] is a neat way of providing a default in XPath 2.0, since if $a is empty the first item in $b will be provided.

Is that about right?

Yup. In both XPath 1.0 and 2.0 the predicate (the thing in [ ]) is described as a "filter expression" which takes a single argument: in 1.0, a node set, in 2.0, a sequence. A numeric filter expression takes the nth item in the sequence (or in 1.0, the set, read in document order) for number n.

This is math only in the general sense, since it's no math you're likely to have learned in school. It's really just a formal notation representing data objects and operations over them.


Current Thread