suppose that the structure of my xml file is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<main>
<_All>
<_999999>
<Employee_Amount>10.0</Employee_Amount>
<Cash_Count>10.0</Cash_Count>
<Dine_in_Amount>10.0</Dine_in_Amount>
<Promos_Actual>10.0</Promos_Actual>
<Combo_Savings_Amount>10.0</Combo_Savings_Amount>
<total_sales>10.0</total_sales>
</_999999>
<_test>
<Employee_Amount>20.0</Employee_Amount>
<Cash_Count>20.0</Cash_Count>
<Dine_in_Amount>20.0</Dine_in_Amount>
<Promos_Actual>20.0</Promos_Actual>
<Combo_Savings_Amount>20.0</Combo_Savings_Amount>
<total_sales>20.0</total_sales>
</_test>
</_All>
</main>
I have to sum all the nodes (for example – Cash_Count of both the nodes,total_sales of both the nodes), the node name can be change so I cannot use its name to navigate. I have tried for-each but i stuck with the xpath expressions. since I am new to xslt 1.0, Please help me for the same.
I have tried with the below xslt with the help of what proskor suggested me in the answers:
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/*">
<table border="1" cellspacing="1" cellpadding="1">
<tr>
<xsl:apply-templates select="/*/*/*" mode="wrap"/>
</tr>
<tr>
<td><xsl:value-of select="name(child::*)"/></td>
<td> </td>
<xsl:for-each select="/*/*/*/*[current()]">
<xsl:variable name="pos" select="position()" />
<td> <xsl:value-of select="sum(/*/*/*/*[$pos])" /></td>
</xsl:for-each>
</tr>
<xsl:apply-templates select="/*/*/*" mode="values"/>
</table>
</xsl:template>
<xsl:template match="/*/*/*" mode="wrap">
<xsl:if test="position() = 1">
<th>Region</th>
<th>Store</th>
<xsl:for-each select="./*">
<th><xsl:value-of select="name()"/></th>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template match="/*/*/*" mode="values">
<xsl:for-each select=".">
<tr>
<td> </td>
<td><xsl:value-of select="name()"/></td>
<xsl:for-each select="./*">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
it worked fine but produses output with extra td with values 0 to it. since there are 6 nodes hence there are 6 extra td with the value 0 to it.
after sum this tr looks like this..
<tr>
<td>30</td>
<td>30</td>
<td>30</td>
<td>30</td>
<td>30</td>
<td>30</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
Please anyone help to figure it out. Thanks
Change the select-attribute of the xsl:for-each element on line 14 to
"/*[1]/*[1]/*[1]/*":