XSLT排序

时间:2010-07-06 21:55:03

标签: xslt sorting

我正在使用Muenchian方法对来自Marc's solution的xml文档中的数据进行分组,我试图对每个组中的数据进行排序,但没有取得多大成功。

我尝试过以下操作,但没有进行排序:

取代:

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

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

我也试过以下内容,但数据是以纯文本形式输出的,而不是排序

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

使用:

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

这是我的完整代码:

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


  <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 个答案:

答案 0 :(得分:0)

请勿更改<xsl:value-of select="MemberLastName"/>。相反,只需将排序元素放在它上面,就在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>