多场分组

时间:2010-05-07 12:59:03

标签: xslt xslt-2.0

我正在从数据库生成XML文件,如下所示......

<?xml version = '1.0'?>
<T0019>
   <IFTA_ACCOUNT>
      <IFTA_CARRIER_ID_NUMBER>705</IFTA_CARRIER_ID_NUMBER>
      <IFTA_LICENSE_NUMBER>631227666</IFTA_LICENSE_NUMBER>
      <IFTA_BASE_COUNTRY>US</IFTA_BASE_COUNTRY>
      <IFTA_BASE_STATE>AL</IFTA_BASE_STATE>
      <IFTA_STATUS_CODE>0  </IFTA_STATUS_CODE>
      <IFTA_STATUS_DATE>2009-01-01</IFTA_STATUS_DATE>
      <IFTA_ISSUE_DATE>2009-01-01</IFTA_ISSUE_DATE>
      <IFTA_EXPIRE_DATE>2009-12-01</IFTA_EXPIRE_DATE>
      <IFTA_UPDATE_DATE>2008-12-30</IFTA_UPDATE_DATE>
      <NAME_TYPE>LG</NAME_TYPE>
      <NAME>K D L TRUCKING INC</NAME>
      <ADDRESS_TYPE>PH</ADDRESS_TYPE>
      <STREET_LINE_1>200 MARTIN LANE</STREET_LINE_1>
      <CITY>OHATCHEE</CITY>
      <STATE>AL</STATE>
      <ZIP_CODE>36271</ZIP_CODE>     
      <COUNTY>CALHOUN COUNTY</COUNTY>
      <COUNTRY>US</COUNTRY>
  </IFTA_ACCOUNT>

   <IFTA_ACCOUNT>
      <IFTA_CARRIER_ID_NUMBER>705</IFTA_CARRIER_ID_NUMBER>
      <IFTA_LICENSE_NUMBER>631227666</IFTA_LICENSE_NUMBER>
      <IFTA_BASE_COUNTRY>US</IFTA_BASE_COUNTRY>
      <IFTA_BASE_STATE>AL</IFTA_BASE_STATE>
      <IFTA_STATUS_CODE>0  </IFTA_STATUS_CODE>
      <IFTA_STATUS_DATE>2009-01-01</IFTA_STATUS_DATE>
      <IFTA_ISSUE_DATE>2009-01-01</IFTA_ISSUE_DATE>
      <IFTA_EXPIRE_DATE>2009-12-01</IFTA_EXPIRE_DATE>
      <IFTA_UPDATE_DATE>2008-12-30</IFTA_UPDATE_DATE>
      <NAME_TYPE>LG</NAME_TYPE>
      <NAME>K D L TRUCKING INC</NAME>
      <ADDRESS_TYPE>MA</ADDRESS_TYPE>
      <STREET_LINE_1>200 MARTIN LANE</STREET_LINE_1>
      <CITY>OHATCHEE</CITY>
      <STATE>AL</STATE>
      <ZIP_CODE>36271</ZIP_CODE>
      <COUNTRY>US</COUNTRY>
    </IFTA_ACCOUNT>    
</T0019>

我从生成的XSLT中获取了前两条记录。 随着XSLT的使用,我已经尝试了很多基于的记录 IFTA_LICENSE_NUMBER,IFTA_BASE_COUNTRY,IFTA_BASE_ST ATE,NAME_TYPE,ADDRESS_TYPE,但我没有像这样生成XML ..

