我是XSLT的新手,并且需要从现有的XML创建XML以插入某个产品的摘要。
XML的示例输入如下:
<?xml version="1.0" encoding="utf-8"?>
<BATCHES>
<BATCH>
<PACKNO>10C00302</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C00304</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C00301</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C00307</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C00300</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C02118</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C02117</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C02107</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C02109</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C02116</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10B00601</PACKNO>
<PRODUCT>000000000002222222</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,12,U,MD,ST,2440x1220,75,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1527.00</AMT>
<UNIT_PRICE>20.36</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10B00600</PACKNO>
<PRODUCT>000000000002222222</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,12,U,MD,ST,2440x1220,75,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1527.00</AMT>
<UNIT_PRICE>20.36</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10B01135</PACKNO>
<PRODUCT>000000000002222222</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,12,U,MD,ST,2440x1220,75,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1527.00</AMT>
<UNIT_PRICE>20.36</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10B05115</PACKNO>
<PRODUCT>000000000003333333</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,9,U,MD,ST,2440x1220,76,D</DESCRIPTION>
<QTY>2.036</QTY>
<UOM>PCS</UOM>
<AMT>1276.80</AMT>
<UNIT_PRICE>16.80</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10B05110</PACKNO>
<PRODUCT>000000000003333333</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,9,U,MD,ST,2440x1220,76,D</DESCRIPTION>
<QTY>2.036</QTY>
<UOM>PCS</UOM>
<AMT>1276.80</AMT>
<UNIT_PRICE>16.80</UNIT_PRICE>
</BATCH>
</BATCHES>
我想得到像这样的输出:
<?xml version="1.0" encoding="utf-8"?>
<BATCHES>
<SUMMARY>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<UOM>PCS</UOM>
<TOT_QTY>26.789999999999992</TOT_QTY>
<TOT_AMT>12205</TOT_AMT>
</SUMMARY>
<BATCH>
<PACKNO>10C00302</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C00304</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C00301</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C00307</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C00300</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C02118</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C02117</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C02107</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C02109</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10C02116</PACKNO>
<PRODUCT>000000000001111111</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,18,U,MD,ST,2440x1220,50,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1220.50</AMT>
<UNIT_PRICE>24.41</UNIT_PRICE>
</BATCH>
<SUMMARY>
<PRODUCT>000000000002222222</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,12,U,MD,ST,2440x1220,75,D</DESCRIPTION>
<UOM>PCS</UOM>
<TOT_QTY>8.036999999999999</TOT_QTY>
<TOT_AMT>4581</TOT_AMT>
</SUMMARY>
<BATCH>
<PACKNO>10B00601</PACKNO>
<PRODUCT>000000000002222222</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,12,U,MD,ST,2440x1220,75,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1527.00</AMT>
<UNIT_PRICE>20.36</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10B00600</PACKNO>
<PRODUCT>000000000002222222</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,12,U,MD,ST,2440x1220,75,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1527.00</AMT>
<UNIT_PRICE>20.36</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10B01135</PACKNO>
<PRODUCT>000000000002222222</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,12,U,MD,ST,2440x1220,75,D</DESCRIPTION>
<QTY>2.679</QTY>
<UOM>PCS</UOM>
<AMT>1527.00</AMT>
<UNIT_PRICE>20.36</UNIT_PRICE>
</BATCH>
<SUMMARY>
<PRODUCT>000000000003333333</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,9,U,MD,ST,2440x1220,76,D</DESCRIPTION>
<UOM>PCS</UOM>
<TOT_QTY>4.072</TOT_QTY>
<TOT_AMT>2553.6</TOT_AMT>
</SUMMARY>
<BATCH>
<PACKNO>10B05115</PACKNO>
<PRODUCT>000000000003333333</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,9,U,MD,ST,2440x1220,76,D</DESCRIPTION>
<QTY>2.036</QTY>
<UOM>PCS</UOM>
<AMT>1276.80</AMT>
<UNIT_PRICE>16.80</UNIT_PRICE>
</BATCH>
<BATCH>
<PACKNO>10B05110</PACKNO>
<PRODUCT>000000000003333333</PRODUCT>
<DESCRIPTION>BOARD,PLASTIC;P,9,U,MD,ST,2440x1220,76,D</DESCRIPTION>
<QTY>2.036</QTY>
<UOM>PCS</UOM>
<AMT>1276.80</AMT>
<UNIT_PRICE>16.80</UNIT_PRICE>
</BATCH>
</BATCHES>
到目前为止,我能够使用以下XSLT获取摘要,但在尝试和试验2天后无法进展。我想知道有人会那么善良帮助我。提前谢谢。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="BATCH" match="BATCH" use="DESCRIPTION" />
<xsl:template match="/">
<xsl:apply-templates select="BATCHES/BATCH[generate-id() = generate-id(key('BATCH', DESCRIPTION)[1])]" />
</xsl:template>
<xsl:template match="BATCH">
<ITEM>
<xsl:copy-of select="PRODUCT" />
<xsl:copy-of select="DESCRIPTION" />
<xsl:copy-of select="UOM" />
<TOT_QTY><xsl:value-of select="sum(key('BATCH', DESCRIPTION)/QTY)" /></TOT_QTY>
<TOT_AMT><xsl:value-of select="sum(key('BATCH', DESCRIPTION)/AMT)" /></TOT_AMT>
</ITEM>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
使用以下脚本:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:key name="BATCH" match="BATCH" use="DESCRIPTION" />
<xsl:template match="BATCHES">
<xsl:copy>
<xsl:for-each select=
"BATCH[generate-id()=generate-id(key('BATCH', DESCRIPTION)[1])]">
<xsl:variable name="group" select="key('BATCH', DESCRIPTION)"/>
<SUMMARY>
<xsl:copy-of select="PRODUCT" />
<xsl:copy-of select="DESCRIPTION" />
<xsl:copy-of select="UOM" />
<TOT_QTY><xsl:value-of select="sum($group/QTY)"/></TOT_QTY>
<TOT_AMT><xsl:value-of select="sum($group/AMT)"/></TOT_AMT>
</SUMMARY>
<xsl:copy-of select="$group"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:transform>
当主模板与BATCHES
匹配时,xsl:copy
复制此(主要)标签的名称。
使用xsl:for-each
执行对组的循环,但是
你需要整个组(不仅仅是它的第一个元素),
所以再次打电话给key
,将整个小组保存在group
变量
<SUMMARY>
标记打印组摘要。
最后要做的就是打印整个内容
小组(xsl:copy-of
)。