Is xsl:sequence always non-empty?


I don't understand output from this stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="">
    <xsl:template match="/">
        <xsl:apply-templates select="root/sub"/>

    <xsl:template match="sub">
        <xsl:variable name="seq">
            <xsl:sequence select="*" />

            <xsl:value-of select="@id" />
            <xsl:text>: </xsl:text>
            <xsl:value-of select="count($seq)" />

when applied to following XML:

    <sub id="empty" />
    <sub id="one"><one/></sub>
    <sub id="two"><one/><one/></sub>
    <sub id="three"><one/><one/><one/></sub>

Output, written by xsl:message element, is:

empty: 1
one: 1
two: 1
three: 1

I expected this one instead:

empty: 0
one: 1
two: 2
three: 3

Why does count($seq) always return 1 in this case? How would you change variable definition, so that I can later test it for emptiness? (Simple <xsl:variable name='seq' select='*' /> would return expected answer, but is not an option ... I want to change between variable in this template, and test it for emptiness later).


You are selecting the sub nodes, and then counting each node - so it'll always be 1. You need to count the children, for example:

By : FinnNk

This video can help you solving your question :)
By: admin