XML - 使用XSLT 2组合不同的标签

时间:2013-03-19 10:48:53

标签: xml xslt-2.0 xslt-grouping

我已经在这里询问过类似的文件(XML - combine tags to children with XSLT)。不幸的是,我的XML已经增长,所以我再次需要你的帮助。

<?xml version="1.0"?>                                                                                                                                 
<ROWSET>                                                                                                                                              
 <ROW>                                                                                                                                                
  <KUNDENNR>63564</KUNDENNR>                                                                                                                          
  <JAHR>2012</JAHR>                                                                                                                                   
  <MONAT>2</MONAT>                                                                                                                                    
  <NAME>John Doe</NAME>                                                                                                                       
  <NETTO>504,66</NETTO>                                                                                                                               
  <DB_BASIS>21,56</DB_BASIS>                                                                                                                          
  <EKECHT>482,56</EKECHT>                                                                                                                             
  <NAME1>Some Name</NAME1>                                                                                                                       
  <NAME2>BA 31 (BÜRO GGR 1)</NAME2>                                                                                                                   
  <LAND>AT</LAND>                                                                                                                                     
  <PLZ>1082</PLZ>                                                                                                                                     
  <ORT>Wien</ORT>                                                                                                                                     
  <ADM>Henry Ford</ADM>                                                                                                                          
  <KUNDENKLASSE>A</KUNDENKLASSE>                                                                                                                      
  <UEBERKUNDE>Some Value</UEBERKUNDE>                                                                                                             
  <HANDLING>22,577179011</HANDLING>                                                                                                                   
  <SOLLFRACHT>22,68</SOLLFRACHT>              
  <DG_BASIS_P>-10,763888888888888888888888888888888889</DG_BASIS_P>                                                                                   
 </ROW> 
 <ROW>                                                                                                                                                
  <KUNDENNR>63564</KUNDENNR>                                                                                                                          
  <JAHR>2011</JAHR>                                                                                                                                   
  <MONAT>1</MONAT>                                                                                                                                    
  <NAME>John Doe</NAME>                                                                                                                       
  <NETTO>502,66</NETTO>                                                                                                                               
  <DB_BASIS>21,56</DB_BASIS>                                                                                                                          
  <EKECHT>482,56</EKECHT>                                                                                                                             
  <NAME1>Some Name</NAME1>                                                                                                                       
  <NAME2>BA 31 (BÜRO GGR 1)</NAME2>                                                                                                                   
  <LAND>AT</LAND>                                                                                                                                     
  <PLZ>1082</PLZ>                                                                                                                                     
  <ORT>Wien</ORT>                                                                                                                                     
  <ADM>Henry Ford</ADM>                                                                                                                          
  <KUNDENKLASSE>A</KUNDENKLASSE>                                                                                                                      
  <UEBERKUNDE>Some value</UEBERKUNDE>                                                                                                             
  <HANDLING>22,577179011</HANDLING>                                                                                                                   
  <SOLLFRACHT>22,68</SOLLFRACHT>              
  <DG_BASIS_P>-10,538888888888888888889</DG_BASIS_P>
  <HGID>342</HGID>
  <HGNAME>Some product name</HGNAME>                                                                       
 </ROW> 
 <ROW>                                                                                                                                                
  <KUNDENNR>63564</KUNDENNR>                                                                                                                          
  <JAHR>2011</JAHR>                                                                                                                                   
  <MONAT>1</MONAT>                                                                                                                                    
  <NAME>John Doe</NAME>                                                                                                                       
  <NETTO>217,66</NETTO>                                                                                                                               
  <DB_BASIS>21,56</DB_BASIS>                                                                                                                          
  <EKECHT>482,56</EKECHT>                                                                                                                             
  <NAME1>Some Name</NAME1>                                                                                                                       
  <NAME2>BA 31 (BÜRO GGR 1)</NAME2>                                                                                                                   
  <LAND>AT</LAND>                                                                                                                                     
  <PLZ>1082</PLZ>                                                                                                                                     
  <ORT>Wien</ORT>                                                                                                                                     
  <ADM>Henry Ford</ADM>                                                                                                                          
  <KUNDENKLASSE>A</KUNDENKLASSE>                                                                                                                      
  <UEBERKUNDE>Some value</UEBERKUNDE>                                                                                                             
  <HANDLING>22,577179011</HANDLING>                                                                                                                   
  <SOLLFRACHT>22,68</SOLLFRACHT>              
  <DG_BASIS_P>-10,538888888888888888889</DG_BASIS_P>
  <HGID>342</HGID>
  <HGNAME>Some product name</HGNAME>                                                                       
 </ROW> 
</ROWSET>

已添加字段HGID和HGNAME,最终的XML输出应如下所示:

