Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 29th, 2006, 10:50 AM
Registered User
 
Join Date: Sep 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Wierd XSLT behavior

Hi all,

How does XSLT resolve elements that match more than one template? Think you know, then please look below.

This is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<menu>
    <appetizers title="Work up an Appetite">
        <dish id="1" price="8.95">Crab Cakes</dish>
        <dish id="2" price="9.95">Smoked Salmon and Avocado Quesadilla</dish>
    </appetizers>
    <entrees title="chow Time">
        <dish id="1" price="19.94">Grilled Salmon</dish>
        <dish id="2" price="10.39">Pizza</dish>
    </entrees>
    <desserts>
        <dish id="1" price="3.99">Cake</dish>
        <dish id="2" price="1.99">Chocolate</dish>
    </desserts>
</menu>

If I use the following stylesheet,
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="4.0" encoding="iso-8859-1" indent="yes"/>

    <xsl:template match="/menu/entrees/dish" >
        <xsl:value-of select="parent::*/@title" />: <xsl:value-of select="." />
    </xsl:template>
    <xsl:template match="/menu/appetizers/dish">
        <xsl:value-of select="@id" />: <xsl:value-of select="." />
    </xsl:template>
    <xsl:template match="*/dish">
        <xsl:text>Test</xsl:text>
    </xsl:template>
    <xsl:template match="/menu/desserts/dish">
        <xsl:value-of select="parent::*/@title" />: <xsl:value-of select="." />
    </xsl:template>
    <xsl:template match="dish">
        <xsl:value-of select="@price" />: <xsl:value-of select="." />
    </xsl:template>

</xsl:stylesheet>

I get this output:
Test
Test

Test
Test

: Cake
: Chocolate

But if I use this stylesheet
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="4.0" encoding="iso-8859-1" indent="yes"/>

    <xsl:template match="/menu/entrees/dish" >
        <xsl:value-of select="parent::*/@title" />: <xsl:value-of select="." />
    </xsl:template>
    <xsl:template match="/menu/appetizers/dish">
        <xsl:value-of select="@id" />: <xsl:value-of select="." />
    </xsl:template>

    <xsl:template match="/menu/desserts/dish">
        <xsl:value-of select="parent::*/@title" />: <xsl:value-of select="." />
    </xsl:template>
<xsl:template match="*/dish">
        <xsl:text>Test</xsl:text>
    </xsl:template>
    <xsl:template match="dish">
        <xsl:value-of select="@price" />: <xsl:value-of select="." />
    </xsl:template>

</xsl:stylesheet>

I get this output:
Test
Test

Test
Test

Test
Test

What is going on here? I thought XSLT was event-driven not procedural.
Therefore, it shouldn't matter that I change the order of the templates.

Clarification would truly be appreciated.

Thanks
Reply With Quote
  #2 (permalink)  
Old September 29th, 2006, 11:24 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The rules are clearly explained in the spec.

http://www.w3.org/TR/xslt#conflict

or the equivalent section for XSLT 2.0

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wierd asp:FileUpload problem rstelma ASP.NET 2.0 Professional 4 July 12th, 2008 05:31 AM
Wierd Printing Problem danielr Infopath 2 February 6th, 2007 05:54 AM
wierd error crmpicco Javascript How-To 1 August 8th, 2005 10:50 AM
Strange Behavior With Anchor in XSLT kwilliams XSLT 6 July 21st, 2005 01:52 PM
Wierd Date Calculations Steven Access 2 December 16th, 2003 07:44 PM



All times are GMT -4. The time now is 11:32 PM.


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