Re: [xsl] Trouble understanding the Test expression in xsl:if

Subject: Re: [xsl] Trouble understanding the Test expression in xsl:if
From: Liam Quin <liam@xxxxxx>
Date: Wed, 27 May 2009 19:56:18 -0400
On Wed, May 27, 2009 at 02:28:05PM -0500, Keith Gilbert wrote:
> My shortened XML data:
> <category>
> 	<category_name>apples</category_name>
> 	<product>red delicious</product>
> </category>
> 	<category_name>oranges</category_name>
> 	<product>sunkist</product>
> </category>

> My simplified XSLT:
> <xsl:template match="category">
> 	<xsl:apply=templates/>
> </xsl:template>
> <xsl:template match="category_name">
> 	<xsl:if test="category_name = apples">
> 		<xsl:call-template name=apple_template/>
> 	</xsl:if>
> </xsl:template>

I think you'd do better by saying what you're trying to do here.

(1) you want the aples category to use a different template:
<xsl:template match="category[category_name = 'apples'">
  something different

(2) you want to do something additional to the category name
when there are apples

<xsl:template match="category_name">
  <xsl:if test=". = 'apples'">
    <!--* call a template to display the category name: *-->
    <xsl:call-template name="apple-template" />

Your "if" doesn't work because it's inside a template that is matching
the category_name element, and it's looking for a category_name element
inside that, whereas in fact there's just a string.
Most likely you want my suggestion (1), to treat the whole category
element differently if its name is "apples".


Liam Quin, W3C XML Activity Lead, *

Current Thread