2017-09-25 51 views
2

我正嘗試在新節點下按重排節點(按排序順序)對重複節點進行分組。這是我的xml。在新節點下排序和組合重複節點

<NodeRoot> 
    <NodeA> 
     <NodeB> 
      <NodeC>101</NodeC> 
      <NodeC>102</NodeC> 
      <NodeC>101</NodeC> 
      <NodeC>104</NodeC> 
     </NodeB> 
    </NodeA> 
    <NodeA> 
     <NodeB> 
      <NodeC>102</NodeC> 
      <NodeC>103</NodeC> 
      <NodeC>101</NodeC> 
      <NodeC>102</NodeC> 
     </NodeB> 
    </NodeA> 
</NodeRoot> 

這就是我想要實現的。請注意,重複的NodeC首先進行排序,然後分組到新的NodeGroup節點下。

<NodeRoot> 
    <NodeA> 
     <NodeB> 
      <NodeGroup> 
       <NodeC>101</NodeC> 
       <NodeC>101</NodeC> 
      </NodeGroup> 
      <NodeGroup> 
       <NodeC>102</NodeC> 
      </NodeGroup> 
      <NodeGroup> 
       <NodeC>104</NodeC> 
      </NodeGroup> 
     </NodeB> 
    </NodeA> 
    <NodeA> 
     <NodeB> 
      <NodeGroup> 
       <NodeC>101</NodeC> 
      </NodeGroup> 
      <NodeGroup> 
       <NodeC>102</NodeC> 
       <NodeC>102</NodeC> 
      </NodeGroup> 
      <NodeGroup> 
       <NodeC>103</NodeC> 
      </NodeGroup> 
     </NodeB> 
    </NodeA> 
</NodeRoot> 

這是我的XSLT(我只能訪問xslt1)

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

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="NodeB"> 
     <NodeB> 
       <xsl:apply-templates select="NodeB[not(NodeC = preceding-sibling::NodeB/NodeC)]" mode="GroupC"> 
        <xsl:sort select="NodeC" data-type="number"/> 
       </xsl:apply-templates> 
     </NodeB> 
    </xsl:template> 

    <xsl:template match="NodeB" mode="GroupC"> 
     <xsl:variable name="GC" select="NodeC"/> 
     <NodeB> 
      <NodeGroup> 
       <xsl:apply-templates select="NodeB[NodeC/text()=$GC]" mode="SameC" /> 
      </NodeGroup> 
     </NodeB> 
    </xsl:template> 

    <xsl:template match="NodeC" mode="SameC"> 
     <xsl:copy-of select="NodeC"/> 
    </xsl:template> 

</xsl:stylesheet> 

這不是分組重複NodeCs。誰能幫忙?

回答

0

如果你想要採取這種方法與前面的兄弟姐妹,那麼你只需要確保你處理NodeC孩子:

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

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="NodeB"> 
     <NodeB> 
       <xsl:apply-templates select="NodeC[not(. = preceding-sibling::NodeC)]" mode="GroupC"> 
        <xsl:sort select="." data-type="number"/> 
       </xsl:apply-templates> 
     </NodeB> 
    </xsl:template> 

    <xsl:template match="NodeC" mode="GroupC"> 
     <NodeGroup> 
      <xsl:copy-of select="../NodeC[. = current()]"/> 
     </NodeGroup> 
    </xsl:template> 

</xsl:stylesheet> 

額外的模式甚至沒有必要這樣。

在XSLT 1.0分組通常更有效的使用Muenchian grouping雖然在這裏你那麼將需要一個地連接了NodeB父的生成的ID與NodeC價值的關鍵,讓您識別的NodeB子樹內組是

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

    <xsl:key name="group" match="NodeC" use="concat(generate-id(..), '|', .)"/> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="NodeB"> 
     <NodeB> 
       <xsl:apply-templates select="NodeC[generate-id() = generate-id(key('group', concat(generate-id(..), '|', .))[1])]" mode="GroupC"> 
        <xsl:sort select="." data-type="number"/> 
       </xsl:apply-templates> 
     </NodeB> 
    </xsl:template> 

    <xsl:template match="NodeC" mode="GroupC"> 
     <NodeGroup> 
      <xsl:copy-of select="key('group', concat(generate-id(..), '|', .))"/> 
     </NodeGroup> 
    </xsl:template> 

</xsl:stylesheet> 
+0

我嘗試使用Muenchian分組,但無法在不同的NodeB中對NodeC(基於值)進行分組。事實上,我很樂意在Muenchian的分組中做到這一點,我相信這樣做更有效率。 – asd123

+0

不確定你的意思是說「我確實需要一個新節點」。我已經使用Muenchian分組添加了一個示例。 –

+0

感謝這兩個解決方案。我以爲你的意思是不需要額外的節點(如在節點組節點中)。閱讀不正確。我還是無法把頭腦放在關鍵位置(對Muenchian分組不太熟悉,如果你能爲我和更廣泛的觀衆解釋一下,這將是非常好的。 – asd123