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
|