2017-10-16 165 views
0

我有以下XML與一些嵌套元素。 我需要幫助將此XML轉換爲平面層次結構。XSLT,XML:如何將分組塊分爲平面層次結構?

你可能想看看這個問題還有:提前 XSLT, XML: Grouping by attribute value

感謝您的支持。 托馬斯

原始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> 

    <record type="200" > 
    <field number="1" > 
     <item>546</item> 
    </field> 
    </record> 

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

目標XML:

<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> 

    <record type="200" > 
    <field number="1" > 
     <item>546</item> 
    </field> 
    </record> 

    <record type="201" > 
    <field number="1" > 
     <item>123</item> 
    </field> 
    </record> 
</transaction> 
+0

到目前爲止你有什麼? – Steve

+0

我從蒂姆的答案XLST:https://stackoverflow.com/questions/46693291/xslt-xml-grouping-by-attribute-value – ThomasMuller

+0

@ThomasMuller不是你想做的完全相反嗎? – kumesana

回答

1

試試這個:

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

    <xsl:template match="/"> 
     <xsl:text>&#x0A;</xsl:text> 
     <transaction> 
      <xsl:text>&#x0A;</xsl:text> 
      <xsl:for-each select="//record"> 
       <xsl:copy-of select="." /> 
       <xsl:text>&#x0A;</xsl:text> 
      </xsl:for-each> 
      <xsl:text>&#x0A;</xsl:text> 
     </transaction> 
    </xsl:template> 

</xsl:stylesheet> 

<xsl:text>標籤來保存一些格式的輸出XML但我不知道你是否對此感興趣。如果沒有,請隨時刪除它們。

它通過使用for-each來查找輸入XML中的元素。 select屬性起始處的//表示它可以匹配文檔中的任何位置,而不僅僅是在當前級別。

然後它簡單地使用copy-of來插入在for-each中找到的整個節點。

+0

史蒂夫,我的朋友很棒。非常感謝。 – ThomasMuller

相關問題