Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 28th, 2005, 06:12 PM
Authorized User
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl:choose

Greetings,

I have a question regarding the evaluation of an expression for the xsl:choose instruction. Here is a sample snippet that will help me explain:

Code:
<xsl:template match="Entity">
    <xsl:choose>
        <xsl:when test="@Attrib">
            <xsl:call-template name="do-something">
            </xsl:call-template>
            <xsl:apply-templates/>
        </xsl:when>

        <xsl:when test="@Attrib2='Value2'">
            <xsl:call-template name="do-something-else">
            </xsl:call-template>
            <xsl:apply-templates/>
        </xsl:when>

        <xsl:when test="@Attrib3='Value3'">
            <xsl:call-template name="do-something-completely-different">
            </xsl:call-template>
            <xsl:apply-templates/>
        </xsl:when>

        <xsl:otherwise>
            <xsl:apply-templates/>
        </xsl:otherwise>

    </xsl:choose>
</xsl:template>
I suppose the question has to do with how the conditional expression is evaluated. The first "xsl:when" is always getting executed in my implementation. Even when the 'Attrib' attribute does not exist for the given node.

In my xml file the first node to evaluate has a attibute of 'Attrib2' with a value of 'Value2'. Therefore only the second 'xsl:when' above should evaluate. The test for the first xsl:when above is checking merely for the existence of a 'Attrib' attribute. (This is very similar to the bottom of page 237 in the XSLT 2.0 Programmers Reference book.)

When I move the first xsl:when down after the Attrib2 and Attrib3 tests, all seems to work correctly. As this branch will be executed much more frequently than the others (yet, hot for the first few nodes) I wanted it placed above the other tests.

Am I missing something here with regards to the way I am testing the attributes?

Thanks in advance for any assistance.

Dan

 
Old March 28th, 2005, 06:28 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If the test conditions are mutually exclusive, then it makes excellent sense to put the one that will fire most often first. But if more than one of the conditions can be true, you need to be aware that they are tested in order, and the first condition found that is true is the (only) one to be activated.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 29th, 2005, 09:49 AM
Authorized User
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael. I understand that the first match will execute. Perhaps my example is unclear. The first time through, 'Entity' only has an 'Attrib2' with a value of 'Value2'. There is no 'Attrib' this time through, yet the first test is still executing, while only the second test should be.

Although the first test is the most frequent, it is not a True condition for the first node in my xml file.

Dan

 
Old March 29th, 2005, 09:54 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Seems odd, can you show the XML, or at least a snippet containing the Entity element?

--

Joe (Microsoft MVP - XML)
 
Old March 29th, 2005, 11:34 AM
Authorized User
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think I've found the problem, and I'll fess up so others can avoid my mistake:

Even though the first 'Entity' node I encounter does not have a visible 'Attrib' attribute in the xml file, this attribute is assigned a default value in the DTD, and therefore exists for each 'Entity' node in the tree. Therefore, if listed first in my xsl:choose tests, it will indeed execute.

Does this sound accurate? While mocking up an xml file I commented out the DTD reference (as the names had been changed) and the error went away. This lead me down the path to the entity definition and default attribute values.

Tricky, to say the least, and this assumes that I have indeed found the problem.


Dan





 
Old March 29th, 2005, 11:44 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

No that seems the only possible explanation. That's why I asked to see the xml. Had you mentioned that you were validating it might have occurred to me sooner :)

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
choose between two directories and use one jjk2 PHP How-To 1 June 3rd, 2008 05:07 PM
<xsl:choose> and <xsl:otherwise> problem djmarquette XSLT 4 January 21st, 2005 01:56 PM
"choose, when, otherwise" - problem natjimy XSLT 8 December 17th, 2004 02:22 PM
XSL:CHOOSE & XPATH falmouth XSLT 3 November 17th, 2004 08:36 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.