tags:

views:

35

answers:

1

I am using the Muenchian method to group data from my xml document from Marc's solution and I am trying to sort the data within each group without much success.

I have tried the following, but no sorting is carried out:

replaced:

<xsl:value-of select="MemberLastName"/>

with

<xsl:apply-templates select="MemberLastName" > 
    <xsl:sort order="ascending" /> 
</xsl:apply-templates>

I have also tried the following, but the data is being outputted as plain text, and not sorted

 <xsl:template match="/NewDataSet/QueryResultData">
    <xsl:apply-templates>
      <xsl:sort select="MemberLastName"/>
   <xsl:sort select="MemberFirstName"/>
    </xsl:apply-templates>
  </xsl:template>

with:

<xsl:apply-templates select="MemberLastName"/>

here's my complete code:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="msxsl"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;


  <xsl:output method="xml" indent="yes"/>      
  <xsl:key name="groups" match="/NewDataSet/QueryResultData" use="MemberReportGroup4Description" />

  <xsl:template match="/NewDataSet">
    <xsl:apply-templates select="QueryResultData[generate-id() = generate-id(key('groups', MemberReportGroup4Description)[1])]"/>
  </xsl:template>

  <xsl:template name="formatDate">
    <xsl:param name="dateTime" />
    <xsl:variable name="date" select="substring-before($dateTime, 'T')" />
    <xsl:variable name="year" select="substring-before($date, '-')" />
    <xsl:variable name="month" select="substring-before(substring-after($date, '-'), '-')" />
    <xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')" />
    <xsl:value-of select="concat($month, '/', $day, '/', $year)" />
  </xsl:template>

  <xsl:template name="formatCourse">
    <xsl:param name="courseTitle" />
    <xsl:variable name="course" select="substring-after($courseTitle, '|')" />
    <xsl:value-of select='$course' />
  </xsl:template>

   <xsl:template name="organizationName">
    <xsl:param name="orgName" />
    <xsl:value-of select='$orgName' />
  </xsl:template> 

   <xsl:template match="/NewDataSet/QueryResultData">
    <xsl:apply-templates>
      <xsl:sort select="MemberLastName"/>
      <xsl:sort select="MemberFirstName"/>
    </xsl:apply-templates>
  </xsl:template>






  <xsl:template name="QueryResultData" match="QueryResultData">

  <style type="text/css">
th { text-align:left; font-weight:bold;}

#stores
{
font-family:"Arial", Helvetica, sans-serif;
width:600px;
border-collapse:collapse;
padding: 5px;
}
#stores td, #stores th 
{
font-size:8pt;
border:1px solid #C8EAC8;
padding:3px 7px 2px 7px;
}
#stores th 
{
font-size:8pt;
text-align:left;
padding-top:5px;
padding-bottom:4px;
background-color:#339933;
color:#fff;
}
.odd 
{
color:#000;
background-color:#ECF8EC;
}
h1.store { font-family: Arial, Helvetica, sans-serif; font-size: 18px; color: #000099; font-weight:bold; margin-bottom:-2px;}

.CenterText { text-align:center !important; }
</style>


    <h1 class="store"> 
        <xsl:call-template name="organizationName">
          <xsl:with-param name="orgName" select="/NewDataSet/MetaData/OrganizationName" />
        </xsl:call-template>, #<xsl:value-of select="MemberReportGroup4Description"/></h1>
    <table id="stores">
      <tr class="heading">
        <th scope="col">Learner</th>
        <th scope="col">Course</th>
        <th scope="col">Enrollment Date</th>
        <th scope="col" class="CenterText">Viewed %</th>
      </tr>
      <xsl:for-each select="key('groups', MemberReportGroup4Description)">
        <tr>
          <xsl:if test="position() mod 2 = 1">
            <xsl:attribute name="class">odd</xsl:attribute>
          </xsl:if>
          <td>
            <xsl:value-of select="MemberLastName"/>, <xsl:value-of select="MemberFirstName"/>
          </td>
          <td>
            <xsl:call-template name="formatCourse">
                <xsl:with-param name="courseTitle" select="SortPrefixAndTitle" />
            </xsl:call-template>
          </td>
          <td>
            <xsl:call-template name="formatDate">
                <xsl:with-param name="dateTime" select="EnrollmentDate" />
            </xsl:call-template>
          </td>
          <td class="CenterText">
            <xsl:value-of select="ViewedPercentage"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>
+1  A: 

Don't change <xsl:value-of select="MemberLastName"/>. Instead, just put the sort element above it, just under the for-each:

  <xsl:for-each select="key('groups', MemberReportGroup4Description)">
    <xsl:sort select="MemberLastName"/>
    <xsl:sort select="MemberFirstName"/>
    <tr>
      <xsl:if test="position() mod 2 = 1">
        <xsl:attribute name="class">odd</xsl:attribute>
      </xsl:if>
      <td>
        <xsl:value-of select="MemberLastName"/>, <xsl:value-of select="MemberFirstName"/>
      </td>
      ...
    </tr>
  </xsl:for-each>
Owen S.
Thanks Owen! Worked great.
Neil