XSL:使用计数查找特定值的每个实例

XSL: Using count to find each instance of a specific value

我正在尝试编写 XSL 文件,找出同一个人购买了多少张 SIM 卡,并输出该号码及其客户 ID。

这是 XML 文件的摘录,其中包含相关标签的示例:

    <sim>
        <simID>16</simID>
        <areaCode>081</areaCode>
        <number>1234582</number>
        <customerID>5</customerID>
        <yearPurchased>2008</yearPurchased>
        <monthPurchased>10</monthPurchased>
        <dayPurchsed>12</dayPurchsed>
    </sim>
    <customer>
        <customerID>5</customerID>
        <surname>Brown</surname>
        <firstname>Peter</firstname>
        <streetAddress>103 Main Street</streetAddress>
        <townName>Dorpborough</townName>
        <countyName>Kilkenny</countyName>
        <contractOrPrepaid>contract</contractOrPrepaid>
        <confirmedIdentity>1</confirmedIdentity>
    </customer>

在标签 <sims><customers>

中,这些标签有多个实例,都具有相同的子标签

这是我的 XSL 代码:

<table rules="all">

                <thead>
                    <tr>
                        <th>Customer ID</th>
                        <th>No. of Sims Purchased</th> 
                     </tr>
                </thead> 

                <tbody>
                    <xsl:for-each select="database/customers/customer">

                        <xsl:variable name="customerIDvar" select="customerID"/>

                        <xsl:variable name="numOfSims">
                            <xsl:for-each select="database/sims/sim">
                                <xsl:value-of select="count([customerID=$customerIDvar])">
                            </xsl:for-each>
                        </xsl:variable>

                        <xsl:if test="$numOfSims>1">
                            <tr>
                                <td>
                                    <xsl:value-of select="$customerIDvar"/>
                                </td>
                                <td>
                                    <xsl:value-of select="$numOfSims"/>
                                </td>
                            </tr>
                        </xsl:if>
                    </xsl:for-each>         
                </tbody>

            </table>

我无法弄清楚我到底做错了什么,特别是 "numOfSims" 变量我无法开始工作。任何帮助将不胜感激。

应该是这样的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
    <table rules="all">
        <thead>
            <tr>
                <th>Customer ID</th>
                <th>No. of Sims Purchased</th>
            </tr>
        </thead>
        <tbody>
            <xsl:for-each select="database/customers/customer">
                <xsl:variable name="customerIDvar" select="customerID"/>
                <xsl:variable name="numOfSims">
                    <xsl:value-of select="count(/database/sims/sim[customerID=$customerIDvar])"/>
                </xsl:variable>
                <xsl:if test="$numOfSims>1"><tr>
                        <td>
                            <xsl:value-of select="$customerIDvar"/>
                        </td>
                        <td>
                            <xsl:value-of select="$numOfSims"/>
                        </td>
                    </tr>
                </xsl:if>
            </xsl:for-each>
        </tbody>
    </table>
</xsl:template>
</xsl:stylesheet>

假设您的 XML 类似于:

<database>
<sims>
    <sim>
        <simID>16</simID>
        <areaCode>081</areaCode>
        <number>1234582</number>
        <customerID>5</customerID>
        <yearPurchased>2008</yearPurchased>
        <monthPurchased>10</monthPurchased>
        <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
        <simID>16</simID>
        <areaCode>081</areaCode>
        <number>1234582</number>
        <customerID>5</customerID>
        <yearPurchased>2008</yearPurchased>
        <monthPurchased>10</monthPurchased>
        <dayPurchsed>12</dayPurchsed>
    </sim>
</sims>
<customers>
    <customer>
        <customerID>5</customerID>
        <surname>Brown</surname>
        <firstname>Peter</firstname>
        <streetAddress>103 Main Street</streetAddress>
        <townName>Dorpborough</townName>
        <countyName>Kilkenny</countyName>
        <contractOrPrepaid>contract</contractOrPrepaid>
        <confirmedIdentity>1</confirmedIdentity>
    </customer>
</customers>
</database>