我正在制作一个 Web 应用程序,其中页面是 xml,并且它们使用 xslt 样式表来创建 xhtml 输出。
因此,许多页面布局将包含在 XSLT 样式表中,从某种意义上说,XSLT 表包含有关页面布局的所有常见信息,是否可以为 xslt 创建 ASP.NET 母版页床单??基本上,我有一个生成 xml 的 aspx 页面,并包含另一个 aspx 页面,该页面是一个 xslt 工作表,使用母版页插入页面上的任何内容更改 [仅中间 Pane ]。
此外,您认为这是一个好的设计策略吗?
最佳答案
这就是我所说的“填空”技术,是的,这是一种非常好的设计模式,它允许将表示和处理逻辑分开,并且能够无需更改转换即可拥有多种不同的输出格式。
下面是使用此技术的完整且非常简短的示例:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pMasterPage" select=
"'file:///c:/temp/delete17.xml'"/>
<xsl:variable name="vDoc" select="/"/>
<xsl:template match="node()|@*">
<xsl:param name="pInput"/>
<xsl:copy>
<xsl:apply-templates select="node()|@*">
<xsl:with-param name="pInput" select="$pInput"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="document($pMasterPage)/*">
<xsl:with-param name="pInput" select="$vDoc"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="first-name|last-name|age">
<xsl:param name="pInput"/>
<xsl:value-of select="$pInput/*/*[name()=name(current())]"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于以下 XML 文档时:
<person>
<first-name>John</first-name>
<last-name>Smith</last-name>
<age>23</age>
<nationality>British</nationality>
</person>
如果文件:c:\temp\delete17.xm
l 包含此内容:
<html>
<h1>Person:</h1>
<p>First Name: <first-name/></p>
<p>Last Name: <last-name/></p>
<p>Age: <age/></p>
</html>
然后产生以下结果:
<html>
<h1>Person:</h1>
<p>First Name: John</p>
<p>Last Name: Smith</p>
<p>Age: 23</p>
</html>
浏览器显示为:
人:
名字:约翰
姓:史密斯
年龄:23
关于asp.net - XSLT 母版页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5940893/