Counting nodes using XSLT

Posted on 2006-06-06
Last Modified: 2008-02-26
Im transforming XML document to HTML using XSLT stylesheet, here is example:
<?xml version="1.0" encoding="utf-8"?>
    <title>3D Game Engine Programming</title>
    <title>A Programmer's Introduction to C#</title>
    <file>APress A Programmer's Introduction to C#.pdf</file>

whole document is much longer, of course.
Now i need to create html table which will contain all publishers found in XML file and count of books they published, for example:
Publisher Published
Apress        10
Wiley          20


Sofar i have this code:

<xsl:for-each select="/catalog/book">
                <xsl:value-of select="publisher"/>
                <xsl:value-of select="count(//publisher[.=current()/publisher])"/>

this counts number of published books, but they are repeating for each book so output looks like this:
Apress 50
Wiley   20
Wiley   20
Apress 50

Any ideas how to get right output? Thanks
Question by:zajda82
    LVL 60

    Expert Comment

    by:Geert Bormans
    Hi zajda82,

    you could limit your loop to just once per unique publisher

    <xsl:for-each select="/catalog/book[not(publisher = preceding::book/publisher)]">
                    <xsl:value-of select="publisher"/>
                    <xsl:value-of select="count(//publisher[.=current()/publisher])"/>

    in my next post I will show how to group using keys,
    that has a better performance

    LVL 60

    Accepted Solution


    here is a solution using keys
    This grouping method is known as the muenchian grouping algorithm

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="" version="1.0">
        <xsl:key name="book-by-publ" match="book" use="publisher"/>
        <xsl:template match="/">
                        <xsl:for-each select="//book[generate-id() = generate-id(key('book-by-publ', publisher)[1])]">
                            <xsl:variable name="pub" select="publisher"/>
                                <td><xsl:value-of select="$pub"/></td>
                                <td><xsl:value-of select="count(//book[publisher = $pub])"/></td>

    check out this article for understanding
    LVL 3

    Author Comment

    awesome answer m8, thanks a lot!!!!
    LVL 60

    Expert Comment

    by:Geert Bormans

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now