Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Sorting with Muenchian Method (http://www.velocityreviews.com/forums/t392435-sorting-with-muenchian-method.html)

aleksander.hansen@gmail.com 11-03-2006 01:08 PM

Sorting with Muenchian Method
 
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:output method="html" version="1.0" encoding="utf-16"
indent="yes"/>
<xsl:key name="kChannel" match="article/field[@name='KANAL']/category"
use="@path" />
<xsl:output 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.lepin@ctncorp.com 11-03-2006 02:06 PM

Re: Sorting with Muenchian Method
 

aleksander.hansen@gmail.com 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:output 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



All times are GMT. The time now is 01:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.