1

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

客户
3
4

汽车数量
3
2

但我现在得到的是:

客户
3
3
3
4
4
9

车数
null
null
null
null
null
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>

提前致谢。

4

1 回答 1

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>

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

于 2012-04-19T16:37:36.473 回答