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

1 回答 1

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>
于 2013-10-08T07:31:39.157 回答