在根据条件对多个键进行分组时,我遇到了问题。
我有两个XML记录。文件1和文件2
目标是创建一个包含两个部分的表。同样,改变。
在两个文档中都有相同的rec_no、part_no、ext_qty的元素应该放在相同的小节中。元素,这些元素在两个文档中都有,但是随着rec_no、part_no、ext_qty中的更改,ext_qty应该在change下。
现在,我在这个条件下创建了不同的键。现在我的问题是如何分组密钥以获得结果。
这是输出表和我的注释。
---------------------------------------------------------
Section RecNo Desc Doc-1 Qty Doc-2 Qty Total
---------------------------------------------------------
Same 111 Desc1 1 1 100
---------------------------------------------------------
Same Total 100
---------------------------------------------------------
Change 222 Desc2 2 3 500
Change 333 Desc3 3 3 600
Change 444 Desc4 6 4 600
---------------------------------------------------------
Change Total 1300
---------------------------------------------------------
Grand Total 1400
---------------------------------------------------------我的XML是:
<Logia>
<DocHeader>
<Document>
<Downto>
<part_no>111</part_no>
<rec_no>aaa</rec_no>
<desc>Desc1</desc>
<ext_qty>1</ext_qty>
<mat_cost>100.00</mat_cost>
</Downto>
<Downto>
<part_no>222</part_no>
<rec_no>bbb</rec_no>
<desc>Desc2</desc>
<ext_qty>2</ext_qty>
<mat_cost>100.00</mat_cost>
</Downto>
<Downto>
<part_no>333</part_no>
<rec_no>ccc</rec_no>
<desc>Desc3</desc>
<ext_qty>3</ext_qty>
<mat_cost>100.00</mat_cost>
</Downto>
<Downto>
<part_no>444</part_no>
<rec_no>ddd</rec_no>
<desc>Desc4</desc>
<ext_qty>6</ext_qty>
<mat_cost>100.00</mat_cost>
</Downto>
</Document>
<Document>
<Downto>
<part_no>111</part_no>
<rec_no>aaa</rec_no>
<desc>Desc1</desc>
<ext_qty>1</ext_qty>
<mat_cost>100.00</mat_cost>
</Downto>
<Downto>
<part_no>222</part_no>
<rec_no>bbb</rec_no>
<desc>Desc3</desc>
<ext_qty>3</ext_qty>
<mat_cost>100.00</mat_cost>
</Downto>
<Downto>
<part_no>333</part_no>
<rec_no>bbb</rec_no>
<desc>Desc3</desc>
<ext_qty>3</ext_qty>
<mat_cost>100.00</mat_cost>
</Downto>
<Downto>
<part_no>444</part_no>
<rec_no>ddd</rec_no>
<desc>Desc4</desc>
<ext_qty>4</ext_qty>
<mat_cost>400.00</mat_cost>
</Downto>
</Document>
</DocHeader>
</Logia>我的XSLT是
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="MAT1PARTKEY" match="Document[1]/Downto" use="part_no" />
<xsl:key name="MAT2PARTKEY" match="Document[2]/Downto" use="part_no" />
<xsl:key name="MAT1RECKEY" match="Document[1]/Downto" use="rec_no" />
<xsl:key name="MAT2RECKEY" match="Document[2]/Downto" use="rec_no" />
<xsl:key name="MAT1QTYKEY" match="Document[1]/Downto" use="ext_qty" />
<xsl:key name="MAT2QTYKEY" match="Document[2]/Downto" use="ext_qty" />
<xsl:key name="MAT1COSTKEY" match="Document[1]/Downto" use="mat_cost" />
<xsl:key name="MAT2COSTKEY" match="Document[2]/Downto" use="mat_cost" />
<xsl:key name="MATERIALBYPARTNO" match="Document/Downto" use="part_no" />
<xsl:template match="/Logia/DocHeader">
<table border="1">
<!-- header -->
<tr>
<th>Section</th>
<th>PartNo</th>
<th>RecNo</th>
<th>Desc</th>
<th>Doc-1 Qty</th>
<th>Doc-2 Qty</th>
<th> Total</th>
</tr>
<!-- same -->
<xsl:variable name="same" select="Document[1]/Downto[key('MAT2RECKEY', part_no]" />
<xsl:apply-templates select="$same">
<xsl:with-param name="section">Same</xsl:with-param>
</xsl:apply-templates>
<xsl:variable name="same-total" select="sum($same/mat_cost)" />
<tr>
<td colspan="6">Same Total</td>
<th><xsl:value-of select="$same-total"/></th>
</tr>
<!-- change -->
<xsl:variable name="change" select="Document[1]/Downto[not(key('MAT2RECKEY', rec_no ))]" />
<xsl:apply-templates select="$change">
<xsl:with-param name="section">Change</xsl:with-param>
</xsl:apply-templates>
<xsl:variable name="change-total" select="sum($change/mat_cost)" />
<tr>
<td colspan="6">Change Total</td>
<th><xsl:value-of select="$change-total"/></th>
</tr>
<!-- grand total -->
<tr>
<th colspan="6">Grand Total</th>
<th><xsl:value-of select="$same-total + $add-total + $delete-total"/></th>
</tr>
</table>
</xsl:template>
<xsl:template match="Downto">
<xsl:param name="section"/>
<xsl:if test="generate-id() = generate-id(key('MATERIALBYPARTNO', part_no)[1])">
<tr>
<td><xsl:value-of select="$section"/></td>
<td><xsl:value-of select="part_no"/></td>
<td><xsl:value-of select="rec_no"/></td>
<td><xsl:value-of select="desc"/></td>
<td><xsl:value-of select="sum(key('MAT1PARTKEY', part_no)/ext_qty)"/></td>
<td><xsl:value-of select="sum(key('MAT2PARTKEY', part_no)/ext_qty)"/></td>
<td><xsl:value-of select="sum(key('MATERIALBYPARTNO', part_no)/mat_cost)"/></td>
</tr>
</xsl:if>
</xsl:template>
</xsl:stylesheet>任何帮助都是非常感谢的。
发布于 2014-11-28 13:29:18
按数量和成本计算的钥匙是无用的。
你可以构造任何复杂的密钥。在你的情况下:
<xsl:key name="downtoByKeyFields" match="Downto" use="concat(rec_no, '||', part_no, '||', ext_qty)"/>当你调用这个键时,你应该做同样的事情:
key('downtoByKeyFields', concat(rec_no, '||', part_no, '||', ext_qty))此键将返回具有给定组合的rec_no、part_no、ext_qty值的所有Downto字段。
附注:如果这些值中没有一个包含x :),则此操作应该有效。)
主要思想:
<xsl:key name="MATERIAL2BYKEY" match="Document[2]/Downto" use="concat(rec_no, '||', part_no, '||', ext_qty)" />
...
<xsl:variable name="same" select="Document[1]/Downto[key('MATERIAL2BYKEY', concat(rec_no, '||', part_no, '||', ext_qty))]" />
...
<xsl:variable name="change" select="Document[1]/Downto[not(key('MATERIAL2BYKEY', concat(rec_no, '||', part_no, '||', ext_qty)))]" /> 这是非常简单的:具有相同的rec_no、part_no、ext_qty的元素在键方面是将所有元素与相同的rec_no、part_no、ext_qty匹配的键。我们需要指定表达式,告诉xslt转换器,两个元素具有相同的rec_no、part_no、ext_qty -concat(例如,rec_no、“Some分离器”、part_no、“Some分离器”、ext_qty)。
https://stackoverflow.com/questions/27188192
复制相似问题