我正在尝试编写和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>
我无法弄清楚我究竟做错了什么,具体来说就是&#34; numOfSims&#34;变量我无法开始工作。任何帮助将不胜感激。
答案 0 :(得分:1)
应该是这样的:
<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>