<?xml version="1.0"?>                                                                                                                                 
<ROWSET>                                                                                                                                              
 <KUNDE>
  <KUNDENNR>63564</KUNDENNR>                                                                                                                          
  <NAME>John Doe</NAME>      
  <NAME1>Some Name</NAME1>                                                                                                                       
  <NAME2>BA 31 (B RO GGR 1)</NAME2>                                                                                                                   
  <LAND>AT</LAND>                                                                                                                                     
  <PLZ>1082</PLZ>                                                                                                                                     
  <ORT>Wien</ORT>                                                                                                                                     
  <ADM>Henry Ford</ADM>                                                                                                                          
  <KUNDENKLASSE>A</KUNDENKLASSE>    
  <UEBERKUNDE>Some Value</UEBERKUNDE>                                                                                                              
    <ROW>                                                                                                                                                
        <JAHR>2012</JAHR>                                                                                                                                   
        <MONAT>2</MONAT>                                                                                                                                                                                                                                                                   
        <DB_BASIS>21,56</DB_BASIS>                                                                                                                          
        <EKECHT>482,56</EKECHT>                                                                                                                             
        <HANDLING>22,577179011</HANDLING>                                                                                                                   
        <SOLLFRACHT>22,68</SOLLFRACHT>                                                                                                                      
        <DG_BASIS_P>4,27218325209051638727063765703642056038</DG_BASIS_P>
        <WGROUPITEM>
            <HGID>125</HGID>
            <HGNAME>Another product name</HGNAME>
            <NETTO>504,66</NETTO>
        </WGROUPITEM>                                                                                   
    </ROW>
    <ROW>
        <JAHR>2012</JAHR>                                                                                                                                   
        <MONAT>1</MONAT>                                                                                                                                    
        <NETTO>502,66</NETTO>                                                                                                                              
        <DB_BASIS>21,56</DB_BASIS>                                                                                                                        
        <EKECHT>482,56</EKECHT>                                                                                                                           
        <HANDLING>22,577179011</HANDLING>                                                                                                                    
        <SOLLFRACHT>22,68</SOLLFRACHT>                                                                                                                      
        <DG_BASIS_P>-10,538888888888888888889</DG_BASIS_P> 
        <WGROUPITEM>
            <HGID>359</HGID>
            <HGNAME>Some other product name</HGNAME> 
            <NETTO>502,66</NETTO>                                                                                    
        </WGROUPITEM>
        <WGROUPITEM>
            <HGID>342</HGID>
            <HGNAME>Some product name</HGNAME> 
            <NETTO>217,66</NETTO>
        </WGROUPITEM>
    </ROW>  
 </KUNDE> 
 </ROWSET>

所以基本上我需要通过MONAT和JAHR(月和年)对那些HGNAME,HGID和NETTO字段进行分组。 我试过修改前面提到的帖子中的XSL文件,但我没有运气。我有一个问题,我无法通过MONAT和JAHR找到如何正确分组,我总是得到所有记录或者没有: - (。

编辑: 另外,我想找到类似的UEBERKUNDE条目并将它们分组为父级,所以它看起来像

<ROWSET>
<UEBERKUNDE>
    <NAME>Some value</NAME>
    <KUNDE>
        <KUNDENNR>63564</KUNDENNR>
        .......
   </KUNDE>
</UEBERKUNDE>

如果有人可以帮助我,那将是非常棒的。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我终于找到了如何做到并希望分享我的解决方案...... 这是最终的xslt。请注意,某些名称已更改。我还将NETTO,DB_BASIS等字段移到了新的WGROUPITEM子组中。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="rowsByMonth" match="ROW" use="concat(MONAT, '+', JAHR, '+', KUNDENNR)"></xsl:key>

<xsl:template match="ROWSET">

    <ROWSET>
        <xsl:for-each-group select="ROW" group-by="UKID">
            <UEBERKUNDE>
                <NAME><xsl:value-of select="UEBERKUNDE" /></NAME>
                <xsl:copy-of select="UKID" />
                <xsl:for-each-group select="current-group()" group-by="KUNDENNR">
                    <KUNDE>
                        <xsl:variable name="currentKdnr" select="KUNDENNR"/>
                        <xsl:copy-of select="KUNDENNR" />
                        <xsl:copy-of select="KNAME1" />
                        <xsl:copy-of select="KNAME2" />
                        <xsl:copy-of select="KNAME3" />
                        <xsl:copy-of select="LAND" />
                        <xsl:copy-of select="PLZ" />
                        <xsl:copy-of select="ORT" />
                        <xsl:copy-of select="ADM" />
                        <xsl:copy-of select="KUNDENKLASSE" />

                        <xsl:for-each select="current-group()[count(. | key('rowsByMonth', concat(MONAT,'+',JAHR,'+',KUNDENNR))[1]) = 1]">
                            <ROW>
                                <xsl:copy-of select="JAHR" />
                                <xsl:copy-of select="MONAT" />
                                <xsl:copy-of select="HANDLING" />
                                <xsl:copy-of select="SOLLFRACHT" />
                                <xsl:for-each select="key('rowsByMonth', concat(MONAT,'+',JAHR,'+',KUNDENNR))">
                                    <WGROUPITEM>
                                        <xsl:copy-of select="HGNAME" />
                                        <xsl:copy-of select="HGID" />
                                        <xsl:copy-of select="DG_BASIS" />
                                        <xsl:copy-of select="EKECHT" />
                                        <xsl:copy-of select="DB_BASIS" />
                                        <xsl:copy-of select="NETTO" />
                                    </WGROUPITEM>
                                </xsl:for-each>
                            </ROW>
                        </xsl:for-each>
                    </KUNDE>
                </xsl:for-each-group>
            </UEBERKUNDE>
        </xsl:for-each-group>
    </ROWSET>
</xsl:template></xsl:stylesheet>

非常感谢。