Re: [xsl] group-by an optional attribute

Subject: Re: [xsl] group-by an optional attribute
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 22 Nov 2011 17:23:35 +0000
On 22/11/2011 17:12, Mark wrote:
<xsl:for-each-group select="current-group()" group-by="Stamp/@line"

This fails because @line is an optional attribute and does not always
appear in the XML. The only cure I can think of is to run a
pre-processing stylesheet that, should the @line attribute be absent
from a <Stamp> element, adds an @line="0" to each <Stamp> element. This
is not a problem, but I wondered if there were another more elegant
means making the group-by work?

I tried to formulate an alternative, thinking this would group-by the
true value of @line were it present, or by zero in its absence, but it
<xsl:for-each-group select="current-group()" group-by="if(Stamp/@line)
then Stamp/@line else 0">

You don't say how it fails.

what you wrote should be legal but whether it "works" depends on how you want things to group.

I find

<xsl:for-each-group select="current-group()" group-by="string(Stamp/@line)"

usually does what I want in these situations, but whether it does what you want, I can't guess:-)

if you select nothing then things go in no group, but by using string() you never select nothing and things without the attribute end up in the group keyed on the string "". this assumes that there is at most one Stamp child.


google plus: https:/

The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. ________________________________________________________________________

Current Thread