获取xsl模板中的参数值

时间:2014-04-11 08:20:21

标签: xml xslt xslt-2.0

我将参数传递给xsl模板,但它没有在那里显示它的值:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<information>
    <person>
        <name>John</name>
    </person>
    <person>
        <name>Joseph</name>
    </person>
    <person>
        <name>Ajay</name>
    </person>
</information>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/">
   <html>
   <body>

   <xsl:call-template name="show_name">
     <xsl:with-param name="element" select=" 'hie' "/>
   </xsl:call-template>

   </body>
   </html>
 </xsl:template>


<xsl:template name="show_name" match="/">
   <xsl:param name="element" />
   <xsl:for-each select="information/person">
     <p>Name: <xsl:value-of select="$element" /></p>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet> 

输出:

Name:
Name:
Name:

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您有两个与文档节点"/"匹配的模板。当两个具有相等优先级的模板匹配相同的东西时,这被认为是XSLT中的错误。可能发生的事情是您的特定处理器实际上并没有抛出错误,但总是选择第二个模板以匹配“/”,因此第一个模板未被使用。

无论如何,你的第二个模板只需要作为命名模板运行,所以不要这样做

 <xsl:template name="show_name" match="/">

这样做

<xsl:template name="show_name">
相关问题