I have pasted stripped down versions of my XML and XSLT below.
I have almost got what I need but I can't seem to get a total row for 'Class 2' if the xml is ordered as below.
The first 2 orderline elements both have a Class attribute of 'No Class'. If I put these elements last then the transformation works. However the xml is received in the order below and so needs to work like this.
Teh order of the xml seems to be the problem so I was thinking that perhaps it would be possible to sort the xml and put into a variable then I could transform the contents of the variable. Is it possible to do things that way in xslt
Can anyone help?
XML
<?xml version="1.0" encoding="utf-8" standalone="no"?><!DOCTYPE EmailOrder SYSTEM "http://ift.tt/Xkz2G5"><?xml-stylesheet type='text/xsl' href='BBSORG6.XSL'?><EmailOrders>
<Order Key="COLGRE1-20140914-2345">
<Customer_Msg>Class sort</Customer_Msg>
<OrderLine>
<OrderKey>COLGRE1-20140914-2345</OrderKey>
<ProductKey>1001</ProductKey>
<Qty>1</Qty>
<Free>0</Free>
<Cost>12.99</Cost>
<VAT_rate>0</VAT_rate>
<Details>Friends Character Encyclopedia </Details>
<Class>No Class</Class>
<Bags>0</Bags>
<Section>Funfare</Section>
</OrderLine>
<OrderLine>
<OrderKey>COLGRE1-20140914-2345</OrderKey>
<ProductKey>1002</ProductKey>
<Qty>2</Qty>
<Free>0</Free>
<Cost>19.98</Cost>
<VAT_rate>0</VAT_rate>
<Details>Winnie’s Big Bad Robot</Details>
<Class>No Class</Class>
<Bags>0</Bags>
<Section>Funfare</Section>
</OrderLine>
<OrderLine>
<OrderKey>COLGRE1-20140914-2345</OrderKey>
<ProductKey>1012</ProductKey>
<Qty>1</Qty>
<Free>0</Free>
<Cost>6.50</Cost>
<VAT_rate>0</VAT_rate>
<Details>Snow</Details>
<Class>Class 1</Class>
<Bags>5</Bags>
<Section>Funfare</Section>
</OrderLine>
<OrderLine>
<OrderKey>COLGRE1-20140914-2345</OrderKey>
<ProductKey>1088</ProductKey>
<Qty>2</Qty>
<Free>0</Free>
<Cost>17.98</Cost>
<VAT_rate>0</VAT_rate>
<Details>Great Fairy Bake Off</Details>
<Class>Class 1</Class>
<Bags>5</Bags>
<Section>Funfare</Section>
</OrderLine>
<OrderLine>
<OrderKey>COLGRE1-20140914-2345</OrderKey>
<ProductKey>1123</ProductKey>
<Qty>1</Qty>
<Free>0</Free>
<Cost>3.99</Cost>
<VAT_rate>0</VAT_rate>
<Details>Space</Details>
<Class>Class 1</Class>
<Bags>5</Bags>
<Section>Funfare</Section>
</OrderLine>
<OrderLine>
<OrderKey>COLGRE1-20140914-2345</OrderKey>
<ProductKey>1001</ProductKey>
<Qty>2</Qty>
<Free>0</Free>
<Cost>25.98</Cost>
<VAT_rate>0</VAT_rate>
<Details>Friends Character Encyclopedia </Details>
<Class>Class 2</Class>
<Bags>4</Bags>
<Section>Funfare</Section>
</OrderLine>
<OrderLine>
<OrderKey>COLGRE1-20140914-2345</OrderKey>
<ProductKey>1002</ProductKey>
<Qty>1</Qty>
<Free>0</Free>
<Cost>9.99</Cost>
<VAT_rate>0</VAT_rate>
<Details>Winnie’s Big Bad Robot</Details>
<Class>Class 2</Class>
<Bags>4</Bags>
<Section>Funfare</Section>
</OrderLine>
<OrderLine>
<OrderKey>COLGRE1-20140914-2345</OrderKey>
<ProductKey>1012</ProductKey>
<Qty>1</Qty>
<Free>0</Free>
<Cost>6.50</Cost>
<VAT_rate>0</VAT_rate>
<Details>Snow</Details>
<Class>Class 2</Class>
<Bags>4</Bags>
<Section>Funfare</Section>
</OrderLine>
<OrderLine>
<OrderKey>COLGRE1-20140914-2345</OrderKey>
<ProductKey>1023</ProductKey>
<Qty>10</Qty>
<Free>0</Free>
<Cost>69.90</Cost>
<VAT_rate>0</VAT_rate>
<Details>The Witch with an Itch</Details>
<Class>Class 2</Class>
<Bags>4</Bags>
<Section>Funfare</Section>
</OrderLine>
<OrderLine>
<OrderKey>COLGRE1-20140914-2345</OrderKey>
<ProductKey>1333</ProductKey>
<Qty>2</Qty>
<Free>0</Free>
<Cost>19.98</Cost>
<VAT_rate>0</VAT_rate>
<Details>Scientriffic: Planet Earth</Details>
<Class>Class 2</Class>
<Bags>4</Bags>
<Section>Book Zone</Section>
</OrderLine>
</Order>
</EmailOrders>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://ift.tt/tCZ8VR" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template match="/">
<xsl:apply-templates select="EmailOrders/Order"/>
</xsl:template>
<xsl:template match="Order">
<TABLE WIDTH="65%" BORDER="1" CELLPADDING="4" bgcolor="yellow" align="center">
<TR ALIGN="left">
<TD>Customer message</TD>
<TD>
<xsl:value-of select="Customer_Msg"/>
</TD>
</TR>
</TABLE>
<BR/>
<BR/>
<TABLE WIDTH="100%" BORDER="1" CELLPADDING="4" bgcolor="lightyellow" rules="cols" align="center">
<xsl:choose>
<xsl:when test="Customer_Msg = 'Class sort' ">
<THEAD>
<TH>Class</TH>
<TH>List</TH>
<TH>Paid</TH>
<TH>Free</TH>
<TH>No.</TH>
<TH>Title</TH>
<TH>Price</TH>
</THEAD>
<xsl:for-each select="OrderLine">
<xsl:sort select="Class"/>
<xsl:sort select="ProductKey"/>
<TR ALIGN="left">
<TD>
<xsl:value-of select="Class"/>
</TD>
<TD>
<xsl:value-of select="Section"/>
</TD>
<TD>
<xsl:value-of select="Qty"/>
</TD>
<TD>
<xsl:value-of select="Free"/>
</TD>
<TD>
<xsl:value-of select="ProductKey"/>
</TD>
<TD>
<xsl:value-of select="Details"/>
</TD>
<TD>
<xsl:value-of select="Cost"/>
</TD>
</TR>
<xsl:if test="Class != following::Class[1] or position() = last()">
<xsl:variable name="LastClass">
<xsl:value-of select="Class"/>
</xsl:variable>
<TR ALIGN="left" bgcolor="lightblue">
<TD>
<xsl:value-of select="$LastClass"/> Totals</TD>
<TD><xsl:value-of select="Bags"/> Bags</TD>
<TD>
<xsl:value-of select="sum(/EmailOrders/Order/OrderLine[Class=$LastClass]/Qty)"/>
</TD>
<TD>
<xsl:value-of select="sum(/EmailOrders/Order/OrderLine[Class=$LastClass]/Free)"/>
</TD>
<TD/>
<TD/>
<TD>
<xsl:value-of select="format-number(sum(/EmailOrders/Order/OrderLine[Class=$LastClass]/Cost),'#####.##')"/>
</TD>
</TR>
</xsl:if>
</xsl:for-each>
<TR ALIGN="left" bgcolor="lightseagreen">
<TD>Order totals</TD>
<TD/>
<TD>
<xsl:value-of select="sum(/EmailOrders/Order/OrderLine/Qty)"/>
</TD>
<TD>
<xsl:value-of select="sum(/EmailOrders/Order/OrderLine/Free)"/>
</TD>
<TD/>
<TD/>
<TD>
<xsl:value-of select="format-number(sum(/EmailOrders/Order/OrderLine/Cost),'#####.##')"/>
</TD>
</TR>
</xsl:when>
<xsl:otherwise>
<THEAD>
<TH>List</TH>
<TH>Paid</TH>
<TH>Free</TH>
<TH>Ref</TH>
<TH>Title</TH>
<TH>Total</TH>
</THEAD>
<xsl:for-each select="OrderLine">
<xsl:sort select="ProductKey"/>
<TR ALIGN="left">
<TD>
<xsl:value-of select="Section"/>
</TD>
<TD>
<xsl:value-of select="Qty"/>
</TD>
<TD>
<xsl:value-of select="Free"/>
</TD>
<TD>
<xsl:value-of select="ProductKey"/>
</TD>
<TD>
<xsl:value-of select="Details"/>
</TD>
<TD>
<xsl:value-of select="Cost"/>
</TD>
</TR>
<xsl:if test="Section != following::Section[1] or position() = last()">
<xsl:variable name="LastSection">
<xsl:value-of select="Section"/>
</xsl:variable>
<TR ALIGN="left" bgcolor="lightblue">
<TD>
<xsl:value-of select="Section"/> totals</TD>
<TD>
<xsl:value-of select="sum(/EmailOrders/Order/OrderLine[Section=$LastSection]/Qty)"/>
</TD>
<TD>
<xsl:value-of select="sum(/EmailOrders/Order/OrderLine[Section=$LastSection]/Free)"/>
</TD>
<TD/>
<TD/>
<TD>
<xsl:value-of select="format-number(sum(/EmailOrders/Order/OrderLine[Section=$LastSection]/Cost),'#####.##')"/>
</TD>
</TR>
</xsl:if>
</xsl:for-each>
<TR ALIGN="left" bgcolor="lightseagreen">
<TD>Order totals</TD>
<TD>
<xsl:value-of select="sum(/EmailOrders/Order/OrderLine/Qty)"/>
</TD>
<TD>
<xsl:value-of select="sum(/EmailOrders/Order/OrderLine/Free)"/>
</TD>
<TD/>
<TD/>
<TD>
<xsl:value-of select="format-number(sum(/EmailOrders/Order/OrderLine/Cost),'#####.##')"/>
</TD>
</TR>
</xsl:otherwise>
</xsl:choose>
</TABLE>
<BR/>
<BR/>
</xsl:template>
</xsl:stylesheet>
No comments:
Post a Comment