简体   繁体   中英

XSLT transformation group by element

Hello all i trying to learn XSLT transformation but i stuck in this moment when i wanna grouped by category, counting the number of records, which fall into this category and sum of the field sum.

For example my xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl" ?>
<root>
    <row>
        <prid>1</prid>
        <product>Name 1</product>
        <category>Cat 1</category>
        <price>23</price>
        <qty>9</qty>
        <sum>44</sum>
    </row>
    <row>
        <prid>2</prid>
        <product>Name 2</product>
        <category>Cat 3</category>
        <price>10</price>
        <qty>2</qty>
        <sum>22</sum>
    </row>
</root>

And my xsl file is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
        <html>
            <body>
                <table>
                    <tr>
                        <th style="text-align:left">Prid</th>
                        <th style="text-align:left">Product</th>
                        <th style="text-align:left">Category</th>
                        <th style="text-align:left">Price</th>
                        <th style="text-align:left">Qty</th>
                        <th style="text-align:left">Sum</th>
                    </tr>
                            <xsl:for-each-group select="/root/row" group-by="category">
                            <xsl:for-each select="current-group()">
                                    <tr>
                                        <td>
                                            <xsl:value-of select="prid" />
                                        </td>
                                        <td>
                                            <xsl:value-of select="product" />
                                        </td>
                                        <td>
                                            <xsl:value-of select="category" />
                                        </td>
                                        <td>
                                            <xsl:value-of select="price" />
                                        </td>
                                        <td>
                                            <xsl:value-of select="count(current-group())" />
                                        </td>
                                        <td>
                                            <xsl:value-of select="sum(current-group()/sum)" />
                                        </td>
                                    </tr>
                                </xsl:for-each>
                            </xsl:for-each-group>
                </table>
            </body>
        </html>
 </xsl:template>
</xsl:stylesheet>

If have someone can help me to where i wrong and explain it to me. Thanks

You haven't said very clearly what output you want, and you haven't said very clearly what output you are getting. You also haven't told us how you are running the transformation (what XSLT processor, and how invoked).

The code as written is correct. It's a little bit strange in that you are grouping, and then not producing any output that makes the grouped structure visible, but perhaps that's what you want.

You say you're getting an error message. You've only told us about a summary message that says it failed: somewhere there will be detailed messages that tell you why it failed. If you're not seeing those messages then you need to tell us what processor you are running and how, so we can tell you where to find them.

It's a common mistake to think you are running an XSLT 2.0 processor when you aren't. Some people imagine that putting "version='2.0'" at the top of the stylesheet is all you need to do. But if you send a stylesheet saying "version='2.0'" to a processor that was written in 2001 and only understands 1.0, it's not going to take any notice of that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM