按复杂日期排序



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

<?xml version="1.0" encoding="utf-8"?>
<FormComments>
    <Comments EventName="Position Clearance">
        <Comment>
            <IsWindowsUser>
                <![CDATA[True]]>
            </IsWindowsUser>
            <UserName>
                <![CDATA[DOMAINuserId]]>
            </UserName>
            <DisplayName>
                <![CDATA[DOMAINuserId]]>
            </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[DOMAINuserId]]>
            </UserName>
            <DisplayName>
                <![CDATA[DOMAINuserId]]>
            </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[DOMAINuserId]]>
            </UserName>
            <DisplayName>
                <![CDATA[DOMAINuserId]]>
            </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[DOMAINuserId]]>
            </UserName>
            <DisplayName>
                <![CDATA[DOMAINuserId]]>
            </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[DOMAINuserId]]>
            </UserName>
            <DisplayName>
                <![CDATA[DOMAINuserId]]>
            </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[DOMAINuserId]]>
        </UserName>
        <DisplayName>
            <![CDATA[DOMAINuserId]]>
        </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结构的情况下,是否存在排序方法?

我一直在使用这个在线工具测试我的XSLThttp://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 

结果完全排序…

这里的问题是,您正在排序comments元素,但是在comments元素中有多个comment元素,并且您仅按第一个日期排序comments元素。

这里并不需要两个xsl:for-each语句。您可以直接遍历注释子元素,这将解决排序问题。
<xsl:for-each select="FormComments/Comments/Comment">

看起来您想先访问Comments元素的eventName属性,然后再遍历子Comment元素。这仍然是可以实现的,只要在注释元素

上使用父选择器
<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>

最新更新