Hi!
I´m a newbie trying to get the size of groups with XSLT 2.0
What I'm trying to do is get the count of different child elements of a certain node, so I'm grouping them by name.

Given the following sample xml file what I want is a 2 columns table:

Node name | Instances
A | 1
B | 2
C | 1

And what I get with my code is the number of childs, not the size of each gruop.

Node name | Instances
A | 3
B | 2
C | 1

<?xml version="1.0" encoding="ISO-8859-1"?>
<demo>
	<A>
		<aa1>data1</aa1>
		<aa2>data2</aa2>
		<aa3>data3</aa3>
	</A>
	<B>
		<bb1>data1</bb1>
		<bb2>data1</bb2>
	</B>
	<B>
		<bb1>data1</bb1>
		<bb2>data1</bb2>
	</B>
	<C>
		<cc1>data3</cc1>
	</C>
</demo>

Here is my xslt code

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="demo">
  <html>
  <body>
  <h2>Group by name</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
	  <th>Node name</th>
      <th>Instances</th>	  
    </tr>
	<xsl:for-each-group select="*" group-by="name()">
    	<tr>
		    <td><xsl:value-of select="name()" /></td>
			<!-- this returns the number of childs, not the size of the GROUP -->
			<td><xsl:value-of select="count(*)" /></td>
		</tr>
	</xsl:for-each-group>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Thanks in advance!
Lorena

Recommended Answers

All 7 Replies

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" method="html"/>
    <xsl:key name="only" use="name()" match="child::demo/*"/>
    <xsl:template match="/">
        <html>
            <body>
                <style type="text/css">
                    table { {
                        border: solid 3px #000000;
                    }
                    }
                    td,th {
                        border: solid 3px #000000;
                    }
                    th {
                        color:#ff0000;
                    }
                    .even {
                        color:#00ff00;
                    }
                    .odd {
                        color:#ff00ff;
                    }</style>
                <p>Select parent Node and count child Node</p>
                <div>
                    <table>
                        <tr>
                            <th>Where</th>
                            <th>Count</th>
                            <th>What</th>
                        </tr>
                        <xsl:for-each
                            select="child::demo/*[generate-id() =
                            generate-id(key('only', name())[1])]
                            ">
                            <tr>
                                <xsl:choose>
                                    <xsl:when test="position()mod 2 = 0">
                                        <xsl:attribute name="class">even</xsl:attribute>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:attribute name="class">odd</xsl:attribute>
                                    </xsl:otherwise>
                                </xsl:choose>

                                <td>
                                    <xsl:value-of select="name()"/>
                                </td>
                                <td>

                                    <xsl:value-of select="count(key('only',name())/*)"/>

                                </td>
                                <td>
                                    <xsl:for-each select="key('only',name())/*">
                                        <xsl:value-of select="."/>
                                    </xsl:for-each>
                                </td>
                            </tr>
                        </xsl:for-each>
                    </table>
                </div>
                <p>All child Note count</p>
                <div>
                    <table>
                        <tr>
                            <th>Where</th>
                            <th>Count</th>
                            <th>What</th>
                        </tr>
                        <xsl:for-each select="child::demo/*">
                            <tr>
                                <xsl:choose>
                                    <xsl:when test="position()mod 2 = 0">
                                        <xsl:attribute name="class">even</xsl:attribute>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:attribute name="class">odd</xsl:attribute>
                                    </xsl:otherwise>
                                </xsl:choose>
                                <td>
                                    <xsl:value-of select="name()"/>
                                </td>
                                <td>

                                    <xsl:value-of select="count(child::*)"/>

                                </td>
                                <td>
                                    <xsl:for-each select="child::*">
                                        <xsl:value-of select="."/>
                                    </xsl:for-each>
                                </td>
                            </tr>
                        </xsl:for-each>
                    </table>
                </div>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

result

<html>
   <body>
      <style type="text/css">
                    table { {
                        border: solid 3px #000000;
                    }
                    }
                    td,th {
                        border: solid 3px #000000;
                    }
                    th {
                        color:#ff0000;
                    }
                    .even {
                        color:#00ff00;
                    }
                    .odd {
                        color:#ff00ff;
                    }</style>
      <p>Select parent Node and count child Node</p>
      <div>
         <table>
            <tr>
               <th>Where</th>
               <th>Count</th>
               <th>What</th>
            </tr>
            <tr class="odd">
               <td>A</td>
               <td>3</td>
               <td>data1data2data3</td>
            </tr>
            <tr class="even">
               <td>B</td>
               <td>4</td>
               <td>data1data1data1data1</td>
            </tr>
            <tr class="odd">
               <td>C</td>
               <td>1</td>
               <td>data3</td>
            </tr>
         </table>
      </div>
      <p>All child Note count</p>
      <div>
         <table>
            <tr>
               <th>Where</th>
               <th>Count</th>
               <th>What</th>
            </tr>
            <tr class="odd">
               <td>A</td>
               <td>3</td>
               <td>data1data2data3</td>
            </tr>
            <tr class="even">
               <td>B</td>
               <td>2</td>
               <td>data1data1</td>
            </tr>
            <tr class="odd">
               <td>B</td>
               <td>2</td>
               <td>data1data1</td>
            </tr>
            <tr class="even">
               <td>C</td>
               <td>1</td>
               <td>data3</td>
            </tr>
         </table>
      </div>
   </body>
