2014-10-08 208 views
0

我是XSLT的新手,我在计算表中的小节总计时遇到了问题。我正在使用XSLT 1.0。我能够从XML中获得Grand Total,但是获取子部分有很多ifs/else和很难解释的条件。 请在下面找到示例表和XML。在表中计算小节总计

所以我想要做的是将文档1与文档2进行比较。 如果Recno匹配这两个文档,那么这些记录将进入“相同”。 如果一个Rec在Document1中而不在Document2中,那么它将进入“Add”部分。 如果一个Rec在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

你绝对应该发布你的输入XML;你可能应该发布你想要的输出XML和你迄今为止尝试过的。谢谢。 – kjhughes 2014-10-08 15:30:08

+0

感谢您的意见。同时这个xml对于5K的行很大,并且和我的xslt文件一样,它有一些1K的代码。编辑这些以使通用可能会很耗时 – user2822294 2014-10-08 15:51:15

+0

请将您输入**的最小但完整的示例作为代码**发布。如果数据是正确的 - 上面表格的内容可以做得很好 - 我认为目前不是这样,因为分组没有意义。还要解释 - 或者最好是显示 - 输出的预期格式。 – 2014-10-08 15:56:03

回答

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> 

呈现为:

enter image description here

+0

我还有一个关于分组的问题考虑输出,我必须按照它们的数量对REC no 333进行分组。在添加部分中的REC 333应出现一次,和它们的数量(3 + 3)应该被显示在一个行段RECNO说明数量值 同111 DESC1 1 100.00 同444 Desc4 4 400.00 相同的总500 添加333 Desc3 3 300.00 添加333 Desc3 3 300.00 添加555 Desc5 5 500.00 添加总计1100 删除222 DESC2 2 200.00 删除777 Desc7 7 700.00 删除888 Desc8 8 800.00 删除共计1700 总计3300 – user2822294 2014-11-26 15:08:00

+0

@ user2822294恐怕这是不可能的。也许你应该发布一个新问题? – 2014-11-26 17:29:21

+0

感谢迈克尔的回应。我去做。 – user2822294 2014-11-27 05:21:35