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 September 29th, 2006, 10:50 AM
Registered User
 
Join Date: Sep 2006
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
 
Old September 29th, 2006, 11:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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





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





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