Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT按复杂日期排序_Xml_Sorting_Xslt - Fatal编程技术网

Xml XSLT按复杂日期排序

Xml XSLT按复杂日期排序,xml,sorting,xslt,Xml,Sorting,Xslt,我正在尝试按日期和时间对xml进行排序 <?xml version="1.0" encoding="utf-8"?> <FormComments> <Comments EventName="Position Clearance"> <Comment> <IsWindowsUser> <![CDATA[True]]> <

我正在尝试按日期和时间对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结构的情况下,是否有方法对其进行排序

我一直在使用这个在线工具来测试我的XSLT

预期结果应该是这样的

 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 

结果完全排序。

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

这里不需要为每个
语句使用两个xsl:for。您可以直接迭代子comment元素,这将解决排序问题

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

在迭代子Comment元素之前,您似乎想先访问Comments元素的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>

事件
批准人
行动
评论
时间
位置间隙
用户ID
获得许可
获得间隙
2012年12月21日下午2:47
薪酬顾问
用户ID
需要修改
这个表格需要修改!!
2012年12月21日下午2:48
修改
用户ID
重新提交所做的更改
修好表格了吗
2012年12月21日下午2:49
位置间隙
用户ID
获得许可
已获得许可
2012年12月21日下午2:50
薪酬顾问
用户ID
否认
还没有达到标准!
2012年12月21日下午2:51
如何进行
用户ID
停止
算了吧!!!
2012年12月21日下午2:52

在这种情况下,是否可以显示您的预期输出?谢谢您忘记了一件重要的事情——向我们展示转换必须产生的确切的想要的输出。请编辑此问题并提供此重要信息。谢谢Tim!!!每个人的双倍是让我痛苦的。你的解决方案非常有效+一个好答案。新年快乐@请点击答案旁边的复选标记,接受答案。
<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>
<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>