[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: Re: [xsl] Trouble understanding the Test expression in xsl:if
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 27 May 2009 15:54:19 -0400

At 2009-05-27 14:28 -0500, Keith Gilbert wrote:
I'm having trouble getting my head around this:

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>


For the life of me I can't figure out how to evaluate the value of "category_name" in the xsl:if line. I understand (I think) how to use xsl:value-of, but that doesn't seem to work inside the xsl:if. Any thoughts?

The content of an element is evaluated as a string from the concatenation of the descendent text nodes, and you incorrectly have:


<xsl:if test="category_name = apples">

... which tests the content of the child named "category_name" with the content of the child named "apples".

You want to compare the content to a string:

<xsl:if test="category_name = 'apples'">

I hope this helps.

. . . . . . . Ken

--
XSLT/XSL-FO/XQuery hands-on training - Los Angeles, USA 2009-06-08
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


Current Thread
Keywords
xml