Sorting with Muenchian Method

  • Thread starter aleksander.hansen
  • Start date
A

aleksander.hansen

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
 
P

p.lepin

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>
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top