Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Novice: Sorting in XSL 1

Status
Not open for further replies.

ad2

Technical User
Dec 31, 2002
186
0
0
US
Hi - I need to sort the libraries in each county by LIBRARY_NAME. Any help on where the
<xsl:sort select="LIBRARY_NAME"/> would be in the XSL file.- Thanks!


Short versions of each file:

Here is my XML doc:
<LIBRARIES>
<COUNTY>
<COUNTY_NAME>Boone</COUNTY_NAME>

<LIBRARY>
<LIBRARY_NAME>Lebanon Public Library</LIBRARY_NAME>
<ADDRESS>104 E Washington St.</ADDRESS>
<CITY>Lebanon</CITY>
<STATE>IN</STATE>
<ZIP>46052</ZIP>
<PHONE>(765) 482-3460</PHONE>
<FAX>(317) 873-5059 </FAX>
<WEB> </LIBRARY>

<LIBRARY>
<LIBRARY_NAME>Thorntown Public Library</LIBRARY_NAME>
<ADDRESS>124 N. Market St.</ADDRESS>
<CITY>Thorntown</CITY>
<STATE>IN</STATE>
<ZIP>46071</ZIP>
<PHONE>(765) 436-7348</PHONE>
<FAX>(765) 436-7011</FAX>
<WEB> </LIBRARY>
<LIBRARY>
<LIBRARY_NAME>Hussey-Mayfield Memorial Public Library</LIBRARY_NAME>
<ADDRESS>250 N. 5th St.</ADDRESS>
<CITY>Zionsville</CITY>
<STATE>IN</STATE>
<ZIP>46077</ZIP>
<PHONE>(317) 873-3149</PHONE>
<FAX>(317) 873-8339</FAX>
<WEB> </LIBRARY>

</COUNTY>
</LIBRARIES>

Here is My XSL:
<xsl:eek:utput method="html" indent="yes"/>

<xsl:template match="/"> <!--root rule-->
<html>
<head><title>County Libraries</title></head>
<body><xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="COUNTY">
<xsl:apply-templates select="COUNTY_NAME"/>
<xsl:apply-templates select="LIBRARY"/>
</xsl:template>

<xsl:template match="COUNTY/COUNTY_NAME"> <!-- Get the name of the county and display in in a table cell-->
<table width="80%">
<tr>
<td bgcolor="#99ccff"> <!--When you spit out the HTML, add an a name link for each county name, thus creating a link for the clickable map-->
<h3><a><xsl:attribute name="name"><xsl:apply-templates/></xsl:attribute></a><xsl:apply-templates/> <xsl:text> County</xsl:text></h3>
</td>
</tr>
</table>
</xsl:template>

<xsl:template match="LIBRARY"><!-- get each library for the county and display it-->
<b><xsl:apply-templates select="LIBRARY_NAME"/></b><br/>
<xsl:apply-templates select="ADDRESS"/><br/>
<xsl:apply-templates select="CITY"/><xsl:text>, </xsl:text>
<xsl:apply-templates select="STATE"/><xsl:text> </xsl:text>
<xsl:apply-templates select="ZIP"/><br/>
<xsl:text>Phone: </xsl:text><xsl:apply-templates select="PHONE"/><br/>
<xsl:text>FAX: </xsl:text><xsl:apply-templates select="FAX"/><br/>
<xsl:text>WEB: </xsl:text>
<a><xsl:attribute name="href"> <!--make the web address a hot link-->
<xsl:text> select="WEB"/>
</xsl:attribute>
<i><xsl:value-of select="WEB"/></i>
</a>
<p> </p>
</xsl:template>

</xsl:stylesheet>
 
This worked in cooktop...

<xsl:template match="COUNTY">
<xsl:apply-templates select="COUNTY_NAME">
<xsl:sort select="COUNTY_NAME"/>
</xsl:apply-templates>
<xsl:apply-templates select="LIBRARY">
<xsl:sort select="LIBRARY_NAME"/>
</xsl:apply-templates>
</xsl:template>
 
Thank you rabbit65!
 
Rabbitt65,

Made one slight modification, so that the counties would sort when not in order on the XML file, I had to make a separate template match. Thanks for your help!!!

<xsl:template match="LIBRARIES"> <!--when you get the counties, sort them in alpha order-->
<xsl:apply-templates select="COUNTY">
<xsl:sort select="COUNTY_NAME" order="ascending"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="COUNTY"> <!--when you get the libraries in a county, sort them in alpha order-->
<xsl:apply-templates select="COUNTY_NAME">
</xsl:apply-templates>
<xsl:apply-templates select="LIBRARY">
<xsl:sort select="LIBRARY_NAME"/>
</xsl:apply-templates>
</xsl:template>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top