Sorting with Muenchian Method

Discussion in 'XML' started by aleksander.hansen@gmail.com, Nov 3, 2006.

  1. Guest

    Hello, I have xml data that I need to group and sort. I have tried
    grouping it using the Muenchian Method. Probably not solved the best
    way, but it works. But I can't get the sorting right.

    Here's my XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="channel.xslt"?>
    <SearchResults>
    <SearchHit>
    <article>
    <field name="KANAL">
    <category path="//TV//Channels//Channel1" id="1"/>
    </field>
    <field name="TITTEL">Program 1</field>
    <field name="SCHEDULE">
    <schedule startTime="2400"/></field>
    </article>
    <article>
    <field name="KANAL">
    <category path="//TV//Channels//Channel2" id="2"/>
    </field>
    <field name="TITTEL">Program 2</field>
    <field name="SCHEDULE">
    <schedule startTime="0900"/></field>
    </article>
    <article>
    <field name="KANAL">
    <category path="//TV//Channels//Channel2" id="2"/>
    </field>
    <field name="TITTEL">Program 3</field>
    <field name="SCHEDULE">
    <schedule startTime="1100"/></field>
    </article>
    <article>
    <field name="KANAL">
    <category path="//TV//Channels//Channel2" id="2"/>
    </field>
    <field name="TITTEL">Program 4</field>
    <field name="SCHEDULE">
    <schedule startTime="1000"/></field>
    </article>
    <article>
    <field name="KANAL">
    <category path="//TV//Channels//Channel1" id="1"/>
    </field>
    <field name="TITTEL">Program 5</field>
    <field name="SCHEDULE">
    <schedule startTime="2100"></schedule></field>
    </article>
    </SearchHit>
    </SearchResults>

    Here's my XSLT:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="html" version="1.0" encoding="utf-16"
    indent="yes"/>
    <xsl:key name="kChannel" match="article/field[@name='KANAL']/category"
    use="@path" />
    <xsl:eek:utput method="html"/>
    <xsl:template match="SearchResults/SearchHit">
    <xsl:for-each
    select="article/field[@name='KANAL']/category[generate-id()=generate-id(key('kChannel',
    @path))]">
    <xsl:variable name="theArticle"
    select="current()/parent::node()/parent::node()" />
    <xsl:variable name="ChannelName" select="@path" />
    <xsl:value-of select="$ChannelName" /><br/>
    <xsl:apply-templates
    select="/SearchResults/SearchHit/article/field/category[@path =
    current()/@path]" />
    </xsl:for-each>
    </xsl:template>

    <xsl:template match="category">
    <xsl:variable name="theArticle"
    select="current()/parent::node()/parent::node()" />
    <xsl:value-of select="$theArticle/field[@name='TITTEL']"/> -
    <xsl:value-of
    select="$theArticle/field[@name='SCHEDULE']/schedule/@startTime"/><br/>
    </xsl:template>
    </xsl:stylesheet>

    Here's my current output:

    //TV//Channels//Channel1
    Program 1- 2400
    Program 5- 2100
    //TV//Channels//Channel2
    Program 2- 0900
    Program 3- 1100
    Program 4- 1000

    I would like to sort the channels by its ID and the programs by its
    startTime. Does anybody have an idea?

    Any help would be appreciated!

    Regards,
    A Hansen, XSLT newbie
     
    , Nov 3, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hello, I have xml data that I need to group and sort. I
    > have tried grouping it using the Muenchian Method.
    > Probably not solved the best way, but it works. But I
    > can't get the sorting right.


    Funny, I don't see any <xsl:sort>s in your transformation.
    Precisely how were you trying to get the sorting right?

    [XML]

    I wonder who designed that...

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml"/>
    <xsl:key name="k" match="article"
    use="field[@name='KANAL']/category/@path"/>
    <xsl:template match="/">
    <result>
    <xsl:apply-templates
    select=
    "
    //article
    [
    generate-id()=
    generate-id
    (
    key('k',field[@name='KANAL']/category/@path)
    )
    ]
    " mode="channel">
    <xsl:sort
    select="field[@name='KANAL']/category/@id"/>
    </xsl:apply-templates>
    </result>
    </xsl:template>
    <xsl:template match="article" mode="channel">
    <channel>
    <xsl:attribute name="path">
    <xsl:value-of
    select="field[@name='KANAL']/category/@path"/>
    </xsl:attribute>
    <xsl:apply-templates
    select=
    "
    //article
    [
    field[@name='KANAL']/category/@id=
    current()/field[@name='KANAL']/category/@id
    ]
    " mode="program">
    <xsl:sort
    select=
    "
    field[@name='SCHEDULE']/schedule/@startTime
    "/>
    </xsl:apply-templates>
    </channel>
    </xsl:template>
    <xsl:template match="article" mode="program">
    <program>
    <xsl:value-of select="field[@name='TITTEL']"/>
    <xsl:text> - </xsl:text>
    <xsl:value-of
    select=
    "
    field[@name='SCHEDULE']/schedule/@startTime
    "/>
    </program>
    </xsl:template>
    </xsl:stylesheet>

    --
    Pavel Lepin
     
    , Nov 3, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Mackenzie
    Replies:
    0
    Views:
    1,125
    Colin Mackenzie
    Jul 3, 2003
  2. Marrow
    Replies:
    2
    Views:
    1,269
    Marrow
    Jul 7, 2003
  3. Tristan Miller

    XSL grouping (Muenchian)

    Tristan Miller, Jan 31, 2005, in forum: XML
    Replies:
    2
    Views:
    569
    Philippe Poulard
    Jan 31, 2005
  4. reclusive monkey
    Replies:
    2
    Views:
    1,449
    reclusive monkey
    Aug 10, 2005
  5. Muenchian problem

    , May 9, 2006, in forum: XML
    Replies:
    1
    Views:
    438
    George Bina
    May 10, 2006
Loading...

Share This Page