Re: [xsl] group-by an optional attribute

Subject: Re: [xsl] group-by an optional attribute
From: "Mark" <mark@xxxxxxxxxxxx>
Date: Tue, 22 Nov 2011 10:43:54 -0700
Hi Martin and David,
You both cleared up the fact that the group-by formulation is not the likely cause of failure, so now I know that it is not the fundamental reason for the failure and I will investigate further.


Thanks,
Mark

-----Original Message----- From: David Carlisle
Sent: Tuesday, November 22, 2011 10:23 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Cc: Mark
Subject: Re: [xsl] group-by an optional attribute


On 22/11/2011 17:12, Mark wrote:
Hi,
Consider:
<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
fails:
<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.

David



--
google plus: https:/profiles.google.com/d.p.carlisle

________________________________________________________________________
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