XSLT按复杂日期排序

时间:2012-12-31 20:54:37

标签: xml sorting xslt

我正在尝试按日期和时间对此xml进行排序

<?xml version="1.0" encoding="utf-8"?>
<FormComments>
    <Comments EventName="Position Clearance">
        <Comment>
            <IsWindowsUser>
                <![CDATA[True]]>
            </IsWindowsUser>
            <UserName>
                <![CDATA[DOMAIN\userId]]>
            </UserName>
            <DisplayName>
                <![CDATA[DOMAIN\userId]]>
            </DisplayName>
            <DateTime>
                <![CDATA[12/21/2012 2:47 PM]]>
            </DateTime>
            <Body>
                <![CDATA[Clearance is obtained]]>
            </Body>
            <Action>
                <![CDATA[Clearance Obtained]]>
            </Action>
        </Comment>
        <Comment>
            <IsWindowsUser>
                <![CDATA[True]]>
            </IsWindowsUser>
            <UserName>
                <![CDATA[DOMAIN\userId]]>
            </UserName>
            <DisplayName>
                <![CDATA[DOMAIN\userId]]>
            </DisplayName>
            <DateTime>
                <![CDATA[12/21/2012 2:50 PM]]>
            </DateTime>
            <Body>
                <![CDATA[Clearance already obtained]]>
            </Body>
            <Action>
                <![CDATA[Clearance Obtained]]>
            </Action>
        </Comment>
    </Comments>
    <Comments EventName="Comp Advisor">
        <Comment>
            <IsWindowsUser>
                <![CDATA[True]]>
            </IsWindowsUser>
            <UserName>
                <![CDATA[DOMAIN\userId]]>
            </UserName>
            <DisplayName>
                <![CDATA[DOMAIN\userId]]>
            </DisplayName>
            <DateTime>
                <![CDATA[12/21/2012 2:48 PM]]>
            </DateTime>
            <Body>
                <![CDATA[This form needs modifications!!]]>
            </Body>
            <Action>
                <![CDATA[Modifications Needed]]>
            </Action>
        </Comment>
        <Comment>
            <IsWindowsUser>
                <![CDATA[True]]>
            </IsWindowsUser>
            <UserName>
                <![CDATA[DOMAIN\userId]]>
            </UserName>
            <DisplayName>
                <![CDATA[DOMAIN\userId]]>
            </DisplayName>
            <DateTime>
                <![CDATA[12/21/2012 2:51 PM]]>
            </DateTime>
            <Body>
                <![CDATA[Still not up to par!]]>
            </Body>
            <Action>
                <![CDATA[Deny]]>
            </Action>
        </Comment>
    </Comments>
    <Comments EventName="Modify">
        <Comment>
            <IsWindowsUser>
                <![CDATA[True]]>
            </IsWindowsUser>
            <UserName>
                <![CDATA[DOMAIN\userId]]>
            </UserName>
            <DisplayName>
                <![CDATA[DOMAIN\userId]]>
            </DisplayName>
            <DateTime>
                <![CDATA[12/21/2012 2:49 PM]]>
            </DateTime>
            <Body>
                <![CDATA[Done fixing the form]]>
            </Body>
            <Action>
                <![CDATA[Changes Made Resubmit]]>
            </Action>
        </Comment>
    </Comments>
    <Comments EventName="How to Proceed">
    <Comment>
        <IsWindowsUser>
            <![CDATA[True]]>
        </IsWindowsUser>
        <UserName>
            <![CDATA[DOMAIN\userId]]>
        </UserName>
        <DisplayName>
            <![CDATA[DOMAIN\userId]]>
        </DisplayName>
        <DateTime>
            <![CDATA[12/21/2012 2:52 PM]]>
        </DateTime>
        <Body>
            <![CDATA[Forget it!!!]]>
        </Body>
        <Action>
            <![CDATA[Stop]]>
        </Action>
     </Comment>
  </Comments>
</FormComments>

我想出了这个

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

    <STYLE TYPE="text/css">
        TD{
        font-family: Arial; font-size: 9pt;
        background color:"#939495";
        }

        TD.Center{
        text-align:"center";
        }

        TH{
        background color:"#B31B34";
        }

        caption{
        font-size:20pt;
        }
    </STYLE>

    <hr />

    <table cellpadding='8' border='1'>
        <caption>Workflow Information </caption>
        <th>Event</th>
        <th>Approver</th>
        <th>Action</th>
        <th>Comment</th>
        <th>Time</th>

        <xsl:for-each select="FormComments/Comments">
                      <!-- Sort by year -->
                      <xsl:sort select="substring-before(substring-after(substring-after(Comment/DateTime,'/'),'/'),' ')"/>
                      <!-- Sort by Month -->
                      <xsl:sort select="substring(normalize-space(Comment/DateTime),1,2)"/>
                      <!-- Sort by AM / PM -->
                      <xsl:sort select="substring-after(substring-after(normalize-space(Comment/DateTime), ' '),' ')"/>
                      <!-- Sort by hour -->
                      <xsl:sort select="substring-before(substring-after(substring-after(Comment/DateTime,' '),' '),':')"/>
                      <!-- Sort by minute -->
                      <xsl:sort select="substring-before(substring-after(normalize-space(Comment/DateTime), ':'),' ')"/>

                               <xsl:variable name="eventName">
                <xsl:value-of select="@EventName" />
            </xsl:variable>

            <xsl:for-each select="Comment">

                <xsl:variable name="User">
                    <xsl:value-of select="UserName" />
                </xsl:variable>

                <tr>
                    <td>
                        <xsl:copy-of select="$eventName" />
                    </td>
                    <td>
                        <xsl:copy-of select="substring-after($User,'\')" />
                    </td>
                    <td class="Center">
                        <xsl:value-of select="Action"/>
                    </td>
                    <td>
                        <xsl:value-of select="Body"/>
                    </td>
                    <td>
                        <xsl:value-of select="DateTime"/>
                    </td>

                </tr>
            </xsl:for-each>
        </xsl:for-each>
    </table>
