xsl-list
[Top] [All Lists]

Re: [xsl] XSL Variable not getting set

2007-03-14 08:38:12
mIchael wolff wrote:
This works great!:

<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>


I had no idea you could loop that way with XSL. Obviously, I'm a newbie at this and am still learning
a lot.

I'd like to add the following, in case your stylesheet is going to change in the future. You have a lot "if ... equals 'null' " in your code. Consider using a micro pipeline like this:

<xsl:template match="/">
   <xsl:variable name="no-null">
<xsl:apply-templates select="niku:QueryResult/*" mode="remove-null" /> </xsl:variable>
   <xsl:apply-templates select="$no-null/*" />
</xsl:template>

<xsl:template match="* | @*" mode="remove-null">
   <xsl:copy>
       <xsl:apply-templates select="* | @*" mode="#current"/>
   </xsl:copy>
</xsl:template>

<xsl:template match="@*[.='null']" mode="remove-null">
   <xsl:attribute name="{name()}" />
</xsl:template>


Now you can rewrite the part where you use the tests-for-null like this:

<xsl:value-of select="(@fiscalyear, @gl)" separator="," />

the rest of your code can remain the way George suggested.
In general, if you would not care about the comma being there or not, you could also write

<xsl:value-of select="(@fiscalyear[.!='null'], @gl[.!='null'])" separator="," />

but that would remove both the comma and the value in case of the attribute being null. There are many other ways of doing this, but by tackling it in one place (i.e., with the micro pipeline), you will save yourself a lot of duplicate code.

Cheers,
-- Abel Braaksma

--~------------------------------------------------------------------
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>
--~--