Reputation: 347
I have a problem with my XSLT, the Totals in the summary just wont show up, can anyone help me with this.
I have this XML
<?xml version="1.0" encoding="utf-8"?>
<Document>
<Header>
<Year>2019</Year>
<Month>08</Month>
<DocNumber>1</DocNumber>
<Company>
<Name>My Company</Name>
<City>City</City>
<Country>Country</Country>
</Company>
<Items>
<Item>
<Type>A</Type>
<Name>Item 2</Name>
<Amount>20</Amount>
<Quantity>2</Quantity>
<Discount>3</Discount>
</Item>
<Item>
<Type>B</Type>
<Name>Item 3</Name>
<Amount>40</Amount>
<Quantity>1</Quantity>
<Discount>3</Discount>
</Item>
<Item>
<Type>C</Type>
<Name>Item 1</Name>
<Amount>30</Amount>
<Quantity>2</Quantity>
<Discount>4</Discount>
</Item>
</Items>
</Header>
</Document>
And this XSLT 1.0
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="noDisplay" select="'|B|D|'" />
<xsl:template match="Document">
<Root>
<xsl:apply-templates select="Header" />
</Root>
</xsl:template>
<xsl:template match="Header">
<xsl:variable name="iTotQty" select ="sum(Items/Item/Quantity)" />
<xsl:variable name="iTotAmt" select ="sum(Items/Item/Amount)" />
<xsl:variable name="iTotDisc" select ="sum(Items/Item/Discount)" />
<Summary>
<xsl:attribute name="Year">
<xsl:value-of select ="Year" />
</xsl:attribute>
<xsl:attribute name="Month">
<xsl:value-of select ="Month" />
</xsl:attribute>
<xsl:attribute name="Number">
<xsl:value-of select ="DocNumber" />
</xsl:attribute>
<xsl:attribute name="Number">
<xsl:value-of select ="DocNumber" />
</xsl:attribute>
<xsl:attribute name="Number">
<xsl:value-of select ="DocNumber" />
</xsl:attribute>
<xsl:attribute name="Number">
<xsl:value-of select ="DocNumber" />
</xsl:attribute>
<xsl:attribute name="TotalQty">
<xsl:value-of select ="$iTotQty" />
</xsl:attribute>
<xsl:attribute name="TotalAmt">
<xsl:value-of select ="$iTotAmt" />
</xsl:attribute>
<xsl:attribute name="TotalDisc">
<xsl:value-of select ="$iTotDisc" />
</xsl:attribute>
<xsl:apply-templates select="Items/Item" />
</Summary>
</xsl:template>
<xsl:template match="Items/Item">
<xsl:variable name="bNoDisplay" select ="contains($noDisplay, concat('|', Type, '|'))" />
<Item>
<xsl:attribute name="Type">
<xsl:value-of select ="Type" />
</xsl:attribute>
<xsl:attribute name="Name">
<xsl:value-of select ="Name" />
</xsl:attribute>
<xsl:if test ="not($bNoDisplay)">
<xsl:attribute name="Qty">
<xsl:value-of select ="Quantity" />
</xsl:attribute>
<xsl:attribute name="Amt">
<xsl:value-of select ="Amount" />
</xsl:attribute>
<xsl:attribute name="Disc">
<xsl:value-of select ="Discount" />
</xsl:attribute>
</xsl:if>
</Item>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Which results in this:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Summary Year="2019" Month="08" Number="1" TotalQty="5" TotalAmt="90" TotalDisc="10">
<Item Type="A" Name="Item 2" Qty="2" Amt="20" Disc="3" />
<Item Type="B" Name="Item 3" />
<Item Type="C" Name="Item 1" Qty="2" Amt="30" Disc="4" />
</Summary>
</Root>
The condition for the summary is that it should only sum up all of the Data (Qty, Amount and Discount) that are displayed in the Items, if it is not displayed it should not be part of the sum and if the Data (Qty, Amount and Discount) doesn't contain anything at all or resulted to 0 then should not be displayed
I can only use XSLT 1.0 so can anyone correct my stylesheet, thanks
Upvotes: 0
Views: 128
Reputation: 117165
Consider the following stylesheet, minimized to address the problem presented in your edited question:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="noDisplay" select="'|B|D|'" />
<xsl:template match="/Document">
<Root>
<xsl:apply-templates select="Header" />
</Root>
</xsl:template>
<xsl:template match="Header">
<xsl:variable name="displayed-items" select="Items/Item[not(contains($noDisplay, concat('|', Type, '|')))]" />
<Summary Year="{Year}" Month="{Month}" Number="{DocNumber}" TotalQty="{sum($displayed-items/Quantity)}" TotalAmt="{sum($displayed-items/Amount)}" TotalDisc="{sum($displayed-items/Discount)}">
<!-- omitted -->
</Summary>
</xsl:template>
</xsl:stylesheet>
When applied to your input XML example, the result will be:
Result
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Summary Year="2019" Month="08" Number="1" TotalQty="4" TotalAmt="50" TotalDisc="7"/>
</Root>
Upvotes: 1