</xsl:template>
</xsl:stylesheet>

问题是它只在Comments节点内排序,而不是在所有Comments节点上排序。

我无法发布图片,因为我刚刚注册了此网站...输出看起来像

Workflow Information  
Event              Approver  Action              Comment                Time 
Position Clearance tf114096  Clearance Obtained  Clearance is obtained  12/21/2012 2:47 PM  
Position Clearance rbg14096  Clearance Obtained  Clearance already obtained  12/21/2012 2:50 PM  
Comp Advisor       thy14096  Modifications Needed  This form needs modifications!!  12/21/2012 2:48 PM  
Comp Advisor       trw14096  Deny  Still not up to par!  12/21/2012 2:51 PM  
Modify             we214096  Changes Made Resubmit  Done fixing the form  12/21/2012 2:49 PM  
How to Proceed     cf414096  Stop  Forget it!!!  12/21/2012 2:52 PM  

请注意,发生过两次的事件会在该事件中排序,但不会针对所有其他事件排序。 在给定这种XML结构的情况下,有没有办法对此进行排序?

我一直在使用这个在线工具来测试我的XSLT http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

预期结果应如下所示

 Workflow Information  
 Event              Approver  Action              Comment                Time 
 Position Clearance tf114096  Clearance Obtained  Clearance is obtained  12/21/2012 2:47 PM 
 Comp Advisor       thy14096  Modifications Needed  This form needs modifications!!  12/21/2012 2:48 PM 
 Modify             we214096  Changes Made Resubmit  Done fixing the form  12/21/2012 2:49 PM 
 Position Clearance rbg14096  Clearance Obtained  Clearance already obtained  12/21/2012 2:50 PM  
 Comp Advisor       trw14096  Deny  Still not up to par!  12/21/2012 2:51 PM  
 How to Proceed     cf414096  Stop  Forget it!!!  12/21/2012 2:52 PM 

结果完整排序..

1 个答案:

答案 0 :(得分:2)

这里的问题是您要对评论元素进行排序,但评论元素中有多个评论元素,而您是仅按第一个日期排序评论元素。

这里你真的不需要两个 xsl:for-each 语句。您可以直接迭代子评论元素,这将解决您的排序问题。

<xsl:for-each select="FormComments/Comments/Comment">

在迭代子注释元素之前,您似乎想首先访问注释元素的 eventName 属性。这仍然是可以实现的,只需在位于注释元素上时使用父选择器

<xsl:value-of select="../@EventName"/>

尝试以下XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <table>
            <th>Event</th>
            <th>Approver</th>
            <th>Action</th>
            <th>Comment</th>
            <th>Time</th>
            <xsl:for-each select="FormComments/Comments/Comment"><!-- Sort by year -->
                <xsl:sort select="substring-before(substring-after(substring-after(DateTime,'/'),'/'),' ')"/><!-- Sort by Month -->
                <xsl:sort select="substring(normalize-space(DateTime),1,2)"/><!-- Sort by AM / PM -->
                <xsl:sort select="substring-after(substring-after(normalize-space(DateTime), ' '),' ')"/><!-- Sort by hour -->
                <xsl:sort select="substring-before(substring-after(substring-after(DateTime,' '),' '),':')"/><!-- Sort by minute -->
                <xsl:sort select="substring-before(substring-after(normalize-space(DateTime), ':'),' ')"/>
                <xsl:variable name="User">
                    <xsl:value-of select="UserName"/>
                </xsl:variable>
                <tr>
                    <td>
                        <xsl:value-of select="../@EventName"/>
                    </td>
                    <td>
                        <xsl:value-of select="substring($User,8)"/>
                    </td>
                    <td class="Center">
                        <xsl:value-of select="Action"/>
                    </td>
                    <td>
                        <xsl:value-of select="Body"/>
                    </td>
                    <td>
                        <xsl:value-of select="DateTime"/>
                    </td>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

(为了简洁我已经删除了一些行)

当应用于您的XML时,输出以下内容

<table>
<th>Event</th>
<th>Approver</th>
<th>Action</th>
<th>Comment</th>
<th>Time</th>
<tr>
<td>Position Clearance</td>
<td>userId</td>
<td class="Center">Clearance Obtained</td>
<td>Clearance is obtained</td>
<td>12/21/2012 2:47 PM</td>
</tr>
<tr>
<td>Comp Advisor</td>
<td>userId</td>
<td class="Center">Modifications Needed</td>
<td>This form needs modifications!!</td>
<td>12/21/2012 2:48 PM</td>
</tr>
<tr>
<td>Modify</td>
<td>userId</td>
<td class="Center">Changes Made Resubmit</td>
<td>Done fixing the form</td>
<td>12/21/2012 2:49 PM</td>
</tr>
<tr>
<td>Position Clearance</td>
<td>userId</td>
<td class="Center">Clearance Obtained</td>
<td>Clearance already obtained</td>
<td>12/21/2012 2:50 PM</td>
</tr>
<tr>
<td>Comp Advisor</td>
<td>userId</td>
<td class="Center">Deny</td>
<td>Still not up to par!</td>
<td>12/21/2012 2:51 PM</td>
</tr>
<tr>
<td>How to Proceed</td>
<td>userId</td>
<td class="Center">Stop</td>
<td>Forget it!!!</td>
<td>12/21/2012 2:52 PM</td>
</tr>
</table>