Xml XSL:使用count查找特定值的每个实例

Xml XSL:使用count查找特定值的每个实例,xml,xslt,Xml,Xslt,我试图编写和XSL文件,找出同一个人购买了多少张SIM卡,并将该数字与他们的客户ID一起输出 下面是XML文件的摘录以及相关标记的示例: <sim> <simID>16</simID> <areaCode>081</areaCode> <number>1234582</number> <customerID>5</custo

我试图编写和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>

16
081
1234582
5.
2008
10
12
5.
棕色的
彼得
大街103号
多普布罗
基尔肯尼
合同
1.
在标记
中有多个这些标记的实例,它们都具有相同的子级

以下是我的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>

客户ID
购买模拟人生的数目

我不知道我到底做错了什么,特别是“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>

客户ID
购买模拟人生的数目
假设您的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>

16
081
1234582
5.
2008
10
12
16
081
1234582
5.
2008
10
12
5.
棕色的
彼得
大街103号
多普布罗
基尔肯尼
合同
1.

不是答案,但
不应该是
?它可能应该是“1”,这样会更干净。@publicgk我最近知道
不需要转义。@LingamurthyCS,谢谢。我不知道。