不使用for-each循环时对数据进行排序

时间:2011-02-18 18:43:19

标签: xml xslt

我想根据节点“Reason”

的值按特定顺序对XML进行排序

我有我的XML

  <AgentSales>
    <AgentName>MEYER RICK</AgentName>
    <State>Talking Out</State>
    <Reason>Undefined</Reason>
    <time>29:09</time>
  </AgentSales>
  <AgentSales>
    <AgentName>BALENTINE JAMES</AgentName>
    <State>Talking Out</State>
    <Reason>Undefined</Reason>
    <time>16:07</time>
  </AgentSales>
  <AgentSales>
    <AgentName>SHOEMAKER ERIC</AgentName>
    <State>Talking Out</State>
    <Reason>Undefined</Reason>
    <time>08:21</time>
  </AgentSales>
  <AgentSales>
    <AgentName>HARVEY MICHAEL</AgentName>
    <State>Talking Out</State>
    <Reason>Undefined</Reason>
    <time>02:11</time>
  </AgentSales>
  <AgentSales>
    <AgentName>MORRIS BRANDEN</AgentName>
    <State>Talking Out</State>
    <Reason>Undefined</Reason>
    <time>02:05</time>
  </AgentSales>
  <AgentSales>
    <AgentName>FORER DAVID</AgentName>
    <State>Talking Out</State>
    <Reason>Undefined</Reason>
    <time>01:15</time>
  </AgentSales>

这是我目前的风格。它所做的只是忽略任何具有“说出来”状态的AgentSales但是然后只是沿着这条线走下去并按原样显示其余的数据。我希望按时间对数据进行排序,同时仍然没有显示带有“说出来”和“说话”的AgentSales我很确定我只需要将我的选择更新为:

<xsl:apply-templates select="NewDataSet/AgentSales[State!=\'Talking Out\'] || NewDataSet/AgentSales[State!=\'Talking In\']"/>

但是如何对剩下的剩余数据进行排序呢?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <table cellpadding="3" cellspacing="0" width="390">
                    <tr>
                        <th style="text-align:left;"><span style="font:20px arial; font-weight:bold;">Agent Name</span></th>
                        <th style="text-align:center;"><span style="font:20px arial; font-weight:bold;">State</span></th>
                        <th style="text-align:center;"><span style="font:20px arial; font-weight:bold;">Time</span></th>
                    </tr>
                    <xsl:apply-templates select="NewDataSet/AgentSales[State!=\'Talking Out\']"/>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="AgentSales">
        <tr>
            <xsl:if test="(position() mod 2 = 1)">
                <xsl:attribute name="bgcolor">#cccccc</xsl:attribute>
            </xsl:if>
            <td style="text-align:left;"><span style="font:14px arial; font-weight:bold;"><xsl:value-of select="AgentName"/></span></td>
            <td style="text-align:center;"><span style="font:14px arial; font-weight:bold;"><xsl:value-of select="State"/></span></td>
            <td style="text-align:center;"><span style="font:14px arial; font-weight:bold;"><xsl:value-of select="time"/></span></td>
        </tr>
    </xsl:template>

</xsl:stylesheet>

2 个答案:

答案 0 :(得分:2)

您可以向应用模板添加排序元素,如下所示。

<xsl:apply-templates select="NewDataSet/AgentSales[State!=\'Talking Out\']">
    <xsl:sort select="time" />
</xsl:apply-templates>

以下是更详细的article,讨论了这一点。

答案 1 :(得分:2)

使用

<xsl:apply-templates select=
  "NewDataSet/AgentSales
           [not(State='Talking Out')
          and
            not(State='Talking In')
           ]
"> 
  <xsl:sort select="time"/>
<xsl:apply-templates