Home > Software engineering >  Required item type of the context item for the child axis is node(); supplied value (.) has item typ
Required item type of the context item for the child axis is node(); supplied value (.) has item typ

Time:02-16

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?

CodePudding user response:

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()])"/>

CodePudding user response:

To add to Martin's answer, what's wrong is that in the expression count(m[@x = $x]), m means ./child::m, that is, you're asking for element children of the context item that are named m; but the containing xsl:for-each changes the context item so it is a string (one of the distinct values), which of course has no children.

  • Related