2014-01-28 55 views
1

源文档: 下面是xml文档。分组元素并在xslt中添加其元素值

<?xml version="1.0"?> 
<Library> 
<Book code="123"> 
    <BookName>XML</BookName> 
    <Category>Programming</Category> 
    <Quantity>10</Quantity> 
    <Price>100</Price> 
</Book> 
<Book code="345"> 
    <BookName>Photoshop</BookName> 
    <Category>Design</Category> 
    <Quantity>50</Quantity> 
    <Price>200</Price> 
</Book> 
<Book code="123"> 
    <BookName>XML</BookName> 
    <Category>Programming</Category> 
    <Quantity>5</Quantity> 
    <Price>100</Price> 
</Book> 
<Book code="345"> 
    <BookName>Photoshop</BookName> 
    <Category>Design</Category> 
    <Quantity>10</Quantity> 
    <Price>200</Price> 
</Book> 
    <Book code="456"> 
    <BookName>Illustrator</BookName> 
    <Category>Design</Category> 
    <Quantity>100</Quantity> 
    <Price>300</Price> 
</Book>  
</Library> 

XSLT样式表: 这是本样式表。我试图使用xslt 2.但是,我没有得到如何获得所需的输出。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html> 
<body> 
<h1>Books Information</h1> 
<table border="1"> 
<xsl:for-each-group select="Library/Book" group-by="Category"> 
<xsl:sort select="current-grouping-key()"/> 
<tr> 
<td colspan="4">Category: 
    <b> 
    <xsl:value-of select="current-grouping-key()"/> 
    </b> 
</td> 
</tr> 
<tr> 
    <th>Book Code</th> 
    <th>Quantity</th> 
    <th>Unit Price</th> 
    <th>Price</th> 
</tr>  
<xsl:apply-templates select="current-group()"> 
    <xsl:sort select="@code"/> 
</xsl:apply-templates> 

</xsl:for-each-group> 
</table> 
</body> 
</html> 
</xsl:template> 
<xsl:template match="Book"> 
<tr> 
    <td><xsl:value-of select="@code"/></td> 
    <td><xsl:value-of select="Quantity"/></td> 
    <td><xsl:value-of select="Price"/></td> 
    <td> </td> 
</tr> 
    </xsl:template> 
</xsl:stylesheet> 

电流输出

<html> 
    <body> 
    <h1>Books Information</h1> 
    <table border="1"> 
    <tr> 
     <td colspan="4">Category: 
       <b>Design</b></td> 
    </tr> 
    <tr> 
     <th>Book Code</th> 
     <th>Quantity</th> 
     <th>Unit Price</th> 
     <th>Price</th> 
    </tr> 
    <tr> 
     <td>345</td> 
     <td>50</td> 
     <td>200</td> 
     <td></td> 
    </tr> 
    <tr> 
     <td>345</td> 
     <td>10</td> 
     <td>200</td> 
     <td></td> 
    </tr> 
    <tr> 
     <td>456</td> 
     <td>100</td> 
     <td>300</td> 
     <td></td> 
    </tr> 
    <tr> 
     <td colspan="4">Category: 
       <b>Programming</b></td> 
    </tr> 
    <tr> 
     <th>Book Code</th> 
     <th>Quantity</th> 
     <th>Unit Price</th> 
     <th>Price</th> 
    </tr> 
    <tr> 
     <td>123</td> 
     <td>10</td> 
     <td>100</td> 
     <td></td> 
    </tr> 
    <tr> 
     <td>123</td> 
     <td>5</td> 
     <td>100</td> 
     <td></td> 
    </tr> 
    </table> 
    </body> 
</html> 

预期输出

