Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPATH equivalent of SELECT DISTINCT

Reply
Thread Tools

XPATH equivalent of SELECT DISTINCT

 
 
andrewmrichards
Guest
Posts: n/a
 
      12-02-2010
Hi all

I have a document which looks like this:

<catalog>
<item>
<label>Label 1</label>
<title>Title 1</title>
<artist>Artist 1</artist>
<format>cd</format>
</item>
<item>
<label>Label 1</label>
<title>Title 1</title>
<artist>Artist 1</artist>
<format>download</format>
</item>
<item>
<label>Label 1</label>
<title>Title 2</title>
<artist>Artist 2</artist>
<format>cd</format>
</item>
<item>
<label>Label 2</label>
<title>Title 3</title>
<artist>Artist 3</artist>
<format>cd</format>
</item>
</catalog>

What I'd like to do is use an XSL stylesheet to transform this into:

<catalog>
<label>
<labelName>Label 1</labelName>
<items>
<item>
Title 1 CD
</item>
<item>
Title 1 Download
</item>
Title 2 CD
</item
</items>
</label>
<label>
<labelName>Label 2</labelName>
<items>
<item>
Title 3 CD
</item>
</items>
</label>
</catalog>

I've tried loads of approaches, generally centered around using
position()=1 as a test, or variations of this, and they don't work - I
always end up with everything repeated for EVERY item, and I can't get
a structure where there's only one <label> structure for each label,
no matter how many items they have. I can see why they don't work -
but I can't find an approach which will.

What I want is something akin to <xsl:for-each
select="distint(label)"> in the way that the SQL SELECT DISTINCT
keyword works.

Can anyone point me in the right direction?

Many thanks

Andrew Richards
 
Reply With Quote
 
 
 
 
Piet van Oostrum
Guest
Posts: n/a
 
      12-03-2010
andrewmrichards <(E-Mail Removed)> writes:

> Hi all
>
> I have a document which looks like this:
>
> <catalog>
> <item>
> <label>Label 1</label>
> <title>Title 1</title>
> <artist>Artist 1</artist>
> <format>cd</format>
> </item>
> <item>
> <label>Label 1</label>
> <title>Title 1</title>
> <artist>Artist 1</artist>
> <format>download</format>
> </item>
> <item>
> <label>Label 1</label>
> <title>Title 2</title>
> <artist>Artist 2</artist>
> <format>cd</format>
> </item>
> <item>
> <label>Label 2</label>
> <title>Title 3</title>
> <artist>Artist 3</artist>
> <format>cd</format>
> </item>
> </catalog>
>
> What I'd like to do is use an XSL stylesheet to transform this into:
>
> <catalog>
> <label>
> <labelName>Label 1</labelName>
> <items>
> <item>
> Title 1 CD
> </item>
> <item>
> Title 1 Download
> </item>
> Title 2 CD
> </item
> </items>
> </label>
> <label>
> <labelName>Label 2</labelName>
> <items>
> <item>
> Title 3 CD
> </item>
> </items>
> </label>
> </catalog>
>
> I've tried loads of approaches, generally centered around using
> position()=1 as a test, or variations of this, and they don't work - I
> always end up with everything repeated for EVERY item, and I can't get
> a structure where there's only one <label> structure for each label,
> no matter how many items they have. I can see why they don't work -
> but I can't find an approach which will.
>
> What I want is something akin to <xsl:for-each
> select="distint(label)"> in the way that the SQL SELECT DISTINCT
> keyword works.
>
> Can anyone point me in the right direction?
>

In XSLT/XPath 2.0 there is a distinct-values function. But you probably would use for-each-group to collect the items that belong to the same label. In XSLT/XPath 1.0 you could use the Muench method with a key on the labels.
--
Piet van Oostrum <(E-Mail Removed)>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
Nu Fair Trade woonartikelen op http://www.zylja.com
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-03-2010
andrewmrichards wrote:

> <catalog>
> <item>
> <label>Label 1</label>
> <title>Title 1</title>
> <artist>Artist 1</artist>
> <format>cd</format>
> </item>
> <item>
> <label>Label 1</label>
> <title>Title 1</title>
> <artist>Artist 1</artist>
> <format>download</format>
> </item>
> <item>
> <label>Label 1</label>
> <title>Title 2</title>
> <artist>Artist 2</artist>
> <format>cd</format>
> </item>
> <item>
> <label>Label 2</label>
> <title>Title 3</title>
> <artist>Artist 3</artist>
> <format>cd</format>
> </item>
> </catalog>
>
> What I'd like to do is use an XSL stylesheet to transform this into:
>
> <catalog>
> <label>
> <labelName>Label 1</labelName>
> <items>
> <item>
> Title 1 CD
> </item>
> <item>
> Title 1 Download
> </item>
> Title 2 CD
> </item
> </items>
> </label>
> <label>
> <labelName>Label 2</labelName>
> <items>
> <item>
> Title 3 CD
> </item>
> </items>
> </label>
> </catalog>


With XSLT 2.0 (as implemented by Saxon 9 http://saxon.sourceforge.net/,
AltovaXML Tools http://www.altova.com/altovaxml.html, XQSharp
http://www.xqsharp.com/) you can use for-each-group select="item"
group-by="label" as follows:

<xsl:stylesheet
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:strip-space elements="*"/>
<xslutput method="xml" indent="yes"/>

<xsl:template match="catalog">
<xsl:copy>
<xsl:for-each-group select="item" group-by="label">
<label>
<labelName><xsl:value-of
select="current-grouping-key()"/></labelName>
<items>
<xsl:apply-templates select="current-group()"/>
</items>
</label>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

<xsl:template match="item">
<xsl:copy><xsl:value-of select="title, format"/></xsl:copy>
</xsl:template>

</xsl:stylesheet>

With XSLT 1.0 you can use Muenchian grouping
http://www.jenitennison.com/xslt/grouping/index.xml.

--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
number of distinct elements in a const container and iterating over the distinct elements Hicham Mouline C++ 1 04-11-2010 10:56 AM
subject: xpath select distinct over 2 elements will XML 1 08-15-2007 10:32 AM
Distinct and Top in Select mianiro ASP General 1 01-02-2007 03:38 PM
Select Distinct DataGrid =?Utf-8?B?YmVub2l0?= ASP .Net 4 10-14-2005 11:49 AM
SELECT DISTINCT from two tables Marco Alting ASP General 4 07-31-2003 10:26 AM



Advertisments