我有一个这样的输入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 中添加了额外的重复行。哪些不是必需的。对此有任何建议。
预期输出 -
<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>
因为你的问题只有一个"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>