jpkeisala
jpkeisala

Reputation: 8916

Getting value 0 or 1 from count() of XSL nodesets

I have a bunch of nodesets where I want to return "1" instead of "true" when there are more than one hit on count($mynodeset) Is there more compact/smarter way to to do this in XSLT 1.1?

    <xsl:variable name="x5" select="count($mynodeset) != 0"/>
    <xsl:variable name="z5">
    <xsl:choose>
      <xsl:when test="x5 = 'true'">1</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

Upvotes: 1

Views: 821

Answers (2)

Sam Brightman
Sam Brightman

Reputation: 2950

Can't you use the number function?

<xsl:variable name="x5" select="number(count($mynodeset) != 0)"/>

I don't really know XSLT, but this seems quite simple according to:

XPath number function definition (XPath functions are used by XSLT 1.1 expressions)

Upvotes: 4

Gary McGill
Gary McGill

Reputation: 27536

Just use the function number(), which converts a boolean value to 1 or 0.

Upvotes: 4

Related Questions