在输出xml中出现额外的行

时间:2013-10-07 05:33:17

标签: xml xslt xslt-1.0

我有一个像这样的输入xml -

<SAMPLE>
<SAMPLE_ID>111</SAMPLE_ID>
<ROWS>
                    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="1">                                               
                        <PRICE_PER_UNIT>
                            <AMOUNT SIGN="+" VAT="INCLUDED">1025.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">949.07</AMOUNT>
                        </PRICE_PER_UNIT>
                        <ROW_TOTAL>
                            <AMOUNT SIGN="+" VAT="INCLUDED">1025.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">949.07</AMOUNT>
                        </ROW_TOTAL>                            
                    </ROW>
                    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="1">                                                       
                        <PRICE_PER_UNIT>
                            <AMOUNT SIGN="+" VAT="INCLUDED">67.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">53.60</AMOUNT>
                        </PRICE_PER_UNIT>
                        <ROW_TOTAL>
                            <AMOUNT SIGN="+" VAT="INCLUDED">67.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">53.60</AMOUNT>
                        </ROW_TOTAL>                            
                    </ROW>
                    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="1">                                                       
                        <PRICE_PER_UNIT>
                            <AMOUNT SIGN="+" VAT="INCLUDED">3.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">2.40</AMOUNT>
                        </PRICE_PER_UNIT>
                        <ROW_TOTAL>
                            <AMOUNT SIGN="+" VAT="INCLUDED">3.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">2.40</AMOUNT>
                        </ROW_TOTAL>                            
                    </ROW>
                    <ROW ROW_TYPE="SUBTOTAL" ROW_ID="1">                                                        
                        <PRICE_PER_UNIT>
                            <AMOUNT SIGN="+" VAT="INCLUDED">1095.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">0.00</AMOUNT>
                        </PRICE_PER_UNIT>
                        <ROW_TOTAL>
                            <AMOUNT SIGN="+" VAT="INCLUDED">1095.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">0.00</AMOUNT>
                        </ROW_TOTAL>                            
                    </ROW>
                    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="2">                                                       
                        <PRICE_PER_UNIT>
                            <AMOUNT SIGN="+" VAT="INCLUDED">315.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">291.67</AMOUNT>
                        </PRICE_PER_UNIT>
                        <ROW_TOTAL>
                            <AMOUNT SIGN="+" VAT="INCLUDED">630.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">583.33</AMOUNT>
                        </ROW_TOTAL>                            
                    </ROW>
                    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="2">                                                       
                        <PRICE_PER_UNIT>
                            <AMOUNT SIGN="+" VAT="INCLUDED">30.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">24.00</AMOUNT>
                        </PRICE_PER_UNIT>
                        <ROW_TOTAL>
                            <AMOUNT SIGN="+" VAT="INCLUDED">60.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">48.00</AMOUNT>
                        </ROW_TOTAL>                            
                    </ROW>
                    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="2">                           
                        <PRICE_PER_UNIT>
                            <AMOUNT SIGN="+" VAT="INCLUDED">10.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">8.00</AMOUNT>
                        </PRICE_PER_UNIT>
                        <ROW_TOTAL>
                            <AMOUNT SIGN="+" VAT="INCLUDED">20.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">16.00</AMOUNT>
                        </ROW_TOTAL>                            
                    </ROW>
                    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="2">                           
                        <PRICE_PER_UNIT>
                            <AMOUNT SIGN="+" VAT="INCLUDED">175.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">140.00</AMOUNT>
                        </PRICE_PER_UNIT>
                        <ROW_TOTAL>
                            <AMOUNT SIGN="+" VAT="INCLUDED">350.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">280.00</AMOUNT>
                        </ROW_TOTAL>                            
                    </ROW>
                    <ROW ROW_TYPE="SUBTOTAL" ROW_ID="2">                            
                        <PRICE_PER_UNIT>
                            <AMOUNT SIGN="+" VAT="INCLUDED">530.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">0.00</AMOUNT>
                        </PRICE_PER_UNIT>
                        <ROW_TOTAL>
                            <AMOUNT SIGN="+" VAT="INCLUDED">1060.00</AMOUNT>
                            <AMOUNT SIGN="+" VAT="EXCLUDED">0.00</AMOUNT>
                        </ROW_TOTAL>                            
                    </ROW>                  
                </ROWS>

我的转换看起来像这样 -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="kRowById" match="ROW[not(@ROW_TYPE='SUBTOTAL' or @ROW_TYPE='MAIN' or @ROW_TYPE='COMMENT')]" use="@ROW_ID"/>
    <xsl:template match="ROW[not(@ROW_TYPE='SPECIFICATION')]">
        <xsl:copy-of select="."/>
        <xsl:copy-of select="key('kRowById', @ROW_ID)"/>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

我的输出结构正确但唯一的问题是,这个逻辑是在输出xml中添加额外的重复ROWS。 这不是必需的。对此有任何建议。

预期产出 -

