Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 6th, 2006, 10:52 AM
jaa jaa is offline
Registered User
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT Select Distinct


Hello

I'm trying to obtain the distinct values given a combination of nodes (Company and Location). I have created a XSLT (see below) that uses for-each-group, however I always obtain two instances of the first combination in the results.

This is the the sample XML:

<ns0:ClientList xmlns:ns0="http://Test">
    <DataList>
        <Company>60</Company>
        <Location>02</Location>
    </DataList>
    <DataList>
        <Company>60</Company>
        <Location>01</Location>
    </DataList>
    <DataList>
        <Company>60</Company>
        <Location>03</Location>
    </DataList>
</ns0:ClientList>


I'm using the XSLT below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>


    <xsl:template match="/">

<xsl:for-each-group select="//DataList" group-by="Company|Location" >
            <xsl:element name="Company">
                <xsl:value-of select="Company|Location"/>
            </xsl:element>
            <xsl:element name="Location">
                <xsl:value-of select="Location"/>
            </xsl:element>
</xsl:for-each-group>

    </xsl:template>
</xsl:stylesheet>



These are the result I'm getting:

<?xml version="1.0" encoding="UTF-8"?>
<Company>60 02</Company>
<Location>02</Location>
<Company>60 02</Company>
<Location>02</Location>
<Company>60 01</Company>
<Location>01</Location>
<Company>60 03</Company>
<Location>03</Location>
 
Old April 6th, 2006, 11:10 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

When group-by selects a sequence of several items, then each value in the sequence is considered to form a grouping key in its own right, and the relevant node ends up in more than one group. The use case for this is grouping books by author - if a book has several authors, it goes in each of the author groups.

To group on a composite key, you have a choice. You can form the grouping key as a concatenation: group-by="concat(Company, '|', Location)". Or you can use two nested groupings:

<xsl:for-each-group ... group-by="Company">
  <xsl:for-each-group ... group-by="Location">

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Distinct kirkmc Access 13 July 13th, 2006 01:26 PM
Select Distinct kirkmc Excel VBA 3 May 5th, 2006 07:55 PM
Select Distinct? [email protected] SQL Language 5 November 5th, 2005 09:58 AM
Distinct SELECT DISTINCT question... EndEffect Classic ASP Databases 4 August 18th, 2005 08:53 AM
select distinct bmains ADO.NET 0 April 8th, 2004 02:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.