<?xml version="1.0" encoding="UTF-8" ?> 
 <T0019>
    <IFTA_ACCOUNT>
      <IFTA_CARRIER_ID_NUMBER>705</IFTA_CARRIER_ID_NUMBER> 
      <IFTA_BASE_COUNTRY>US</IFTA_BASE_COUNTRY> 
      <IFTA_BASE_STATE>AL</IFTA_BASE_STATE> 
      <IFTA_LICENSE_NUMBER>631227666</IFTA_LICENSE_NUMBER> 
      <IFTA_STATUS_CODE>0</IFTA_STATUS_CODE> 
      <IFTA_STATUS_DATE>2009-01-01</IFTA_STATUS_DATE> 
      <IFTA_ISSUE_DATE>2009-01-01</IFTA_ISSUE_DATE> 
      <IFTA_EXPIRE_DATE>2009-12-01</IFTA_EXPIRE_DATE> 
      <IFTA_UPDATE_DATE>2008-12-30</IFTA_UPDATE_DATE> 
     <IFTA_NAME>
      <NAME_TYPE>LG</NAME_TYPE> 
      <NAME>K D L TRUCKING INC</NAME> 
         <IFTA_ADDRESS>
              <ADDRESS_TYPE>PH</ADDRESS_TYPE> 
              <STREET_LINE_1>200 MARTIN LANE</STREET_LINE_1> 
              <STREET_LINE_2 /> 
              <CITY>OHATCHEE</CITY> 
              <STATE>AL</STATE> 
              <ZIP_CODE>36271</ZIP_CODE> 
              <COUNTY>CALHOUN COUNTY</COUNTY> 
              <COUNTRY>US</COUNTRY> 
          </IFTA_ADDRESS>
         <IFTA_ADDRESS>
              <ADDRESS_TYPE>MA</ADDRESS_TYPE> 
              <STREET_LINE_1>200 MARTIN LANE</STREET_LINE_1> 
              <STREET_LINE_2 /> 
              <CITY>OHATCHEE</CITY> 
              <STATE>AL</STATE> 
              <ZIP_CODE>36271</ZIP_CODE> 
              <COUNTY /> 
              <COUNTRY>US</COUNTRY> 
          </IFTA_ADDRESS>
      </IFTA_NAME>
  </IFTA_ACCOUNT>
   </T0019>

2 个答案:

答案 0 :(得分:0)

您的示例输入记录有两个不同的ADDRESS_TYPE值,因此如果您按ADDRESS_TYPE进行分组,则这两个值将是不同的。由于您的输出还显示了两个不同的值,我假设您并不真正想要分组键列表中的ADDRESS_TYPE。这是真正的问题吗?

The XSLT spec为复合键提供了两种策略。当我使用您的数据尝试'concat'策略时,以及您的密钥列表减去ADDRESS_TYPE,它将这两个记录组合在一起。

答案 1 :(得分:0)

