[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: How to sort?

Subject: RE: How to sort?
From: <Jarno.Elovirta@xxxxxxxxx>
Date: Thu, 14 Oct 2004 15:06:16 +0300
xslt sort condition and
Hi,

> What I do now, is choose all Article elements that contain info="main"
> and info="sub" attributes and then sort everything by state="n"
> attribute.

An Article cannot have two info attributes, thus the above condition can never
be true in XML.

> Is it possible to implement an sorting option that enables Article
> elements where filter="food" to be placed on the top of the Article
> list, that is giving them higher priority in sort than the previous
> value of state="n".
>
> so the XML output will look like:
>
> <?xml version="1.0"?>
> <Documents>
> 	<Document title="1" chapter="i" href="file1.xml" filter="food">
> 		<Article title="1.2" info="main" filter="food"/>
> 		<Article title="1.3" info="main" filter="drink"
> state="2"/>
> 	</Document>
>
> 	<Document title="2" chapter="ii" href="file2.xml"
> filter="drink">
>     	               <Article title="2.2" info="main" filter="food"/>
>     	               <Article title="2.1" info="main"
> filter="drink" state="1"/>
> 	</Document>
>
> 	<Document title="4" chapter="2" href="file2.xml" filter="">
> 		<Article title="3.2" info="main" filter="food"/>
> 	</Document>
> </Documents>

  <xsl:template match="Documents">
    <xsl:copy>
      <xsl:apply-templates select="Document[Article/@info = 'main' and
Article/@info = 'sub']"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Document">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:for-each select="Article[@info = 'main']">
        <xsl:sort select="@filter ='food'" data-type="number"
order="descending"/>
        <xsl:sort select="@state" data-type="number"/>
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

I'm not sure exactly how you wanted the info attribute handled, because you
desired output doesn't use Articles with "sub" info. Anyhow, this produces the
desired output. The

  <xsl:sort select="@filter ='food'" data-type="number" order="descending"/>

sort condition basically works so that the "@filter = 'food'" expression will
either return boolean true or false, which will be cast to number 1 or 0,
respectively, and that gives you the sort key value.

Cheers,

Jarno - Lisa Lashes: Hard Mix

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.