tags:

views:

83

answers:

2

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>
+3  A: 

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

Gary McGill
+3  A: 

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)

Sam Brightman
Your reference is to XPath 1.0. I'm not sure how XSLT 1.1 enters into the picture, but just in case - don't use it, it's an unfinished and dead standard.
Pavel Minaev
Sorry for the lack of clarity, but XPath is specifically used to define XSLT 1.1 expressions. For this and other reasons, "dead" is a bit strong.
Sam Brightman