[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Using the Input Document to Control Generation of Numbers in the Output


Subject: Re: [xsl] Using the Input Document to Control Generation of Numbers in the Output
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Tue, 02 Oct 2007 19:50:16 +0200

Kerry, Richard wrote:
I had wondered about the "(@size, 1)[1]" term, which I'd not seen before
but I think I have now worked out.  If there is no "size" attribute, the
term "@size" returns null and thus does not appear as a first node in
the node-set, ie what's within the (), and thus [1] returns what appears
to be the second element, the '1'.

More precisely, in XSLT 2.0 (the above construct will error in XSLT 1.0) is a _sequence_ of items, of which the first item is a selection of an attribute-node and the second item is an integer. When the attribute-node selection returns nothing, the selection will actually return the empty sequence, and indeed, you guessed correctly, empty sequences are folded when they are part of a sequence:


if @size is not there (@size, 1) becomes ((), 1) which then becomes (1).
if @size is there, (@size, 1) will beomce (@size, 1), i.e., it does not change, @size holds the selected attribute-node.


This shortcut is often used, but when you start out with XSLT 2.0, you may prefer the more verbose form:

if (@size) then @size else 1

which is semantically equivalent with (@size, 1)[1].


Cheers, -- Abel --


Current Thread
Keywords