2017-05-21 51 views
0

我想排序兩個xml文件,以便將它們與winmerge進行比較,但是當我設法排序節點時我遇到了一個問題,但是我還需要使用一樣的名字。在xml中使用xslt多重排序

這裏是我的xml:

<node> 
    <location>A</location> 
    <title>folder in A</title> 
    <acl group="group_4" permissions="111111111"/> 
    <acl group="group_2" permissions="110000000"/> 
    <acl group="group_3" permissions="110000000"/> 
    <acl group="group_1" permissions="110000000"/> 
</node> 
<node> 
    <location>A</location> 
    <title>Another folder in A</title> 
    <acl group="group_1" permissions="110000000"/> 
    <acl group="group_3" permissions="111111111"/> 
    <acl group="group_2" permissions="110000000"/> 
</node> 

我的XSL是這樣的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" encoding="UTF-8" /> 
    <xsl:template match="/"> 
    <xsl:for-each select="import/node"> 
     <xsl:sort select="location" data-type="text" order="ascending"/> 
     <xsl:sort select="title[not(@clear)]" data-type="text" order="ascending"/> 
     <xsl:sort select="@group" data-type="text" order="ascending"/> 
     <node> 
     <xsl:copy-of select="location"/> 
     <xsl:copy-of select="title[not(@clear)]"/> 
     <xsl:copy-of select="acl[not(@basegroup) and not(@baseowner) and not(@standard)]"/> 
     </node> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

,我已經得到了這樣的結果:不是這一個

<node> 
    <location>A</location> 
    <title>Another folder in A</title> 
    <acl group="group_1" permissions="110000000"/> 
    <acl group="group_3" permissions="111111111"/> 
    <acl group="group_2" permissions="110000000"/> 
</node> 
<node> 
    <location>A</location> 
    <title>folder in A</title> 
    <acl group="group_4" permissions="111111111"/> 
    <acl group="group_2" permissions="110000000"/> 
    <acl group="group_3" permissions="110000000"/> 
    <acl group="group_1" permissions="110000000"/> 
</node> 

<node> 
    <location>A</location> 
    <title>Another folder in A</title> 
    <acl group="group_1" permissions="110000000"/> 
    <acl group="group_2" permissions="110000000"/> 
    <acl group="group_3" permissions="111111111"/> 
</node> 
<node> 
    <location>A</location> 
    <title>folder in A</title> 
    <acl group="group_1" permissions="110000000"/> 
    <acl group="group_2" permissions="110000000"/> 
    <acl group="group_3" permissions="110000000"/> 
    <acl group="group_4" permissions="111111111"/> 
</node> 

我已經檢查到類似的帖子,但沒有任何工作。提前致謝。最好的祝福。

+1

你舉的例子是不明確的。看來你想通過'location'和'title'對'node'元素進行排序,然後在每個節點**內對**元素進行排序,並通過'group'屬性對'act'元素進行排序。你實際上做的是試圖通過它們沒有的'group'屬性對'node'元素進行排序。 –

+0

你好邁克爾,你是對的,我嘗試按位置和標題排序節點元素,然後在每個節點中排序acl屬性。爲了做到這一點,我需要創建一個臨時組嗎? –

回答

0

您需要的acl要素分別進行排序,自己的xsl:for-each(或xsl:apply-templates)指令中 - 是這樣的:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes" encoding="UTF-8" /> 

<xsl:template match="/import"> 
    <xsl:for-each select="node"> 
     <xsl:sort select="location" data-type="text" order="ascending"/> 
     <xsl:sort select="title" data-type="text" order="ascending"/> 
     <node> 
      <xsl:copy-of select="location | title"/> 
      <xsl:for-each select="acl"> 
       <xsl:sort select="@group" data-type="text" order="ascending"/> 
       <xsl:copy-of select="."/> 
      </xsl:for-each> 
     </node> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 
+0

謝謝邁克爾這是工作:) –

+0

祝你有美好的一天。最好的祝福 –