Sorting results by 2 different priorities

Discussion in 'XML' started by jose.jeria@gmail.com, Jul 14, 2005.

  1. Guest

    XML file:
    http://www.jeria.net/XSLT/xml/airports_new.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <airports>
    <airport>
    <Input_String>BER</Input_String>
    <string_priority>39</string_priority>
    <code>BER</code>
    <City_Interface>Berlin</City_Interface>
    <Airport>(Alle Flughäfen)</Airport>
    <country_int>Deutschland</country_int>
    <Display_Type>2</Display_Type>
    <Language>de</Language>
    </airport>
    <airport>
    <Input_String>BUH</Input_String>
    <string_priority>26</string_priority>
    <code>BUH</code>
    <City_Interface>Bukarest</City_Interface>
    <Airport>(Alle Flughäfen)</Airport>
    <country_int>Rumänien</country_int>
    <Display_Type>2</Display_Type>
    <Language>de</Language>
    </airport>
    <!-- and so on, up to 8000 entries -->
    </airports>

    XSL file to search (works fine) the element "Input_String":
    http://www.jeria.net/XSLT/xml/airports.xsl

    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput
    media-type="text/xml"
    method="html"
    encoding="UTF-8"/>

    <xsl:param name="queryString"/>
    <xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    <xsl:variable name="lowerCase" select="'abcdefghijklmnopqrstuvwxyz'"/>
    <xsl:variable name="query" select="translate($queryString, $upperCase,
    $lowerCase)"/>

    <xsl:template match="/">
    <html>
    <head>
    <title>Search results</title>
    </head>

    <body>
    <xsl:apply-templates select="airports/airport">
    <xsl:sort data-type="number" select="string_priority"
    order="descending"/>
    </xsl:apply-templates>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="airport">
    <xsl:if test="Input_String[starts-with(translate(., $upperCase,
    $lowerCase),$query)]">
    <p>
    <strong>Input String:</strong> <xsl:value-of
    select="Input_String"/><br/>
    <strong>Priority:</strong> <xsl:value-of
    select="string_priority"/><br/>
    <strong>Airport:</strong> <xsl:value-of select="Airport"/><br/>
    <strong>Country:</strong> <xsl:value-of select="country_int"/><br/>
    <strong>City:</strong> <xsl:value-of select="City_Interface"/><br/>
    <strong>Code:</strong> <xsl:value-of select="code"/><br/>
    </p>
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>

    The result can be seen here:
    http://www.jeria.net/XSLT/

    When I search for example for "ala" (can be tested live on the link
    above), the results are sorted by the "string_priority".
    What I would like to do is to show the direct matches on top. This
    would be the highest priority. So when i search for "ala", it should be
    the nr one hit, not the second one like in this case.
    How can I solved this? By having a xsl:choose in the airport template?
    , Jul 14, 2005
    #1
    1. Advertising

  2. Joris Gillis Guest

    Hi,

    Tempore 11:50:54, die Thursday 14 July 2005 AD, hinc in foro {comp.text.xml} scripsit <>:

    > <xsl:apply-templates select="airports/airport">
    > <xsl:sort data-type="number" select="string_priority"
    > order="descending"/>
    > </xsl:apply-templates>


    for the desired result, make that:
    <xsl:apply-templates select="airports/airport">
    <xsl:sort select="not($query=translate(Input_String, $upperCase,$lowerCase))"/>
    <xsl:sort data-type="number" select="string_priority" order="descending"/>
    </xsl:apply-templates>

    'not($query=translate(Input_String, $upperCase,$lowerCase))'
    will return a boolean value, this is converted to a string ('true' or 'false') and this is used to sort.


    btw, it seems you are sorting all 8000 'airport' elements. Wouldn't it be less CPU-intense to filter the matching nodes before sorting?

    regards,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    Spread the wiki (http://www.wikipedia.org)
    Joris Gillis, Jul 14, 2005
    #2
    1. Advertising

  3. Guest

    Thank you very much for you answer Joris, this was what exactly what I
    wanted.

    About the optimization. You mean that I should use for-each in the
    airport template instead and have the sort there?

    Regards,

    /José
    , Jul 14, 2005
    #3
  4. Joris Gillis Guest

    > About the optimization. You mean that I should use for-each in the
    > airport template instead and have the sort there?

    No No:)

    I meant you probably should move the test condition from the template itself to the apply-templates:

    ....
    <xsl:apply-templates select="airports/airport[Input_String[starts-with(translate(., $upperCase,$lowerCase),$query)]]">
    <xsl:sort select="not($query=translate(Input_String, $upperCase,$lowerCase))"/>
    <xsl:sort data-type="number" select="string_priority" order="descending"/>
    </xsl:apply-templates>
    ....


    <xsl:template match="airport">
    <p>
    <strong>Input String:</strong> <xsl:value-of
    select="Input_String"/><br/>
    <strong>Priority:</strong> <xsl:value-of
    select="string_priority"/><br/>
    <strong>Airport:</strong> <xsl:value-of select="Airport"/><br/>
    <strong>Country:</strong> <xsl:value-of select="country_int"/><br/>
    <strong>City:</strong> <xsl:value-of select="City_Interface"/><br/>
    <strong>Code:</strong> <xsl:value-of select="code"/><br/>
    </p>
    </xsl:template>


    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
    Joris Gillis, Jul 14, 2005
    #4
  5. Guest

    Ah, great, very nice performance improvement.

    Thank you very much for your help

    Regards

    /José
    , Jul 14, 2005
    #5
    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. Monique Y. Mudama
    Replies:
    1
    Views:
    438
    Monique Y. Mudama
    Jun 28, 2005
  2. Sebastian Millies

    SwingWorker and inherited thread priorities

    Sebastian Millies, Sep 12, 2005, in forum: Java
    Replies:
    5
    Views:
    2,315
    Thomas Hawtin
    Sep 12, 2005
  3. Soren Kuula
    Replies:
    2
    Views:
    543
    Malcolm Dew-Jones
    Apr 13, 2005
  4. Brandon J. Van Every

    Brandon's priorities, to your relief

    Brandon J. Van Every, Aug 19, 2003, in forum: Python
    Replies:
    4
    Views:
    324
    =?iso-8859-1?q?Fran=E7ois_Pinard?=
    Aug 20, 2003
  5. sg
    Replies:
    4
    Views:
    290
Loading...

Share This Page