0

我刚刚开始使用 XSLT 并试图解决这个问题。您的帮助将不胜感激。

示例 XML 文件

<purchases>
    <item id = "1" customer_id = "5">
    </item>
    <item id = "2" customer_id = "5">
    </item>
    <item id = "7" customer_id = "4">
    </item>
</purchases>

所需的 HTML 输出:

<table>
    <tr><th>Customer ID</th><th>Number of Items Purchased</th></tr>
    <tr><td>5</td><td>2</td></tr>
    <tr><td>4</td><td>1</td></tr>
</table>

ID 为 5 的客户购买了 2 件商品。ID 为 4 的客户购买了 1 件商品。

4

3 回答 3

1

XSLT 1.0 解决方案....

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

      <xsl:template match="/">
        <table>
          <xsl:apply-templates select="purchases/item"/>     
        </table>
      </xsl:template>

   <xsl:template match="item">
     <xsl:if test="not(
       preceding-sibling::*[@customer_id=current()/@customer_id]
       )">     
         <tr><td><xsl:value-of select="@customer_id"/></td>
           <td><xsl:value-of select="count(following-sibling::item
             [@customer_id=current()/@customer_id])+1"/></td>
         </tr> 
     </xsl:if>   
  </xsl:template>

</xsl:stylesheet>
于 2012-06-05T16:38:21.800 回答
0

一个可能的 Xslt 1.0 解决方案是

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:key name="kCustomer" match="item" use="@customer_id"/>

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="purchases">
        <xsl:element name="table">
            <xsl:element name="tr">
                <xsl:element name="th">Customer ID</xsl:element>
                <xsl:element name="th">Number of Items Purchased</xsl:element>
                <xsl:for-each select="item[generate-id(.) = generate-id(key('kCustomer', @customer_id))]">
                    <xsl:variable name="total" select="count(/purchases/item[@customer_id=current()/@customer_id]/@id)" />
                    <xsl:element name="tr">
                        <xsl:element name="td">
                            <xsl:value-of select="./@customer_id" />
                        </xsl:element>
                        <xsl:element name="td">
                            <xsl:value-of select="$total" />
                        </xsl:element>
                    </xsl:element>
                </xsl:for-each>
            </xsl:element>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

取自Dimitres关于How can I Group and sort based on sum 的回答。


正如 Dimitre 所指出的,购买模板可以简化为

    <xsl:template match="purchases">
        <table>
            <tr>
                <th>Customer ID</th>
                <th>Number of Items Purchased</th>
            </tr>
            <xsl:for-each select="item[generate-id(.) = generate-id(key('kCustomer', @customer_id))]">
                <xsl:variable name="total" select="count(/purchases/item[@customer_id=current()/@customer_id]/@id)" />
                    <tr>
                        <td>
                            <xsl:value-of select="./@customer_id" />
                        </td>
                        <td>
                            <xsl:value-of select="$total" />
                        </td>
                    </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
于 2012-06-05T16:42:42.153 回答
0

如果您想要 XSLT 2.0,那么下面的 XSLT 应该可以解决问题:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output indent="yes"/>
    <xsl:template match="/purchases">
        <table>
            <tr>
                <th>Customer ID</th>
                <th>Number of Items Purchased</th>
            </tr>
            <xsl:for-each-group select="item" group-by="@customer_id">
                <tr>
                    <td>
                        <xsl:value-of select="current-grouping-key()"/>
                    </td>
                    <td>
                        <xsl:value-of select="count(current-group())"/>
                    </td>
                </tr>
            </xsl:for-each-group>
        </table>
    </xsl:template>
</xsl:stylesheet>
于 2012-06-05T16:17:47.467 回答