Re: [xsl] retrieve elements with same attribute values

Subject: Re: [xsl] retrieve elements with same attribute values
From: George Cristian Bina <george@xxxxxxxxxxxxx>
Date: Tue, 08 May 2007 10:14:56 +0300
Hi Charles,

Something like below would do it. That defines a key that matches all the items with the same author. Then iterates the authors and if there are more than 2 items matched then outputs them.

<?xml version='1.0'?>
<xsl:transform version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

    <xsl:output method="text" indent="no"/>
    <xsl:key name="itemsByAuthor" match="item" use="author"/>

    <xsl:template match="/">
        <xsl:for-each select="//author[not(.=preceding::author)]">
            <xsl:if test="count(key('itemsByAuthor', .))>2">
                <xsl:for-each select="key('itemsByAuthor', .)">
                    <xsl:value-of select="../@name"/>
                    <xsl:text> : </xsl:text>
                    <xsl:value-of select="normalize-space(.)"/>
                    <xsl:text> : </xsl:text>
                    <xsl:value-of select="@name"/>
                    <xsl:text>&#10;</xsl:text>
                </xsl:for-each>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:transform>

Regards,
George
---------------------------------------------------------------------
George Cristian Bina - http://aboutxml.blogspot.com/
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


Charles Ohana wrote:
Hello friends,
I'm trying to retrieve all element that have the same values for specific attributes more (or equal) than 3 times (for example).
See example below that explains what I'm trying to achieve.
Any help would be appreciated.
Thanx


I trying to get the output like

70 : Queen : we are the champions
70 : Queen : Bohemian Rhapsody
70 : Queen : radio gaga
reggae : Bob Marley : No woman no cry
reggae : Bob Marley : one love
reggae : Bob Marley : Jammin

From this xml

<catalogue> <category name="70's"> <item name="we are the champions"> <author>Queen</author> </item> <item name="Roxane"> <author>Police</author> </item> <item name="Bohemian Rhapsody"> <author>Queen</author> </item> <item name="radio gaga"> <author>Queen</author> </item> </category>

  <category name="reggae">
     <item name="red red wine">
        <author>UB40</author>
     </item>
     <item name="kingston town">
        <author>UB40</author>
     </item>
     <item name="No woman no cry">
        <author>Bob Marley</author>
     </item>
     <item name="one love">
        <author>Bob Marley</author>
     </item>
     <item name="Jammin">
        <author>Bob Marley</author>
     </item>
  </category>
</catalogue>

Current Thread