on 06-11-2007 2:22 PM
Hi,
I have a requirement in XSLT mapping where i sum up all the data related to amount field and then want to display total that i have added in the another node
eg.
<Detail>
<xsl:variable name="<b>etotal</b>" select="sum(ns0:pay/ns0:checkList/ns0:check/ns0:deductionList/ns0:adjustment/ns0:amount/ns0:amount)"/>
</Detail>
<Trailer>
<TotalDeductionAmount><xsl:value-of select="<b>$etotal</b>"/>
</TotalDeductionAmount>
</Trailer>
how to do ?
since the above example the scope of the variable is local and cannot be accessed globally. how to declare a global variable and then assign value to the variable
or is there any other method to do this in XSLT
Thanks in advance
With Regards
Pradeep N
Hi,
Please see below sample code is solving your problem. Global variable can not solve the problem, you need to use templates and call then appropriately.
-Kavita
Sample Input
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Test>23</Test>
<Test>34</Test>
<Test>90</Test>
</root>
Sample Output
<?xml version="1.0" encoding="UTF-8"?>
<Mt_test>
<Header>"create the header data according to req"</Header>
<Details>
<DetailsCol>23</DetailsCol>
</Details>
<Details>
<DetailsCol>34</DetailsCol>
</Details>
<Details>
<DetailsCol>90</DetailsCol>
</Details>
<Trailer>
<TotalSum>147</TotalSum>
</Trailer>
</Mt_test>
XLST:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<xsl:element name="Mt_test">
<xsl:element name="Header">"create the header data according to req"</xsl:element>
<xsl:variable name="checkNext" select="//Test[1]"/>
<xsl:call-template name="GenerateTest">
<xsl:with-param name="currNode" select="//Test[1]"></xsl:with-param>
<xsl:with-param name="Sum" select="0"></xsl:with-param>
</xsl:call-template>
</xsl:element>
</xsl:template>
<xsl:template name="GenerateTest">
<xsl:param name="currNode"></xsl:param>
<xsl:param name="Sum"></xsl:param>
<xsl:element name="Details">
<xsl:element name="DetailsCol"><xsl:value-of select="$currNode"></xsl:value-of></xsl:element>
</xsl:element>
<xsl:variable name="Sum1" select="$Sum + $currNode"></xsl:variable>
<xsl:variable name="checkNext" select="$currNode/following-sibling::*[1]"/>
<xsl:choose>
<xsl:when test="$checkNext">
<xsl:call-template name="GenerateTest">
<xsl:with-param name="currNode" select="$checkNext"></xsl:with-param>
<xsl:with-param name="Sum" select="$Sum1"></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="GenerateTrailer">
<xsl:with-param name="currNode" select="chkeckNext"></xsl:with-param>
<xsl:with-param name="Sum" select="$Sum1 "></xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="GenerateTrailer">
<xsl:param name="currNode"></xsl:param>
<xsl:param name="Sum"></xsl:param>
<xsl:element name="Trailer">
<xsl:element name="TotalSum"><xsl:value-of select="$Sum"></xsl:value-of></xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
you can do one thing. Create different templates. In the main template from which you are calling the others, create the variable. Pass this variable to the other templates. In this way you can achive the functionality of Global variable.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Pradeep,
i m not shure what is your problem. If you just want to count fields, you dont need a variable, just function sum with a X-Path expression.
XSL variable are not really variable - their value cant be changed. There is a tricky possibility to make them variable, i described that in the weblog: <a href="/people/udo.martens/blog/2006/04/26/xslt-recursive-templates: Recursive Templates</a>
Regards,
Udo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
80 | |
24 | |
11 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.