我有一个像这样的输入 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>