Jump to content

xsl:sort - Using a primary and secondary sorting node

Recommended Posts

Hi everybody, First post here, hoping someone can help. I'm looking for a way to specify a primary and secondary column to sort by in a table. For example, a list of people that are to be contacted in an emergency, some of whom are "Primary" contacts, and some who are "Alternate" contacts. What I want is for the primaries to be listed first, in alphabetical order, before any alternates, who are also listed alphabetically. (Primary and Alternate have also been assigned a numerical value, with Primary being 1 and Alternate being 2.) Currently, these table sort only on one node, the person's name, so I get: Type Name1. Primary Ben Smith2. Alternate David Willis2. Alternate Mary Jones1. Primary Robert Crane What I want is: Type Name1. Primary Ben Smith1. Primary Robert Crane2. Alternate David Willis2. Alternate Mary Jones The values are being pulled from database software where each field (i.e. "Type" or "Name") has a Field ID that the XSLT uses to populate the output document. Code looks like this right now:

<table>   <tbody>	  <tr>		 <th>Type</th>		 <th>Name</th>	  </tr>		 <xsl:for-each select="Record/Field[ID='8132']">		 <xsl:sort select="../Field[ID='8131']/Value"/>	  <tr>		 <td><xsl:value-of select="../Field[ID='8132']/Value"/></td>		 <td><xsl:value-of select="../Field[ID='8131']/Value"/></td>		 </xsl:for-each>	  </tr>   </tbody></table>

'8132' is the Type field, '8131' is the Name field. Hope I've explained this clearly. Thanks in advance for any assistance!

Link to comment
Share on other sites

  • 2 weeks later...

If your input looks like this:

<database><Record><Field ID="8131"><Value>1. Primary</Value></Field><Field ID="8132"><Value>Robert Crane</Value></Field></Record><Record><Field ID="8131"><Value>2. Secondary</Value></Field><Field ID="8132"><Value>David Willis</Value></Field></Record><Record><Field ID="8131"><Value>2. Secondary</Value></Field><Field ID="8132"><Value>Mary Jones</Value></Field></Record><Record><Field ID="8131"><Value>1. Primary</Value></Field><Field ID="8132"><Value>Ben Smith</Value></Field></Record></database>

and style sheet like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"><xsl:output method="html"/><xsl:template match="database">  <table>   <tbody>    <tr>	 <th>Type</th>	 <th>Name</th>    </tr>    <xsl:for-each select="Record">	 <xsl:sort select="Field[@ID='8131']"/>	 <xsl:sort select="Field[@ID='8132']"/>	 <xsl:apply-templates select="."/>    </xsl:for-each>   </tbody>  </table></xsl:template><xsl:template match="Record">  <tr>   <xsl:apply-templates/>  </tr></xsl:template><xsl:template match="Field">  <xsl:choose>   <xsl:when test="@ID='8131'">    <td>	 <xsl:apply-templates/>    </td>   </xsl:when>   <xsl:when test="@ID='8132'">    <td>	 <xsl:apply-templates/>    </td>   </xsl:when>  </xsl:choose></xsl:template><xsl:template match="*">  <xsl:element name="{name()}">   <xsl:apply-templates/>  </xsl:element></xsl:template></xsl:stylesheet>

Your output will be double sorted liked this:

<table><tbody>  <tr>   <th>Type</th>   <th>Name</th>  </tr>  <tr>   <td>    <Value>1. Primary</Value>   </td>   <td>    <Value>Ben Smith</Value>   </td>  </tr>  <tr>   <td>    <Value>1. Primary</Value>   </td>   <td>    <Value>Robert Crane</Value>   </td>  </tr>  <tr>   <td>    <Value>2. Secondary</Value>   </td>   <td>    <Value>David Willis</Value>   </td>  </tr>  <tr>   <td>    <Value>2. Secondary</Value>   </td>   <td>    <Value>Mary Jones</Value>   </td>  </tr></tbody></table>

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...