subject: xpath select distinct over 2 elements

W

will

I have the following xsl which in effect does a distinct select on the
id element
(ie returns list with no repeating items) on subsequent xml..

xml..

<start>
<test>
<id>32354</id>
<rate>2</rate>
</test>
<test>
<id>32354</id>
<rate>2</rate>
</test>
<test>
<id>32354</id>
<rate>4</rate>
</test>
<test>
<id>32356</id>
<rate>4</rate>
</test>
<test>
<id>32357</id>
<rate>2</rate>
</test>
<test>
<id>32357</id>
<rate>2</rate>
</test>
<test>
<id>32358</id>
<rate>5</rate>
</test>
</start>


and xsl..

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:eek:utput method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="//test/id[not(node() =
preceding::node())]">
<xsl:sort select="." order="ascending"/>
<xsl:element name="tk_id">
<xsl:value-of select="concat(node(), ', ', ../rate)"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


This returns a list of unique id

<?xml version="1.0" encoding="UTF-16"?>
<tk_id>32354, 2</tk_id>
<tk_id>32356, 4</tk_id>
<tk_id>32357, 2</tk_id>
<tk_id>32358, 5</tk_id>





This currently seems to be working.. however now i need a distinct
select over 2 elements.
ie selecting unique combinations of id and rate..

The result i would like to get from the above xml would be something
like..

<?xml version="1.0" encoding="UTF-16"?>
<tk_id>32354, 2</tk_id>
<tk_id>32354, 4</tk_id>
<tk_id>32356, 4</tk_id>
<tk_id>32357, 2</tk_id>
<tk_id>32358, 5</tk_id>

as this is the list of unique id and rate combinations..

Any help appreciated..

Thanks
 
P

Pavel Lepin

will said:
<start>
<test>
<id>32354</id>
<rate>2</rate>
</test>
<test>
<id>32354</id>
<rate>2</rate>
</test>
<test>
<id>32354</id>
<rate>4</rate>
</test>
<test>
<id>32356</id>
<rate>4</rate>
</test>
<test>
<id>32357</id>
<rate>2</rate>
</test>
<test>
<id>32357</id>
<rate>2</rate>
</test>
<test>
<id>32358</id>
<rate>5</rate>
</test>
</start>

[transformation snipped]
This currently seems to be working.. however now i need a
distinct select over 2 elements.
ie selecting unique combinations of id and rate..

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="k" match="test"
use="concat(id,', ',rate)"/>
<xsl:key name="dist" match="test"
use="count(.|key('k',concat(id,', ',rate))[1])"/>
<xsl:template match="/">
<result>
<xsl:apply-templates select="key('dist',1)">
<xsl:sort select="concat(id,', ',rate)"/>
</xsl:apply-templates>
</result>
</xsl:template>
<xsl:template match="test">
<xsl:variable name="key"
select="concat(id,', ',rate)"/>
<xsl:copy>
<xsl:attribute name="cnt">
<xsl:value-of select="count(key('k',$key))"/>
</xsl:attribute>
<xsl:value-of select="$key"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top