English 中文(简体)
与SL的重复计算和产出表
原标题:Counting duplicates with XSL and outputting to table

Hi im new to XSL and im trying to output a table that shows the customer ID and the number of cars purchased, of customers that have purchased more than 1 car based on my XML file. An example output would be:

Customer
3
4

Number of Cars
3
2

但现在的情况是:

Customer
3
3
3
4
4
9

Number of Cars
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>

这里是我的SL档案。

<?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>

提前感谢。

最佳回答

有很多办法可以做这些事情,但并不肯定你试图做些什么。 如果你想根据客户填写表格,你可以先通过汽车提货。 你们希望每个线都有一个客户,因此,你们不得不通过客户进行排泄。

类似情况

<?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>

although that is a little messy and can be refined a lot (I have to run out!)

问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签