Search code examples
xmlxsltxslt-2.0saxon

Required item type of the context item for the child axis is node(); supplied value (.) has item type xs:anyAtomicType


Here is my code:

<xsl:for-each select="distinct-values(m/@x)">
  <xsl:variable name="x" select="."/>
  <xsl:value-of select="count(m[@x = $x])"/>
</xsl:for-each>

Saxon says something along these lines:

Static error at char .. in xsl:for-each/@select on line .. column .. of main.xsl:
  XPTY0020: Required item type of the context item for 
  the child axis is node(); supplied value (.) has item 
  type xs:anyAtomicType

What's wrong and how to fix?


Solution

  • I think what you should use is e.g.

    <xsl:for-each-group select="m" group-by="@x">
      <xsl:value-of select="count(current-group())"/>
    </xsl:for-each-group>
    

    If you want to go along with that distinct-values attempt then store e.g. <xsl:variable name="context-node" select="."/> before the for-each and inside access <xsl:value-of select="count($context-node/m[@x = $x])"/> or <xsl:value-of select="count($context-node/m[@x = current()])"/>