xsl-list
[Top] [All Lists]

Re: [xsl] XSL Variable not getting set

2007-03-14 08:55:41
George Cristian Bina wrote:

  <xsl:template match="niku:QueryResult/niku:Records">
    <xsl:for-each-group select="niku:Record"
      group-by="concat(@fiscalyear,@gl)">
      <xsl:variable name="cg" select="current-group()"/>
        <xsl:if test="@fiscalyear != 'null'">
          <xsl:value-of select="@fiscalyear"/>
          <xsl:text>,</xsl:text>
          <xsl:value-of select="@gl"/>
          <xsl:text>,</xsl:text>
          <xsl:for-each select="1 to 12">
            <xsl:variable name="p" select="."/>
<xsl:value-of select="if ($cg[(_at_)month=$p]) then $cg[(_at_)month=$p]/@cost else 0"/>
            <xsl:if test="position()!=last()">,</xsl:if>
          </xsl:for-each>
</xsl:if> </xsl:for-each-group>
  </xsl:template>
</xsl:stylesheet>

Note that the xsl:if is placed differently from the OP, outputting no line at all when @fiscalyear = null, whereas the original output an empty string instead (leaving the comma).

Because all we are dealing with are sequences, you can downsize the example of George even further, removing any addition if-statements altogether. Here's a go:

<xsl:template match="niku:Records">
   <xsl:for-each-group select="niku:Record"
       group-by="concat(@fiscalyear,@gl)">

       <xsl:value-of select="
           (@fiscalyear[.!='null'], '')[1],
           (@gl[.!='null'], '')[1],
           for $i in 1 to 12
           return (current-group()[(_at_)month=$i]/@cost, 0)[1]"
           separator="," />

   </xsl:for-each-group>
</xsl:template>


If you don't like the constructs of the type (@fiscalyear[.!='null'], '')[1] you can replace them with the if-equivalents, of course. Also, it will add to readability with the aforementioned micro pipeline, which will make the same template look like this:

<xsl:template match="niku:Records">
   <xsl:for-each-group select="niku:Record"
       group-by="concat(@fiscalyear,@gl)">

       <xsl:value-of select="
           (@fiscalyear, @gl),
           for $i in 1 to 12
           return (current-group()[(_at_)month=$i]/@cost, 0)[1]"
           separator="," />

   </xsl:for-each-group>
</xsl:template>


It's almost as with Perl: there's more than one way to do it ;)

Cheers,
-- Abel Braaksma
  http://www.nuntia.nl


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>