XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Jan VerhoekSubject: Multiple key grouping
Author: Jan Verhoek
Date: 30 May 2007 02:43 PM
I'd like to select distinct values based on 3 columns. The xml-file could look like this:

<products>
<product>
<Code>Muscadet</Code>
<Category>Drinks</Category>
<Group>Wine</Group>
</product>
<product>
<Code>Coca-cola</Code>
<Category>Drinks</Category>
<Group>Refreshment</Group>
</product>
<product>
<Code>Mars</Code>
<Category>Food</Category>
<Group>Refreshment</Group>
</product>
</products>

The result should display all distinct combinations of Category and Group. Now in XSLT 2.0 this is a 1 minute job. But I need this in XSLT 1.0. I've been trying different aproaches but none works. They all have the same result like in:

<xsl:for-each select="/products/product[not((Category=preceding-sibling::product/Category) and (Group = preceding-sibling::product/Group))]">
<xsl:sort select="Category" order="ascending"/>
<xsl:sort select="Group" order="ascending"/>
['<xsl:value-of select="Category"/>', '<xsl:value-of select="Group"/><xsl:text>']</xsl:text>
<xsl:choose>
<xsl:when test="position()!=last()"><xsl:text>,</xsl:text></xsl:when>
</xsl:choose>
</xsl:for-each>

There is a result, but is skippes certain combinations that are there for sure. I've been thinking of a sorting problem, but have not been able to sort it. I tried a <for-each> within a <for-each> with the same results.

Any clue?

Thanks!!!

Postnext
James DurningSubject: Multiple key grouping
Author: James Durning
Date: 30 May 2007 05:04 PM
To be honest, it looks like what you have should work.
Assumption: each product has only one category and one group.

Personally, I prefer the Muenchian method with a concatenated key.
http://www.jenitennison.com/xslt/grouping/muenchian.html
<xsl:key name="categoryGroup" match="product" use="concatenate(Category, Group)"/>
<xsl:for-each select="/products/product[
count(. | key('categoryGroup', concatenate(Category, Group))[1]) = 1]">
....

Could you give an example of a case where your current code does not work?

Postnext
Jan VerhoekSubject: Multiple key grouping
Author: Jan Verhoek
Date: 30 May 2007 06:22 PM

Thanks James! Like the Stylus Studio Team solution using the Key() functionality works!!! Great.
Jan

>To be honest, it looks like
>what you have should work.
>Assumption: each product has
>only one category and one
>group.
>
>Personally, I prefer the
>Muenchian method with a
>concatenated key.
>http://www.jenitennison.com/xs
>lt/grouping/muenchian.html
><xsl:key
>name="categoryGroup"
>match="product"
>use="concatenate(Category,
>Group)"/>
><xsl:for-each
>select="/products/product[
>count(. | key('categoryGroup',
>concatenate(Category,
>Group))[1]) = 1]">
>....
>
>Could you give an example of a
>case where your current code
>does not work?

Postnext
Ivan PedruzziSubject: Multiple key grouping
Author: Ivan Pedruzzi
Date: 30 May 2007 05:34 PM
Hi Jan,

You should use xsl:key; I am not sure I have understood the desired output, it seems like you would like to generate unique pairs of category/group

See if the following helps

Ivan Pedruzzi
Stylus Studio Team
http://www.stylusstudio.com/xml_download.html

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="CategoryGroup" match="products/product" use="concat(Category,Group)"/>
<xsl:template match="/">
<xsl:for-each select="products/product[generate-id(.) = generate-id(key('CategoryGroup',concat(Category,Group))[1])]">
<xsl:value-of select="concat('[', Category,',',Group,']')"/>
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Postnext
Jan VerhoekSubject: Multiple key grouping
Author: Jan Verhoek
Date: 30 May 2007 05:44 PM
Thanks! That's the only method I haven't tried yet.... but reading Michael Kay's book I thought there was not much different in the approach. I will let you know the result as soon as I get back on this issue.
Jan

>Hi Jan,
>
>You should use xsl:key; I am
>not sure I have understood the
>desired output, it seems like
>you would like to generate
>unique pairs of category/group
>
>See if the following helps
>
>Ivan Pedruzzi
>Stylus Studio Team
>http://www.stylusstudio.com/xm
>l_download.html
>
><?xml version="1.0"?>
><xsl:stylesheet
>version="1.0"
>xmlns:xsl="http://www.w3.org/1
>999/XSL/Transform">
><xsl:output
>method="text"/>
><xsl:key
>name="CategoryGroup"
>match="products/product"
>use="concat(Category,Group)"/&
>gt;
><xsl:template match="/">
><xsl:for-each
>select="products/product[gener
>ate-id(.) =
>generate-id(key('CategoryGroup
>',concat(Category,Group))[1])]
>">
><xsl:value-of
>select="concat('[',
>Category,',',Group,']')"/>
><xsl:if test="position() !=
>last()">,</xsl:if>
> </xsl:for-each>
> </xsl:template>
></xsl:stylesheet>
>
>

Posttop
Jan VerhoekSubject: Multiple key grouping
Author: Jan Verhoek
Date: 30 May 2007 06:25 PM
Ivan Thanks!!! This really works! I couldn't wait actually to try it. Thanks again.
Jan


>Thanks! That's the only method
>I haven't tried yet.... but
>reading Michael Kay's book I
>thought there was not much
>different in the approach. I
>will let you know the result
>as soon as I get back on this
>issue.
>Jan
>
>>Hi Jan,
>>
>>You should use xsl:key; I
>am
>>not sure I have understood
>the
>>desired output, it seems
>like
>>you would like to generate
>>unique pairs of
>category/group
>>
>>See if the following helps
>>
>>Ivan Pedruzzi
>>Stylus Studio Team
>>http://www.stylusstudio.co
>m/xm
>>l_download.html
>>
>><?xml
>version="1.0"?>
>><xsl:stylesheet
>>version="1.0"
>>xmlns:xsl="http://www.w3.o
>rg/1
>>999/XSL/Transform">
>><xsl:output
>>method="text"/>
>><xsl:key
>>name="CategoryGroup"
>>match="products/product"
>>use="concat(Category,Group
>)"/&
>>gt;
>><xsl:template
>match="/">
>><xsl:for-each
>>select="products/product[g
>ener
>>ate-id(.) =
>>generate-id(key('CategoryG
>roup
>>',concat(Category,Group))[
>1])]
>>">
>><xsl:value-of
>>select="concat('[',
>>Category,',',Group,']')"/&
>gt;
>><xsl:if
>test="position() !=
>>last()">,</xsl:if>
>;
>> </xsl:for-each>
>> </xsl:template>
>></xsl:stylesheet>
>>
>>

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.