[xsl] How to do... regular 1 to n loops, 0 instead of null, maximum element, call template succinctly, etc.

Subject: [xsl] How to do... regular 1 to n loops, 0 instead of null, maximum element, call template succinctly, etc.
From: Paul Johnston <paul@xxxxxxxxxxxxxxxx>
Date: Tue, 23 Mar 2004 12:15:18 +0000
Hi,

There are a few problems I have found hard to do using XSL that I have some dodgy workarounds for, but I expect some more experienced people will know a better way.

1) How to do regular programming loops, like for i = 1 to 10
For example I needed to do this recently to do a 16x16 grid to display a class C network. I can only see how to loop on XML nodes, so I had to create a nodeset like:
<rows><row/><row/>....<row/></rows>


2) When a node doesn't exist I want to get zero instead of null
A good solution seems to be to use sum(...), but this only works if you pass it a nodeset. Sometimes I'm inside a template that may be passed a nodeset or maybe a number. The simplest way I figured then was number(concat('0', ...)) but this seems less than ideal.


3) How to get maximum element of a list? (or minimum)
Best solution found was something like the following, which seems quite long and complicated:
<xsl:variable name="max">
<xsl:for-each select="nodes">
<xsl:sort order="descending"/>
<xsl:if test="position() = 1"><xsl:value-of select="text()"/></xsl:if>
</xsl:for-each>
</xsl:variable>


4) How to succinctly call templates with parameters?
Is there a neat and short way to call a template? The following seems awfully long-winded:
<xsl:call-template name="show-trend">
<xsl:with-parameter name="curval">10</xsl:with-parameter>
<xsl:with-parameter name="prevval">1</xsl:with-parameter>
</xsl:call-template>
I'd love to be able to do:
<xsl:call-template name="show-trend" curval="10" prevval="1"/>
But that doesn't work :-( Any workarounds?


5) How to sort so nulls come at top not bottom?
Say I have <fred size="10"/><fred size="11"/><fred/>
If you use <xsl:sort select="@size"/> then the fred with no size comes at the end. How can I make it come at the beginning?


Thanks for any help,

Paul

--
Paul Johnston
Internet Security Specialist
Westpoint Limited
Albion Wharf, 19 Albion Street,
Manchester, M1 5LN
England
Tel: +44 (0)161 237 1028
Fax: +44 (0)161 237 1031
email: paul@xxxxxxxxxxxxxxxx
web: www.westpoint.ltd.uk

Current Thread