asp.net – 根据参数应用不同的XSLT模板
发布时间:2020-05-22 17:02:53 所属栏目:asp.Net 来源:互联网
导读:是)我有的? 我有一个ASP.NET项目,其中有一个XSLT文件,定义了许多模板.根据用户选择,一次只能使用一个模板,以在网页上显示内容.它看起来像这样: xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform xsl:param name=TemplateName/xs
|
是)我有的? 我有一个ASP.NET项目,其中有一个XSLT文件,定义了许多模板.根据用户选择,一次只能使用一个模板,以在网页上显示内容.它看起来像这样: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="TemplateName"></xsl:param>
<xsl:template match="Title_Only">
...template Title_Only body...
</xsl:template>
<xsl:template match="Image_Only">
...template Image_Only body...
</xsl:template>
<xsl:template match="Title_And_Image">
...template Title_And_Image body...
</xsl:template>
</xsl:stylesheet>
我想要的是? 我想将模板名称TemplateName作为参数传递,并能够在数据上应用相应的模板. 有人可以告诉我如何实现这一目标? 解决方法您不能在XSLT 1.0中的匹配模式中使用param或变量的值.但是,您可以从单个模板有条件地应用模板,如下所示:<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="TemplateName"/>
<xsl:template match="/">
<xsl:apply templates select="something[some_condition=$TemplateName]"/>
</xsl:template>
</xsl:stylesheet>
…然后只需设置模板以分别匹配每种类型的节点.模板将仅应用于与您的选择表达式匹配的内容,该表达式基于参数. 有条件地应用模板的示例 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="TemplateName" select="'Title_Only'" />
<xsl:template match="/">
<xsl:apply-templates select="test/val[@name=$TemplateName]" />
</xsl:template>
<xsl:template match="val">
<xsl:value-of select="@name" />
</xsl:template>
</xsl:stylesheet>
适用于此输入: <test>
<val name="Title_Only" />
<val name="Image_Only" />
<val name="Title_And_Image" />
</test>
生产: Title_Only …基于$TemplateName的值. (注意,此示例使用变量,但想法是相同的.) 使用模式分离模板 如果在每种情况下确实需要完全不同的模板,请使用模式.这个样式表: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="TemplateName" select="'Title_Only'" />
<xsl:template match="/">
<xsl:choose>
<xsl:when test="$TemplateName='Title_Only'">
<xsl:apply-templates select="test/val" mode="Title_Only" />
</xsl:when>
<xsl:when test="$TemplateName='Image_Only'">
<xsl:apply-templates select="test/val" mode="Image_Only" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="test/val" mode="Title_And_Image" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="val" mode="Title_Only">
<xsl:value-of select="@title" />
</xsl:template>
<xsl:template match="val" mode="Image_Only">
<xsl:value-of select="@img" />
</xsl:template>
<xsl:template match="val" mode="Title_And_Image">
<xsl:value-of select="@title" />/
<xsl:value-of select="@img" />
</xsl:template>
</xsl:stylesheet>
适用于此来源: <test>
<val title="some title" img="some image"/>
</test>
生产: some title 基于参数的值使用期望的模板. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- .net – Application_Error不会触发?
- asp.net – Gridview行编辑 – 动态绑定到DropDownList
- ASP.NET Web Api:如何使用URL参数传递访问令牌(oAuth 2.0)
- asp.net-mvc – VaryByParam =“*”是否也读取了RouteData.
- asp.net-mvc – 使用ASP.NET MVC支持“Expect:100-continu
- asp.net-mvc-3 – 如何在剃须刀视图中设置复选框?
- iis-7 – 经典ASP站点请求在IIS7中随机挂起
- asp.net – 在另一个控件之前插入控件
- asp.net-mvc-3 – 为什么ValidationSummary(true)显示属性错
- asp.net – System.Web.Cache,会话级别或应用程序级别
推荐文章
站长推荐
热点阅读
