RE: [xsl] xsl 2.0 recursive functions and return value

Subject: RE: [xsl] xsl 2.0 recursive functions and return value
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sun, 26 Feb 2006 19:44:33 -0000
> how can i get the following to work:
> 
> 
> <xsl:function name="r2c:hasTextContent" as="xs:boolean">
>    <xsl:param name="node"/>
>      <xsl:for-each select="$node/*">
>        <xsl:choose>
>          <!--ignore!-->
>          <xsl:when test="r2c:isOutputNode(.)"/>
>          <!--gotcha!-->
>          <xsl:when test="r2c:isLeafNode(.)">
>            <xsl:value-of select="true()"/>
>          </xsl:when>

Use xsl:sequence here! xsl:value-of creates a text node. You're converting a
boolean to a string, wrapping the string in a text node, then because an
xs:boolean is required, the text node is atomized, and the string "true" is
converted back to a boolean.

>          <xsl:otherwise>
> 	<!-- recurse into the current node -->
>            <xsl:sequence select="r2c:hasTextContent(.)"/>
>          </xsl:otherwise>
>        </xsl:choose>
>      </xsl:for-each>
>    <!-- no leafNodes found?:-->
>    <xsl:sequence select="false()"/>
> </xsl:function>
> 
> this will inevitably put more than one boolean into my output 
> sequence, 
> which will cause an error.

Only if there is more than one leaf node.

> Any hints?

Try:

<xsl:function name="r2c:hasTextContent()" as="xs:boolean">
  <xsl:param name="node" as="node()"/>
  <xsl:sequence select="some $x in $node/*[not(r2c:isOutputNode(.)]
satisfies
     (r2c.isLeafNode($x) or r2c.hasTextContent($x))"/>
</xsl:function>

Michael Kay
http://www.saxonica.com/

Current Thread