xsl: how to count the number of items for which there are no more than n number of children

advertisements

xml file:

<faculty>
<student name="a a" group="5">
    <subject date="2013-02-01" name="science">124</subject>
</student>

<student name="q q" group="9">
    <subject date="2013-02-01" name="my">124</subject>
</student>

<student name="z z" group="2">
</student>

<student name="v v" group="9">
    <subject date="2013-02-01" name="tro">tro</subject>
</student>
</faculty>

need: how to count the number of items for which there is no more than 2 subjects.

I know how to display all of these items and the number of subjects but I do not know how to count the total number of subjects:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="name" match="subject" use="@name" />

<xsl:template match="subject">
    <xsl:if test="count(key('name', @name)) &lt; 2">
        subject: <xsl:value-of select="@name" />
        count: <xsl:value-of select="count(key('name', @name))" />
    </xsl:if>
</xsl:template>

thanks in advance


The following will match the student elements that have less than 2 subject element children:

student[count(subject) &lt; 2]

If you wanted the total number of subject elements in the document, use the following:

count(/faculty/student/subject)

If you wanted the count of the list of distinct @name values from subject elements:

count(/faculty/student/subject[generate-id()=generate-id(key('name', @name)[1])])