Sorting results by 2 different priorities

J

jose.jeria

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?
 
J

Joris Gillis

Hi,

Tempore 11:50:54 said:
<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,
 
J

jose.jeria

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é
 
J

Joris Gillis

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>
 
J

jose.jeria

Ah, great, very nice performance improvement.

Thank you very much for your help

Regards

/José
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top