我试图基本上使用XSLT模板重新创建ASP.NET母版页的功能.
我有一个"母版页"模板,其中包含存储在.xslt文件中的大部分页面html.我有另一个特定于单个页面的.xslt文件,它接受表示页面数据的xml.我想从我的新模板中调用母版页模板,并且仍然能够插入我自己的xml将被应用.如果我能通过设置了一个param,让我打电话给模板,帕拉姆作为名称,即会做的伎俩,但似乎并没有被允许.
基本上我有这个:
<xsl:template name="MainMasterPage">
<xsl:with-param name="Content1"/>
<html>
<!-- bunch of stuff here -->
<xsl:value-of select="$Content1"/>
</html>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
还有这个:
<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>
Run Code Online (Sandbox Code Playgroud)
会发生什么是嵌套的xml基本上被剥离,所有插入的都是"TitleMore内容"
提供的代码的问题在这里:
Run Code Online (Sandbox Code Playgroud)<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>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于任何XML文档(未使用)时,将生成所需的正确结果:
<html>
<h1>Title</h1>
<p>More Content</p>
<h2>Hello, World!</h2>
</html>
Run Code Online (Sandbox Code Playgroud)