Martin gave a good explanation.
If what you want is allowed, then elements that don't have a group
attribute at all, would be in the same group as elements that do have a group=''
attribute -- and this is not precise.
The simplest solution is to indicate in the group-by
attribute that whether or not a group
attribute exists doesn't matter.
The simplest way to do so is:
<xsl:for-each-group select="Root/*" group-by="string(@group)">
Here is a complete transformation:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each-group select="Root/*" group-by="string(@group)">
"<xsl:sequence select="string(@group)"/>"
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<Root>
<Item id="1" group="foo" />
<Item id="2" group="bar" />
<Item id="3" />
</Root>
the wanted result is produced:
"foo"
"bar"
""