xsl:apply-templates:匹配不受尊重

时间:2011-11-01 17:44:45

标签: xslt

我遇到了xsl:apply-templates的问题。我试图将模板应用于一个特定的标签,但我看到其他标签的文本。一个简单的xml文件:

<?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>                                                      

输入是:

<?xml version="1.0"?>

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

输出结果为:

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

我期待输出

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

(我现在不关心间距问题)

我可以解决使用xsl:for-each包装xsl:apply-templates的问题:

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

但我真的不喜欢这个解决方案。还有更好的方法吗?

1 个答案:

答案 0 :(得分:3)

match元素上不允许使用属性xsl:apply-templates。在match中将select更改为xsl:apply-templates,然后重试。