XSLT多次传递排序无法正常工作

时间:2013-12-23 09:43:00

标签: xml sorting xslt xslt-2.0

我正在尝试使用XSLT生成一个已排序的HTML表。表数据生成,但是当我尝试对表的行进行排序时,顺序保持不变。

我的代码基于以下StackOverflow问题/回复xslt sort output xml

以下是生成表格的XSLT:

<xsl:variable name="vPass1">
    <unsortedLines>
        <xsl:for-each select="$customer/task">
            <xsl:variable name="task-code" select="./@code" />
                <xsl:for-each select="/workfile/calendar-list/calendar[@id=$customer/task[@code=$task-code]/consultant/@calendar-id]/items/event[summary=$task-code]">
                    <xsl:variable name="calendar" select="../.." />
                    <xsl:variable name="startdate" select="substring(start/datetime, 1, 10)" />
                    <xsl:variable name="enddate" select="substring(end/datetime, 1, 10)" />
                    <xsl:variable name="position" select="position()" />
                    <xsl:variable name="previousevents" select="/workfile/calendar-list/calendar[@id=$calendar/@id]/items/event[$startdate=substring(start/datetime, 1, 10) and summary=$task-code and position() &lt; $position]" />
                    <xsl:if test="not($previousevents or $previousevents='')">
                        <xsl:variable name="work-hour-sum">
                        <xsl:call-template name="sum">
                            <xsl:with-param name="events" select="/workfile/calendar-list/calendar[@id=$calendar/@id]/items/event[$startdate=substring(start/datetime, 1, 10) and summary=$task-code]" />
                        </xsl:call-template>
                        </xsl:variable>
                            <tr>
                                <xsl:attribute name="startdate">
                                    <xsl:value-of select="$startdate" />
                                </xsl:attribute>
                            <td><xsl:value-of select="$customer/@id" /></td>
                            <td><xsl:value-of select="/workfile/query/consultant[@calendar-id=$calendar/@id]/@code" /></td>
                            <td class="startdate"><xsl:value-of select="$startdate" /></td>
                            <td class="day-of-week"><xsl:value-of select="custom-elements/day-of-week[@lang='FR']" /></td>
                            <td><xsl:value-of select="$task-code" /></td>
                            <td>
                                <xsl:attribute name="class">
                                    <xsl:text>number</xsl:text>
                                </xsl:attribute>
                            <xsl:value-of select="format-number($work-hour-sum, '#.00')" />
                        </td>
                        </tr>
                    </xsl:if>
                </xsl:for-each>
            </xsl:for-each>
        </unsortedLines>
    </xsl:variable>
<xsl:apply-templates select="$vPass1/*" />

变量和测试(<xsl:if> ... </xsl:if>)工作正常,我认为它们会减损我的问题。

以下是排序模板的代码:

<xsl:template match="unsortedLines">
    <xsl:perform-sort select="*">
        <xsl:sort select="substring(tr/td[@class='startdate'], 9, 2)" />
    </xsl:perform-sort>
</xsl:template>

当我运行代码时,它将获取数据并填写表格行。但是当我应用排序模板时,顺序保持不变。

收到输出:

<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-02</td>
    <td>Lundi</td>
    <td>CLD</td>
    <td>16.50</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-05</td>
    <td>Jeudi</td>
    <td>CLD</td>
    <td>6.50</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-05</td>
    <td>Jeudi</td>
    <td>CLD</td>
    <td>6.50</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-05</td>
    <td>Jeudi</td>
    <td>CLD</td>
    <td>6.50</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-17</td>
    <td>Mardi</td>
    <td>CLD</td>
    <td>4.00</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-04</td>
    <td>Mercredi</td>
    <td>CLD-150</td>
    <td>9.00</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-04</td>
    <td>Mercredi</td>
    <td>CLD-150</td>
    <td>9.00</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-06</td>
    <td>Vendredi</td>
    <td>CLD-150</td>
    <td>1.00</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-01</td>
    <td>Dimanche</td>
    <td>CLD-200</td>
    <td>5.50</td>
</tr>

期望的输出:

<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-01</td>
    <td>Dimanche</td>
    <td>CLD-200</td>
    <td>5.50</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-02</td>
    <td>Lundi</td>
    <td>CLD</td>
    <td>16.50</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-04</td>
    <td>Mercredi</td>
    <td>CLD-150</td>
    <td>9.00</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-04</td>
    <td>Mercredi</td>
    <td>CLD-150</td>
    <td>9.00</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-05</td>
    <td>Jeudi</td>
    <td>CLD</td>
    <td>6.50</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-05</td>
    <td>Jeudi</td>
    <td>CLD</td>
    <td>6.50</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-05</td>
    <td>Jeudi</td>
    <td>CLD</td>
    <td>6.50</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-06</td>
    <td>Vendredi</td>
    <td>CLD-150</td>
    <td>1.00</td>
</tr>
<tr>
    <td>CLD</td>
    <td>PDI</td>
    <td>2013-12-17</td>
    <td>Mardi</td>
    <td>CLD</td>
    <td>4.00</td>
</tr>

谁能告诉我哪里犯了错误,以及如何纠正错误?非常感谢帮助。

AntonH

1 个答案:

答案 0 :(得分:2)

我认为您需要<xsl:sort select="substring(tr/td[@class='startdate'], 9, 2)" />而不是<xsl:sort select="substring(td[@class='startdate'], 9, 2)" />,因为您正在对包含tr子元素的td元素序列进行排序。