xsl-list
[Top] [All Lists]

RE: How to sort?

2004-10-14 05:06:16
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[(_at_)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


<Prev in Thread] Current Thread [Next in Thread>