2010-07-16 90 views
3

我想基本上用XSLT模板重新创建ASP.NET母版页的功能。我可以将xslt模板的结果作为参数传递给另一个模板吗?

我有一个“母版页”模板,其中包含存储在.xslt文件中的大部分页面html。我有另一个特定于单个页面的.xslt文件,它需要用XML来表示页面数据。我想从我的新模板中调用母版页模板,并且仍然可以插入我自己的将应用的xml。如果我可以传递一个允许我以参数作为名称来调用模板的参数,那就可以做到这一点,但这似乎不被允许。

基本上我有这样的:

<xsl:template name="MainMasterPage"> 
    <xsl:with-param name="Content1"/> 
    <html> 
    <!-- bunch of stuff here --> 
    <xsl:value-of select="$Content1"/> 
    </html> 
</xsl:template> 

而且这样的:

<xsl:template match="/"> 
    <xsl:call-template name="MainMasterPage"> 
    <xsl:with-param name="Content1"> 
     <h1>Title</h1> 
     <p>More Content</p> 
     <xsl:call-template name="SomeOtherTemplate"/> 
    </xsl:with-param> 
    </xsl-call-template> 
</xsl:template> 

什么情况是,嵌套的XML基本上剥离和所有插入的 “TitleMore内容”

+0

好问题(+ 1)。请参阅我的回答以解释问题并寻求正确的解决方案。 – 2010-07-17 03:31:57

回答

5

提供的代码的问题在这里:

<xsl:value-of select="$Content1"/> 

这将输出任一的$Content1顶部节点(如果它包含一个文件)或它的第一个元素或文本子的字符串值的所有文本节点的后代的级联(如果它是一个XML片段)。

您需要使用的

<xsl:copy-of select='$pContent1'>

,而不是

<xsl:value-of select='$pContent1'>

这正确的副本$pContent1

下的所有子节点是一个修正后的变换

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/"> 
    <xsl:call-template name="MainMasterPage"> 
    <xsl:with-param name="pContent1"> 
     <h1>Title</h1> 
     <p>More Content</p> 
     <xsl:call-template name="SomeOtherTemplate"/> 
    </xsl:with-param> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="MainMasterPage"> 
    <xsl:param name="pContent1"/> 
    <html> 
    <!-- bunch of stuff here --> 
    <xsl:copy-of select="$pContent1"/> 
    </html> 
</xsl:template> 

<xsl:template name="SomeOtherTemplate"> 
    <h2>Hello, World!</h2> 
</xsl:template> 
</xsl:stylesheet> 

当这种转变是在任何XML文档(未使用),想要的,正确的应用结果产生

<html> 
    <h1>Title</h1> 
    <p>More Content</p> 
    <h2>Hello, World!</h2> 
</html> 
+0

+1,很好的答案。是否可以使用'apply-templates'(使用相同的'select')代替'copy-of',将模板应用于中间结果?我一直试图让这个工作一段时间,并没有抱怨,但结果只是空的。 – falstro 2011-02-03 09:55:08

+0

@roe:是的,但只有当要应用的模板等同于标识转换时,apply-templates才相当于复制。 – 2011-02-03 13:40:09

+0

我明白了,我的问题是我试图执行转换,然后在该结果上进行另一个转换(一个转换修改文本内容,插入零宽度空格字符,第二个转换完成标记例如fo-inline块),但由于某种原因,结果是空的。 – falstro 2011-02-03 13:42:13

相关问题