Page 1 of 1

Grouping problem...

Posted: Sat Jan 14, 2006 7:19 pm
by undrmoons
The data I'm working with is coming from an unstructured table source - there is nothing to indicate that a row has ended, other than the sequence of elements.

When the <Price> element is followed by any other element other than itself, (like SKU in this example) I'd like to make thta my indicator of the end of a group.

The input XML is similar to this:
---------------------------------
<onepage>
<prodgroup>
<SKU>No. 447139K5</SKU>
<Price>Price 15.99</Price>
</prodgroup>
<prodgroup>
<SKU>456291K5</SKU>
<SKU>456225K5</SKU>
<SKU>456209K5</SKU>
<Price>159.99</Price>
<Price>169.99</Price>
<SKU>484697K5</SKU>
<SKU>456299K5</SKU>
<SKU>574599K5</SKU>
<Price>169.99</Price>
<Price>169.99</Price>
<SKU>484557K5</SKU>
<SKU>484545K5</SKU>
<SKU>484527K5</SKU>
<Price>169.99</Price>
<Price>289.99</Price>
</prodgroup>
<prodgroup>
<SKU>456237K5</SKU>
<Price>169.99</Price>
<Price>169.99</Price>
<Price>169.99</Price>
<Price>189.99</Price>
<SKU>484552K5</SKU>
<Price>169.99</Price>
<Price>169.99</Price>
<Price>169.99</Price>
<Price>309.99</Price>
</prodgroup>
</onepage>
---------------------------------

The ideal result of XML would be:

---------------------------------
<onepage>
<table>
<row>
<SKU>447139K5</SKU>
<Price>15.99</Price>
</row>
</table>
<table>
<row>
<SKU>456291K5</SKU>
<SKU>456225K5</SKU>
<SKU>456209K5</SKU>
<Price>159.99</Price>
<Price>169.99</Price>
</row>
<row>
<SKU>484697K5</SKU>
<SKU>456299K5</SKU>
<SKU>574599K5</SKU>
<Price>169.99</Price>
<Price>169.99</Price>
</row>
<row>
<SKU>484557K5</SKU>
<SKU>484545K5</SKU>
<SKU>484527K5</SKU>
<Price>169.99</Price>
<Price>289.99</Price>
</row>
</table>
<table>
<row>
<SKU>456237K5</SKU>
<Price>169.99</Price>
<Price>169.99</Price>
<Price>169.99</Price>
<Price>189.99</Price>
</row>
<row>
<SKU>484552K5</SKU>
<Price>169.99</Price>
<Price>169.99</Price>
<Price>169.99</Price>
<Price>309.99</Price>
</row>
</table>
</onepage>

---------------------------------

The XSLT that I've currently written doesn't work properly because I'm using group-ending-with="Price".
Since the Price element can sometimes follow itself,
this creates an unwanted break in my result XML.

Any help would be GREATLY appreciated!

Thanks in advance!
-lmm

Posted: Sun Jan 15, 2006 1:13 pm
by george
Hi Imm,

You can group the items based on the starting element, that is a SKU that is not preceded by another SKU element, so we take the first preceding sibling element and test not to be a SKU. Full XSL code below:

Code: Select all


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="onepage">
<onepage>
<xsl:apply-templates/>
</onepage>
</xsl:template>
<xsl:template match="prodgroup">
<table>
<xsl:for-each-group select="*"
group-starting-with="SKU[not(preceding-sibling::*[1][self::SKU])]">
<row>
<xsl:for-each select="current-group()">
<xsl:copy-of select="."/>
</xsl:for-each>
</row>
</xsl:for-each-group>
</table>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George