使用XSL计算重复项并输出到表

时间:2012-04-19 14:49:00

标签: xml xslt xslt-1.0 xslt-2.0

我是XSL新手,我试图输出一张表格,显示客户ID和购买的汽车数量,根据我的XML文件购买了超过1辆汽车的客户。 输出示例如下:

客户
3
4

汽车数量
3
2个

但我现在得到的是:

客户
3
3
3
4
4
9

汽车数量





null

这是我的XML文件。

<cars>

<car>
    <carID>
            3
    </carID>

    <mobileNumber>

        <areaCode>
                00353
        </areaCode>

        <number>
                8723059
        </number>

    </mobileNumber>

    <customerID>
                3
    </customerID>

    <purchaseDate>

            <dayPurchased>
                        6
            </dayPurchased>

            <monthPurchased>
                        April
            </monthPurchased>

            <yearPurchased>
                        2011
            </yearPurchased>

    </purchaseDate>

</car>

<car>
    <carID>
            4
    </carID>

    <mobileNumber>

        <areaCode>
                00353
        </areaCode>

        <number>
                8723099
        </number>

    </mobileNumber>

    <customerID>
                3
    </customerID>

    <purchaseDate>

            <dayPurchased>
                        6
            </dayPurchased>

            <monthPurchased>
                        April
            </monthPurchased>

            <yearPurchased>
                        2011
            </yearPurchased>

    </purchaseDate>

</car>

<car>
    <carID>
            5
    </carID>

    <mobileNumber>

        <areaCode>
                00353
        </areaCode>

        <number>
                8723777
        </number>

    </mobileNumber>

    <customerID>
                3
    </customerID>

    <purchaseDate>

            <dayPurchased>
                        6
            </dayPurchased>

            <monthPurchased>
                        April
            </monthPurchased>

            <yearPurchased>
                        2011
            </yearPurchased>

    </purchaseDate>

</car>

<car>
    <carID>
            16
    </carID>

    <mobileNumber>

        <areaCode>
                00353
        </areaCode>

        <number>
                8721777
        </number>

    </mobileNumber>

    <customerID>
                4
    </customerID>

    <purchaseDate>

            <dayPurchased>
                        6
            </dayPurchased>

            <monthPurchased>
                        April
            </monthPurchased>

            <yearPurchased>
                        2011
            </yearPurchased>

    </purchaseDate>

</car>

<car>
    <carID>
            166
    </carID>

    <mobileNumber>

        <areaCode>
                00353
        </areaCode>

        <number>
                8722777
        </number>

    </mobileNumber>

    <customerID>
                4
    </customerID>

    <purchaseDate>

            <dayPurchased>
                        6
            </dayPurchased>

            <monthPurchased>
                        April
            </monthPurchased>

            <yearPurchased>
                        2011
            </yearPurchased>

    </purchaseDate>

</car>

<car>
    <carID>
            169
    </carID>

    <mobileNumber>

        <areaCode>
                00353
        </areaCode>

        <number>
                8721787
        </number>

    </mobileNumber>

    <customerID>
                9
    </customerID>

    <purchaseDate>

            <dayPurchased>
                        6
            </dayPurchased>

            <monthPurchased>
                        April
            </monthPurchased>

            <yearPurchased>
                        2011
            </yearPurchased>

    </purchaseDate>

</car>
</cars>

这是我的XSL文件。

<?xml version="1.0"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<html> <head><title>Customers</title></head> <body> <table rules="all">
<thead><tr><th>Customer</th><th>Number of Cars Purchased</th></tr></thead> 
  <xsl:for-each select="cars/car">
  <tr><td>  <xsl:apply-templates select="customerID"/> </td>
  </xsl:for-each>
</table></body></html>

</xsl:template>
</xsl:transform>

提前致谢。

1 个答案:

答案 0 :(得分:1)

有很多方法可以做这些事情,但不确定你在那里做什么。如果你想根据顾客做一张桌子,你就无法从汽车中开始。您需要每行一个客户,因此您必须通过计算它们的客户进行迭代。

像这样的东西

<?xml version="1.0"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">

        <html> <head>
            <title>Customers</title></head> <body> 
                <table rules="all">
                    <thead>
                        <tr><th>Customer</th><th>Number of Cars Purchased</th>                            </tr>
                    </thead> 
                    <xsl:for-each select=".//customerID">
                        <xsl:variable name="customerId" select="text()"/>
                        <xsl:if test="not(preceding::customerID) or $customerId != preceding::customerID[1]">
                       <tr>
                           <td>  <xsl:value-of select="."/></td>
                           <td><xsl:value-of select="count(following::customerID[text()=$customerId])+1"/></td>
                       </tr>
                        </xsl:if>
                   </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:transform>

虽然这有点乱,可以很多精炼(我必须用完!)