Re: [xsl] Cast variable to node-set

Subject: Re: [xsl] Cast variable to node-set
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 15 Mar 2002 18:39:33 +0000
Hi David,

> Ok, here is a sample of what's going on (a bit dumbed down for brevity):
>
> <xsl:variable name="cr_amt"
> select="/Transactions/Transaction/CreditAmount"/>
> <xsl:variable name="cr_under_amt" select="MinAmt/CRUnderMinAmt"/>
>
> <td style="text-align:right;">
>     <xsl:call-template name="displaycurrency">
>         <xsl:with-param name="node" select="$cr_amt + $cr_under_amt"/>
>         <xsl:with-param name="total">1</xsl:with-param>
>     </xsl:call-template>
> </td>
>
> Then inside "displaycurrency":
>
> <xsl:variable name="output">
>     <xsl:value-of select="sum($node)"/>
> </xsl:variable>
>
> The $total xsl:param tells the template to prepend the currency code
> to the value (we only show currency code for totals). The reason for
> the sum() is that I can pass either a single node or a node-set. I
> always want to sum a node-set that is passed to the template. I
> tried this:
>
> select="$cr_amt | $cr_under_amt"
>
> But that didn't work.

Hmm... that's weird. If the call looks like:

  <xsl:call-template name="displaycurrency">
    <xsl:with-param name="node" select="$cr_amt | $cr_under_amt"/>
    <xsl:with-param name="total">1</xsl:with-param>
  </xsl:call-template>

then assuming that $cr_amt and $cr_under_amt are node sets containing
CreditAmount and CRUnderMinAmt elements as you've said, the $node
parameter should be a node set and the sum($node) function should
provide you with the sum of those nodes.

In what way didn't it work? Did you get an error message, or just not
the result that you expected to see?

> Also, there is one additional requirement, when I pass a node into
> the template like this:
>
> <td align="right">
>     <xsl:call-template name="displaycurrency">
>         <xsl:with-param name="node" select="$cr_under_amt"/>
>         <xsl:with-param name="total">1</xsl:with-param>
>     </xsl:call-template>
> </td>
>
> The node has two attributes, @currency_code and @minor_unit (you've
> seen these in my last thread). What are the chances of getting them
> into the template without using seperate params? I have them set as
> xsl:param's now:
>
> <xsl:param name="currency_code" select="$node/@currency_code"/>
> <xsl:param name="minor_unit" select="$node/@minor_unit"/>

You don't need to set those as parameters -- just use normal
variables. You're passing the node itself, so you can do anything to
it you can normally do - get its attributes, children, ancestors,
whatever you need.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread