tags:

views:

849

answers:

1

Ok, I'm stumped. I would like test if a parameter sent to an XSLT template contains a period and to print out quotes if it does not. The parameter I would like to test is "value" in the template below. It seems the contains function should work, but for some reason the quotes always get outputted regardless the contents of "value". What am I doing wrong? Thanks

<!-- Add a JSON property -->
<xsl:template name="addProperty">
    <xsl:param name="name" />
    <xsl:param name="value" />

    <xsl:value-of select="$name" />
    <xsl:text>:</xsl:text>
    <xsl:if test="not(contains($value,'.'))">'</xsl:if>
    <xsl:value-of select="$value" />
    <xsl:if test="not(contains($value,'.'))">'</xsl:if>
    <xsl:text>,</xsl:text>   
</xsl:template>
+1  A: 

When I call your template, it works fine. How are you calling it? This is what I used:

<xsl:call-template name="addProperty">
    <xsl:with-param name="name" select="'abc'"/>
    <xsl:with-param name="value" select="'123'"/><!-- quoted number -->
</xsl:call-template>
<xsl:call-template name="addProperty">
    <xsl:with-param name="name" select="'abc'"/>
    <xsl:with-param name="value" select="123"/><!-- NOT quoted number -->
</xsl:call-template>
<xsl:call-template name="addProperty">
    <xsl:with-param name="name" select="'xyz'"/>
    <xsl:with-param name="value" select="'456.789'"/><!-- quoted number -->
</xsl:call-template>
<xsl:call-template name="addProperty">
    <xsl:with-param name="name" select="'xyz'"/>
    <xsl:with-param name="value" select="456.789"/><!-- NOT quoted number -->
</xsl:call-template>

And this is what I got as output:

abc:'123',abc:'123',xyz:456.789,xyz:456.789,

Could you not be passing in the values to the named template that you think you are passing in? What XSLT engine are you using?

A good way to test this is to add something like this to your named template and see what it produces, if you don't have a good debugger handy:

XXX<xsl:value-of select="$value"/>XXX
YYY<xsl:value-of select="contains($value, '.')"/>YYY
ZZZ<xsl:value-of select="not(contains($value, '.'))"/>ZZZ
lavinio
You're correct. I just realized I'm an idiot. A different template was being called with the input I wanted to test for. Sorry for the trouble.
Steve