<SAMPLE>
    <ROW ROW_TYPE="SUBTOTAL" ROW_ID="1">        
        <PRICE_PER_UNIT>
            <AMOUNT SIGN="+" VAT="INCLUDED">1095.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">0.00</AMOUNT>
        </PRICE_PER_UNIT>
        <ROW_TOTAL>
            <AMOUNT SIGN="+" VAT="INCLUDED">1095.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">0.00</AMOUNT>
        </ROW_TOTAL>        
    </ROW>
    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="1">       
        <PRICE_PER_UNIT>
            <AMOUNT SIGN="+" VAT="INCLUDED">1025.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">949.07</AMOUNT>
        </PRICE_PER_UNIT>
        <ROW_TOTAL>
            <AMOUNT SIGN="+" VAT="INCLUDED">1025.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">949.07</AMOUNT>
        </ROW_TOTAL>        
    </ROW>
    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="1">       
        <PRICE_PER_UNIT>
            <AMOUNT SIGN="+" VAT="INCLUDED">67.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">53.60</AMOUNT>
        </PRICE_PER_UNIT>
        <ROW_TOTAL>
            <AMOUNT SIGN="+" VAT="INCLUDED">67.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">53.60</AMOUNT>
        </ROW_TOTAL>        
    </ROW>
    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="1">       
        <PRICE_PER_UNIT>
            <AMOUNT SIGN="+" VAT="INCLUDED">3.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">2.40</AMOUNT>
        </PRICE_PER_UNIT>
        <ROW_TOTAL>
            <AMOUNT SIGN="+" VAT="INCLUDED">3.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">2.40</AMOUNT>
        </ROW_TOTAL>        
    </ROW>
    <ROW ROW_TYPE="SUBTOTAL" ROW_ID="2">        
        <PRICE_PER_UNIT>
            <AMOUNT SIGN="+" VAT="INCLUDED">530.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">0.00</AMOUNT>
        </PRICE_PER_UNIT>
        <ROW_TOTAL>
            <AMOUNT SIGN="+" VAT="INCLUDED">1060.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">0.00</AMOUNT>
        </ROW_TOTAL>        
    </ROW>
    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="2">       
        <PRICE_PER_UNIT>
            <AMOUNT SIGN="+" VAT="INCLUDED">315.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">291.67</AMOUNT>
        </PRICE_PER_UNIT>
        <ROW_TOTAL>
            <AMOUNT SIGN="+" VAT="INCLUDED">630.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">583.33</AMOUNT>
        </ROW_TOTAL>        
    </ROW>
    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="2">       
        <PRICE_PER_UNIT>
            <AMOUNT SIGN="+" VAT="INCLUDED">30.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">24.00</AMOUNT>
        </PRICE_PER_UNIT>
        <ROW_TOTAL>
            <AMOUNT SIGN="+" VAT="INCLUDED">60.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">48.00</AMOUNT>
        </ROW_TOTAL>        
    </ROW>
    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="2">       
        <PRICE_PER_UNIT>
            <AMOUNT SIGN="+" VAT="INCLUDED">10.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">8.00</AMOUNT>
        </PRICE_PER_UNIT>
        <ROW_TOTAL>
            <AMOUNT SIGN="+" VAT="INCLUDED">20.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">16.00</AMOUNT>
        </ROW_TOTAL>        
    </ROW>
    <ROW ROW_TYPE="SPECIFICATION" ROW_ID="2">       
        <PRICE_PER_UNIT>
            <AMOUNT SIGN="+" VAT="INCLUDED">175.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">140.00</AMOUNT>
        </PRICE_PER_UNIT>
        <ROW_TOTAL>
            <AMOUNT SIGN="+" VAT="INCLUDED">350.00</AMOUNT>
            <AMOUNT SIGN="+" VAT="EXCLUDED">280.00</AMOUNT>
        </ROW_TOTAL>        
    </ROW>
</SAMPLE>

1 个答案:

答案 0 :(得分:0)

因为你的问题只有一个“SAMPLE”元素,所以你想要它看多个“SAMPLE”元素并不完全清楚,但我想你可能需要扩展你的xsl:key来包含SAMPLE_ID元素

<xsl:key name="kRowById" 
         match="ROW[not(@ROW_TYPE='SUBTOTAL' or @ROW_TYPE='MAIN' or @ROW_TYPE='COMMENT')]"
         use="concat(@ROW_ID, '|', ../../SAMPLE_ID)"/>

因此,密钥使用ROW_ID和SAMPLE_ID。注意使用|字符作为分隔符;它可以是任何字符,只要它不出现在ROW_ID或SAMPLE_ID中。

然后,在获取SAMPLE的ROW元素时,在现有xsl:copy中使用相同的键。

<xsl:copy-of select="key('kRowById', concat(@ROW_ID, '|', ../../SAMPLE_ID))"/>

试试这个XSLT

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

    <xsl:key name="kRowById" match="ROW[not(@ROW_TYPE='SUBTOTAL' or @ROW_TYPE='MAIN' or @ROW_TYPE='COMMENT')]" use="concat(@ROW_ID, '|', ../../SAMPLE_ID)"/>

    <xsl:template match="ROW[not(@ROW_TYPE='SPECIFICATION')]">
        <xsl:copy-of select="."/>
        <xsl:copy-of select="key('kRowById', concat(@ROW_ID, '|', ../../SAMPLE_ID))"/>
    </xsl:template>

    <xsl:template match="text()"/>

    <xsl:template match="SAMPLE">
      <xsl:copy>
        <xsl:apply-templates />
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>