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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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>
>>
>>

   
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.