user1024203
user1024203

Reputation: 55

xsl:apply-templates: match not being respected

I'm having a problem with xsl:apply-templates. I am attempting to apply a template to one particular tag, but I am seeing text from other tags. A simple xml file:

<?xml version="1.0"?>                                                  

<!-- execute with xsltproc foo.xsl foo.xml -->                         
<xsl:stylesheet version="1.0"                                          
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >     

    <xsl:output method="text" />                                       

    <xsl:template match="/foo">                                        
        <xsl:for-each select="bar">                                    
            <xsl:value-of select="grill"/>                             
            <xsl:apply-templates match="baz"/>                         
        </xsl:for-each>                                                
    </xsl:template>                                                    

    <xsl:template match="foo">[<xsl:value-of select="." />|http://example.com/<xsl:value-of select="." />]</xsl:template>                     
</xsl:stylesheet>                                                      

The input is:

<?xml version="1.0"?>

<foo>
    <bar>
        <baz>a <foo>b</foo> c</baz>
        <grill>grill</grill>
    </bar>
</foo>

The output is:

grill
        a [b|http://example.com/b] c
        grill

I was expecting the output to be

grill
a [b|http://example.com/b] c

(I don't care about the spacing problems for now)

I can get around the problem with wrapping the xsl:apply-templates with a xsl:for-each:

<xsl:for-each select="grill">
    <xsl:apply-templates match="grill"/>
</xsl:for-each>

But I really don't like this solution. Is there a better way?

Upvotes: 2

Views: 297

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52888

The attribute match isn't allowed on the xsl:apply-templates element. Change match to select in the xsl:apply-templates and try it again.

Upvotes: 3

Related Questions