</html>

Thanks for your reply but your code returns the number of childs for each node type, and what I need is for each node type the count of the nodes with that type.

I think that using count(*) inside the for-each-group is the problem, cause this is counting the children.

thanks
Lorena

count context each node
there are 3 kind of text data1 data2 data3

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" method="html"/>
    <xsl:key name="only" use="text()" match="child::demo/*/*"/>
    <xsl:template match="/">
        <html>
            <body>
                <style type="text/css">
                    table { {
                        border: solid 3px #000000;
                    }
                    }
                    td,th {
                        border: solid 3px #000000;
                    }
                    th {
                        color:#ff0000;
                    }
                    .even {
                        color:#00ff00;
                    }
                    .odd {
                        color:#ff00ff;
                    }</style>
                <p>Select context of  child Node</p>
                <div>
                    <table>
                        <tr>
                            <th>Context</th>
                            <th>Count</th>
                            
                        </tr>
                        <xsl:for-each
                            select="child::demo/*/*[generate-id() =
                            generate-id(key('only', text())[1])]
                            ">
                            <tr>
                                <xsl:choose>
                                    <xsl:when test="position()mod 2 = 0">
                                        <xsl:attribute name="class">even</xsl:attribute>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:attribute name="class">odd</xsl:attribute>
                                    </xsl:otherwise>
                                </xsl:choose>

                                <td>
                                    <xsl:value-of select="text()"/>
                                </td>
                                <td>

                                    <xsl:value-of select="count(key('only',text()))"/>

                                </td>
                               
                            </tr>
                        </xsl:for-each>
                    </table>
                </div>
               
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>
<html>
   <body><style type="text/css">
                    table { {
                        border: solid 3px #000000;
                    }
                    }
                    td,th {
                        border: solid 3px #000000;
                    }
                    th {
                        color:#ff0000;
                    }
                    .even {
                        color:#00ff00;
                    }
                    .odd {
                        color:#ff00ff;
                    }</style><p>Select context of  child Node</p>
      <div>
         <table>
            <tr>
               <th>Context</th>
               <th>Count</th>
            </tr>
            <tr class="odd">
               <td>data1</td>
               <td>5</td>
            </tr>
            <tr class="even">
               <td>data2</td>
               <td>1</td>
            </tr>
            <tr class="odd">
               <td>data3</td>
               <td>2</td>
            </tr>
         </table>
      </div>
   </body>
</html>

you can call me via Icq 567877710

Please have a look to the code like this:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Header:$Id: shpmnt05_prepare.xsl,v 1.1 2009-09-09 12:57:09 hs Exp $ -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="uniq"
match="/demo/*"
use="name()"/>

<xsl:template match="/">
<xsl:for-each select="/*">
<xsl:copy>
<xsl:for-each select="/demo/*[generate-id(.) =
generate-id(key('uniq', name()))]">
<xsl:copy>
<count>
<xsl:value-of select="count(../*[name()= current()/name()])"/>
</count>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Hello,

now the output is sorted.
I don't know why the file is not intended, my original is. wht's wrong. But, I think the script could solve your problem.

Greetings

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="uniq"
match="/demo/*"
use="name()"/>

<xsl:template match="/">
<xsl:for-each select="/*">
<xsl:copy>
<xsl:for-each select="/demo/*[generate-id(.) =
generate-id(key('uniq', name()))]">
<xsl:sort select="name()"/>
<xsl:copy>
<count>
<xsl:value-of select="count(../*[name()= current()/name()])"/>
</count>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

without html code
all parser have no problems

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:key name="only" use="local-name()" match="//demo/*"/>
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
		<root>
			<xsl:apply-templates select="demo"/>
		</root>
	</xsl:template>


	<xsl:template match="demo">
		<xsl:for-each select="/demo/*[generate-id(.) = generate-id(key('only', name()))]">
			<xsl:sort select="name()"/>
			<tag>
				<xsl:value-of select="name()"/>
			</tag>
			<count>
				<xsl:value-of select="count(key('only', name()))"/>
			</count>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

Hello,

now the output is sorted.
I don't know why the file is not intended, my original is. wht's wrong. But, I think the script could solve your problem.

Greetings

Thanks a lot! It works.
Now I have to understand what is this script actually doing :D

Greetings
Lorena

ps: can I tag the thread as solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.