Wednesday, September 11, 2013

XSLT shortcut for conditional statements

XSLT shortcut for conditional statements

I have a ton of this in a large stylesheet and it's making the stylesheet
become really cumbersome:
<xsl:when test="Field_Goal_Stats/Field_Goal_Total/FGTtl_Attempted">
"attempted": <xsl:value-of
select="number(Field_Goal_Stats/Field_Goal_Total/FGTtl_Attempted)"
/>,</xsl:when><xsl:otherwise>
"attempted": 0,</xsl:otherwise></xsl:choose>
Basically what I'm trying to do is straight forward. I'm trying to grab
the number() from a respective XPath. If that fails, normally with NaN,
because the field doesn't exist or the field doesn't contain a value
that's appropriate for number(), I set it to zero.
Is there anyway to either do this in 1 line or somehow create a re-usable
component that I can apply against a ton of other XPath nodes I'm running
this code against? Seems wrong to keep doing the whole choose/otherwise
pattern in so many parts of my code.

No comments:

Post a Comment