谢谢,我已经应用了以下XSLT来解决这个问题。

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

    <xsl:template match="T0019">
        <xsl:copy>
            <xsl:for-each-group select="IFTA_ACCOUNT" group-by="IFTA_LICENSE_NUMBER">
                <xsl:for-each-group select="current-group()" group-by="IFTA_BASE_COUNTRY">
                    <xsl:for-each-group select="current-group()" group-by="IFTA_BASE_STATE">
                        <IFTA_ACCOUNT>
                            <xsl:apply-templates select="IFTA_CARRIER_ID_NUMBER|IFTA_BASE_COUNTRY|IFTA_BASE_STATE|IFTA_LICENSE_NUMBER|IFTA_STATUS_CODE|IFTA_STATUS_DATE|IFTA_ISSUE_DATE|IFTA_EXPIRE_DATE|IFTA_UPDATE_DATE"/>
                            <xsl:for-each-group select="current-group()" group-by="NAME_TYPE">
                                <IFTA_NAME>
                                    <xsl:apply-templates select="NAME_TYPE|NAME"/>
                                    <xsl:for-each select="current-group()">
                                        <IFTA_ADDRESS>
                                            <xsl:apply-templates select="ADDRESS_TYPE|STREET_LINE_1|STREET_LINE_2|CITY|STATE|ZIP_CODE|COUNTY|COUNTRY"/>                                            
                                        </IFTA_ADDRESS>                                        
                                    </xsl:for-each>                                    
                                </IFTA_NAME>
                            </xsl:for-each-group>                            
                        </IFTA_ACCOUNT>                        
                    </xsl:for-each-group>                    
                </xsl:for-each-group>  
            </xsl:for-each-group>
        </xsl:copy>

    </xsl:template>
    <xsl:template match="IFTA_CARRIER_ID_NUMBER">
        <IFTA_CARRIER_ID_NUMBER>
            <xsl:value-of select="."/>
        </IFTA_CARRIER_ID_NUMBER>            
    </xsl:template>       
    <xsl:template match="IFTA_LICENSE_NUMBER">
        <IFTA_LICENSE_NUMBER>
            <xsl:value-of select="."/>
        </IFTA_LICENSE_NUMBER>            
    </xsl:template>   
    <xsl:template match="IFTA_BASE_COUNTRY">
        <IFTA_BASE_COUNTRY>
            <xsl:value-of select="."/>
        </IFTA_BASE_COUNTRY>                        
    </xsl:template>   
    <xsl:template match="IFTA_BASE_STATE">
        <IFTA_BASE_STATE>
            <xsl:value-of select="."/>
        </IFTA_BASE_STATE>            
    </xsl:template>   
    <xsl:template match="IFTA_STATUS_CODE">
        <IFTA_STATUS_CODE>
            <xsl:value-of select="."/>
        </IFTA_STATUS_CODE>            
    </xsl:template>   
    <xsl:template match="IFTA_STATUS_DATE">
        <IFTA_STATUS_DATE>
            <xsl:value-of select="."/> 
        </IFTA_STATUS_DATE>            
    </xsl:template>   
    <xsl:template match="IFTA_ISSUE_DATE">
        <IFTA_ISSUE_DATE>
            <xsl:value-of select="."/>
        </IFTA_ISSUE_DATE>            
    </xsl:template>
    <xsl:template match="IFTA_EXPIRE_DATE">
        <IFTA_STATUS_DATE>
            <xsl:value-of select="."/> 
        </IFTA_STATUS_DATE>            
    </xsl:template>   
    <xsl:template match="IFTA_UPDATE_DATE">
        <IFTA_ISSUE_DATE>
            <xsl:value-of select="."/>
        </IFTA_ISSUE_DATE>            
    </xsl:template> 
    <xsl:template match="NAME_TYPE">
        <NAME_TYPE>
            <xsl:value-of select="."/>
        </NAME_TYPE>            
    </xsl:template>
    <xsl:template match="NAME">
        <NAME_TYPE>
            <xsl:value-of select="."/>
        </NAME_TYPE>            
    </xsl:template>     
    <xsl:template match="ADDRESS_TYPE">
        <ADDRESS_TYPE>
            <xsl:value-of select="."/>
        </ADDRESS_TYPE>            
    </xsl:template>

    <xsl:template match="STREET_LINE_1">
        <STREET_LINE_1>
            <xsl:value-of select="."/>
        </STREET_LINE_1>            
    </xsl:template>
    <xsl:template match="STREET_LINE_2">
        <STREET_LINE_2>
            <xsl:value-of select="."/>
        </STREET_LINE_2>            
    </xsl:template>
    <xsl:template match="CITY">
        <CITY>
            <xsl:value-of select="."/> 
        </CITY>            
    </xsl:template>   
    <xsl:template match="STATE">
        <STATE>
            <xsl:value-of select="."/>
        </STATE>            
    </xsl:template> 
    <xsl:template match="ZIP_CODE">
        <ZIP_CODE>
            <xsl:value-of select="."/>
        </ZIP_CODE>            
    </xsl:template>  
    <xsl:template match="COUNTY">
        <COUNTY>
            <xsl:value-of select="."/>
        </COUNTY>            
    </xsl:template>
    <xsl:template match="COUNTRY">
        <COUNTRY>
            <xsl:value-of select="."/>
        </COUNTRY>            
    </xsl:template>    
</xsl:stylesheet>

感谢大家的帮助。