0

我正在尝试按日期和时间对此 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>

问题是它只在评论节点内排序,而不是在所有评论节点中排序。

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

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 

结果完全排序..

4

1 回答 1

2

这里的问题是您正在对评论元素进行排序,但评论元素中有多个评论元素,并且您只是按第一个日期对评论元素进行排序。

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

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

看起来您想先访问Comments元素的eventName属性,然后再遍历子Comment元素。这仍然可以实现,只需在位于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>
于 2012-12-31T22:13:36.880 回答