<html> 
    <body> 
    <h1>Books Information</h1> 
    <table border="1"> 
    <tr> 
     <td colspan="4">Category: 
       <b>Design</b></td> 
    </tr> 
    <tr> 
     <th>Book Code</th> 
     <th>Quantity</th> 
     <th>Unit Price</th> 
     <th>Price</th> 
    </tr> 
    <tr> 
     <td>345</td> 
     <td>60</td> 
     <td>200</td> 
     <td>1200</td> 
    </tr> 
    <tr> 
     <td>456</td> 
     <td>100</td> 
     <td>300</td> 
     <td></td> 
    </tr> 
    <tr> 
     <td colspan="4">Subtotal: 1500</td> 
    </tr> 
    <tr> 
     <td colspan="4">Category: 
       <b>Programming</b></td> 
    </tr> 
    <tr> 
     <th>Book Code</th> 
     <th>Quantity</th> 
     <th>Unit Price</th> 
     <th>Price</th> 
    </tr> 
    <tr> 
     <td>123</td> 
     <td>15</td> 
     <td>1500</td> 
     <td></td> 
    </tr> 
    <tr> 
     <td colspan="4">subtotal: 1500</td> 
    </tr> 
    <tr> 
     <td colspan="4">Grand TOtal: 3000</td> 
    </tr> 
    </table> 
</body> 
</html> 
+2

所以你要计算小计,它的价值你要计算小计,价格所有的组中的项目,或组中所有项目的数量和价格的乘积? –

+0

我想添加具有相同代码号的所有书籍的数量,并将其乘以每单位价格,并在价格栏上显示总价格,即分类明智。 [数量*价格=总价格] [小计将根据类别]。谢谢Martin – user3243634

+0

是的,我想计算产品的数量和价格的一个组中的所有项目 – user3243634

回答

2

它看起来像你正在做的两批这里分组。首先按“类别”在每个类别中按“代码”进行分组。

您第一组看起来不错,但第二组,则需要更换这些线路...

<xsl:apply-templates select="current-group()"> 
    <xsl:sort select="@code"/> 
</xsl:apply-templates> 

有了这些行,因为这会再组的当前类别中的书籍他们的代码

<xsl:for-each-group select="current-group()" group-by="@code"> 
    <xsl:sort select="current-grouping-key()" /> 
    <xsl:apply-templates select="." /> 
</xsl:for-each-group> 

在获得总计为每个组而言,你可以用这个表达

<xsl:value-of select="sum(current-group()/(Quantity * Price))" /> 

这将适用于这两个组,因此可用于获取“代码”的数量和“类别”的小计。对于总体市场,这纯粹是这

<xsl:value-of select="sum(Library/Book/(Quantity * Price))" /> 

试试这个XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html> 
<body> 
<h1>Books Information</h1> 
<table border="1"> 
<xsl:for-each-group select="Library/Book" group-by="Category"> 
<xsl:sort select="current-grouping-key()"/> 
<tr> 
<td colspan="4">Category: 
    <b> 
    <xsl:value-of select="current-grouping-key()"/> 
    </b> 
</td> 
</tr> 
<tr> 
    <th>Book Code</th> 
    <th>Quantity</th> 
    <th>Unit Price</th> 
    <th>Price</th> 
</tr>  
<xsl:for-each-group select="current-group()" group-by="@code"> 
    <xsl:sort select="current-grouping-key()" /> 
    <xsl:apply-templates select="." /> 
</xsl:for-each-group> 
<tr> 
<td colspan="4">subtotal: <xsl:value-of select="sum(current-group()/(Quantity * Price))" /></td> 
</tr> 
</xsl:for-each-group> 
<tr> 
<td colspan="4">Total: <xsl:value-of select="sum(Library/Book/(Quantity * Price))" /></td> 
</tr> 
</table> 
</body> 
</html> 
</xsl:template> 
<xsl:template match="Book"> 
<tr> 
    <td><xsl:value-of select="@code"/></td> 
    <td><xsl:value-of select="Quantity"/></td> 
    <td><xsl:value-of select="Price"/></td> 
    <td><xsl:value-of select="sum(current-group()/(Quantity * Price))" /></td> 
</tr> 
    </xsl:template> 
</xsl:stylesheet> 
+0

非常感谢Tim。伟大的帮助 – user3243634

+0

你好蒂姆,你可以请检查这个问题。我不知道如何在这里使用for-each-group。 http://stackoverflow.com/questions/21297376/xml-to-pdf-by-xsl-fo – user3243634