我是XSLT的新手,我在计算表中的子节总数时遇到问题。我正在使用XSLT 1.0。我能够从XML中获得Grand Total,但是获取子部分有很多ifs / else和条件很难解释。 请在下面找到示例表和XML。
因此,我要做的是将文档1与文档2进行比较。 如果Recno在两个文档中都匹配,则那些记录进入"相同"。 如果Rec1在Document1中而不在Document2中,那么它将转到" Add"部分。 如果Rec2在Document2中而不在Document1中,那么它将转到"删除"部分。
我到处尝试搜索但到目前为止没有运气。赞赏任何指针
我的输出HTML表格应如下所示:
----------------------------------------------------------------
Section RecNo Desc Qty Value
----------------------------------------------------------------
Same 111 Desc1 1 $100.00
Same 444 Desc1 1 $200.00
Same 123 Desc1 1 $300.00
---------------------------------------------------------------
Same Total $600.00
---------------------------------------------------------------
Add 555 Desc1 1 $100.00
Add 999 Desc1 1 $100.00
---------------------------------------------------------------
Add Total $200.00
---------------------------------------------------------------
Delete 777 Desc1 1 $200.00
Delete 888 Desc1 1 $200.00
---------------------------------------------------------------
Delete Total $400.00
---------------------------------------------------------------
Grand Total $1200.00
这是我的XML
<?xml version="1.0" encoding="utf-8"?>
<Logia xmlns="com.configsc">
<DocHeader xmlns="com.configsc.docheader">
<Document>
<Downto>
<rec_no>111</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$200.00</Value>
</Downto>
<Downto>
<rec_no>333</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$200.00</Value>
</Downto>
<Downto>
<rec_no>444</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$200.00</Value>
</Downto>
<Downto>
<rec_no>555</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$200.00</Value>
</Downto>
<Downto>
<rec_no>123</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$300.00</Value>
</Downto>
</Document>
<Document>
<Downto>
<rec_no>222</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$200.00</Value>
</Downto>
<Downto>
<rec_no>111</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$200.00</Value>
</Downto>
<Downto>
<rec_no>444</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$200.00</Value>
</Downto>
<Downto>
<rec_no>777</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$200.00</Value>
</Downto>
<Downto>
<rec_no>888</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$200.00</Value>
</Downto>
<Downto>
<rec_no>111</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$200.00</Value>
</Downto>
<Downto>
<rec_no>123</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>$300.00</Value>
</Downto>
</Document>
</Material>
</DocHeader>
</Logia>
答案 0 :(得分:0)
对于单个SO问题,我担心这可能有点太多了。我决定将您的输入简化为以下内容:
<Logia>
<DocHeader>
<Document>
<Downto>
<rec_no>111</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>100.00</Value>
</Downto>
<Downto>
<rec_no>333</rec_no>
<desc>Desc3</desc>
<qty>3</qty>
<Value>300.00</Value>
</Downto>
<Downto>
<rec_no>444</rec_no>
<desc>Desc4</desc>
<qty>4</qty>
<Value>400.00</Value>
</Downto>
<Downto>
<rec_no>555</rec_no>
<desc>Desc5</desc>
<qty>5</qty>
<Value>500.00</Value>
</Downto>
</Document>
<Document>
<Downto>
<rec_no>222</rec_no>
<desc>Desc2</desc>
<qty>2</qty>
<Value>200.00</Value>
</Downto>
<Downto>
<rec_no>111</rec_no>
<desc>Desc1</desc>
<qty>1</qty>
<Value>100.00</Value>
</Downto>
<Downto>
<rec_no>444</rec_no>
<desc>Desc4</desc>
<qty>4</qty>
<Value>400.00</Value>
</Downto>
<Downto>
<rec_no>777</rec_no>
<desc>Desc7</desc>
<qty>7</qty>
<Value>700.00</Value>
</Downto>
<Downto>
<rec_no>888</rec_no>
<desc>Desc8</desc>
<qty>8</qty>
<Value>800.00</Value>
</Downto>
</Document>
</DocHeader>
</Logia>
请注意删除命名空间以及将 Value
值转换为数字。
应用以下样式表:
XSLT 1.0
<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="doc1" match="Document[1]/Downto" use="rec_no" />
<xsl:key name="doc2" match="Document[2]/Downto" use="rec_no" />
<xsl:template match="/Logia/DocHeader">
<table border="1">
<!-- header -->
<tr>
<th>Section</th>
<th>RecNo</th>
<th>Desc</th>
<th>Qty</th>
<th>Value</th>
</tr>
<!-- same -->
<xsl:variable name="same" select="Document[1]/Downto[key('doc2', rec_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/Value)" />
<tr>
<td colspan="4">Same Total</td>
<th><xsl:value-of select="$same-total"/></th>
</tr>
<!-- add -->
<xsl:variable name="add" select="Document[1]/Downto[not(key('doc2', rec_no ))]" />
<xsl:apply-templates select="$add">
<xsl:with-param name="section">Add</xsl:with-param>
</xsl:apply-templates>
<xsl:variable name="add-total" select="sum($add/Value)" />
<tr>
<td colspan="4">Add Total</td>
<th><xsl:value-of select="$add-total"/></th>
</tr>
<!-- delete -->
<xsl:variable name="delete" select="Document[2]/Downto[not(key('doc1', rec_no ))]" />
<xsl:apply-templates select="$delete">
<xsl:with-param name="section">Delete</xsl:with-param>
</xsl:apply-templates>
<xsl:variable name="delete-total" select="sum($delete/Value)" />
<tr>
<td colspan="4">Delete Total</td>
<th><xsl:value-of select="$delete-total"/></th>
</tr>
<!-- grand total -->
<tr>
<th colspan="4">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"/>
<tr>
<td><xsl:value-of select="$section"/></td>
<td><xsl:value-of select="rec_no"/></td>
<td><xsl:value-of select="desc"/></td>
<td><xsl:value-of select="qty"/></td>
<td><xsl:value-of select="Value"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
以上输入将导致:
<table border="1">
<tr>
<th>Section</th>
<th>RecNo</th>
<th>Desc</th>
<th>Qty</th>
<th>Value</th>
</tr>
<tr>
<td>Same</td>
<td>111</td>
<td>Desc1</td>
<td>1</td>
<td>100.00</td>
</tr>
<tr>
<td>Same</td>
<td>444</td>
<td>Desc4</td>
<td>4</td>
<td>400.00</td>
</tr>
<tr>
<td colspan="4">Same Total</td>
<th>500</th>
</tr>
<tr>
<td>Add</td>
<td>333</td>
<td>Desc3</td>
<td>3</td>
<td>300.00</td>
</tr>
<tr>
<td>Add</td>
<td>555</td>
<td>Desc5</td>
<td>5</td>
<td>500.00</td>
</tr>
<tr>
<td colspan="4">Add Total</td>
<th>800</th>
</tr>
<tr>
<td>Delete</td>
<td>222</td>
<td>Desc2</td>
<td>2</td>
<td>200.00</td>
</tr>
<tr>
<td>Delete</td>
<td>777</td>
<td>Desc7</td>
<td>7</td>
<td>700.00</td>
</tr>
<tr>
<td>Delete</td>
<td>888</td>
<td>Desc8</td>
<td>8</td>
<td>800.00</td>
</tr>
<tr>
<td colspan="4">Delete Total</td>
<th>1700</th>
</tr>
<tr>
<th colspan="4">Grand Total</th>
<th>3000</th>
</tr>
</table>
呈现为: