View Single Post
  #1 (permalink)  
Old December 1st, 2008, 04:52 AM
asc asc is offline
Registered User
 
Join Date: Dec 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question about template precedence

Hello I have just begun to play around with XSLT and I have a question about template matching precedence that I haven't been able to figure out myself. That is, I know how to solve this problem to get the desired result, but I since I'm in the "learning phase" I would like to know why the XSLT stylesheet below produces the result below.

The XML-file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
    <car>
        <brand>BMW</brand>
        <type>335i</type>
    </car>
    <car>
        <brand>Audi</brand>
        <type>A4 3.0 TDI</type>
    </car>
    <car>
        <brand>Alfa</brand>
        <type>159 3.2 JTS V6</type>
    </car>
</cars>
The XSLT stylesheet:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="..."> 
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

     <xsl:template match="/">
        <html>
          <body>
            [list]
              <xsl:apply-templates/>
            </ul>
          </body>
        </html>
      </xsl:template>


    <xsl:template match="*"/>


    <xsl:template match="//car[type='335i']">
        <li><xsl:value-of select='brand'/></li>  
    </xsl:template>

</xsl:stylesheet>
The output generated:
Code:
<html>
  <body>
    <ul /> 
  </body>
</html>
(1) The first thing I'm not quite sure about is if the last two templates are conflicting. Looking at the output it seems that way, but I would have expected, that a more specific match (like the car match) has higher precedence than a more general match (*). Could anyone comment on this?

(2) Under the assumption that the two templates are conflicting, I understand that it depends on the implementation of the XSLT processor if an error is reported or the last of the conflicting templates is chosen. Since my XSLT processor (= XMLSpy) does not report an error, the last matching template should be chosen.

So, in either case I would expect the following output, because
if the templates are conflicting, the third template should be chosen and thus "BMW" should be written to the destination tree and the other cars are not. If, on the other hand, the two templates are not conflicting, then "BMW" should be written to the destination tree by the third template and the other cars match the second template which does nothing.

Code:
<html>
  <body>
    [list]
      <li>BMW</li>
    </ul> 
  </body>
</html>
To summarize, my questions are
(1) Are the "<xsl:template match="*"/>" and "<xsl:template match="//car[type='335i']">" templates and conflicting?

(2) Why does the XSLT-script produce an empty list?

I really want to know what is happening here and hoping for your help ;)

Regards,
Alex

Reply With Quote