2017-10-11 8 views
1

によってグループ化? XSLT 2.0以上を使用する方がよいでしょうか?XSLT、XML:XSLTを使用して属性値に基づいてグループ要素への最善の方法ですどの属性値

<transaction>  
    <record type="1" > 
    <field number="1" > 
     <item >223</item> 
    </field> 
    </record> 

    <record type="14" > 
    <field number="1" > 
     <item >777</item> 
    </field> 
    </record> 

    <record type="14" > 
    <field number="1" > 
     <item >555</item> 
    </field> 
    </record>  
</transaction> 

結果のグループ化後:

は多くは


オリジナルXMLあなたの助けを事前に

トーマスに感謝

<transaction> 
    <records type="1" > 
     <record type="1" > 
     <field number="1" > 
      <item >223</item> 
     </field> 
     </record> 
    </records> 

    <records type="14" > 
     <record type="14" > 
     <field number="1" > 
      <item >777</item> 
     </field> 
     </record> 

     <record type="14" > 
     <field number="1" > 
      <item >555</item> 
     </field> 
     </record> 
    </records> 
</transaction> 
XSLT 2.0で
+1

このXSLTを試してみてください。 XSLT 2.0を使用することができれば、それを使用することがほぼ確実です。この場合、 'xsl:for-each-group'構造を利用することができます。これにより、グループ化がはるかに簡単になります。したがって、この場合には、あなたはどうなる ':' –

+0

こんにちはティムます。の! ご返信ありがとうございました。 私は、次のテンプレートでそれを試してみました: ます。 \t \tます。 \t \tを\tます。 \t \t \t \tます。 \t \t \t \t \tます。 \t \t \t 結果XMLはルート要素のみが含まれています。 もう一度あなたのお手伝いに感謝します。 トーマス ThomasMuller

答えて

0

、あなたはxsl:for-each-groupを使用することができますが、<xsl:for-each-group select="record" group-by="@type">をやろうとしているならば、あなたはその時点でtransaction記録上に配置する必要があります。

さらに、グループ内のすべてのrecord要素を取得するには、current-groupを使用する必要があります。

は、答えは "イエス" である

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

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

<xsl:template match="transaction"> 
    <xsl:copy> 
     <xsl:for-each-group select="record" group-by="@type"> 
      <records type="{current-grouping-key()}" > 
       <xsl:apply-templates select="current-group()" /> 
      </records> 
     </xsl:for-each-group> 
    </xsl:copy> 
</xsl:template> 

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

こんにちはティム、素晴らしい仕事。あなたのサポートのためにもう一度多くのおかげで:-)よろしくお願いします。Thomas – ThomasMuller

+0

こんにちはTim、私は新しい質問を追加しました:https://stackoverflow.com/questions/46767754/xslt-xml-how-to-disentangle-grouped-blocks-フラットな階層へ どのような提案も大歓迎です。事前 トーマスで 感謝 – ThomasMuller

関連する問題