asp.net – 根据参数应用不同的XSLT模板
发布时间:2020-09-21 13:45:37 所属栏目: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 基于参数的值使用期望的模板. (编辑:鄂州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – MS Chart for .NET预定义调色板颜色列表?
- asp.net-mvc – ASP.NET捆绑/分类:包括动态生成的Javascri
- asp.net – WebFormsMVP的缺点?
- 并行运行ASP.NET Webforms和ASP.NET MVC
- asp.net-mvc – MicrosoftMvcValidation.js VS jquery.vali
- 在ASP.NET核心中间件中设置响应状态
- asp.net 上传或下载当文件名包含有特殊字符#的处理
- asp.net-mvc-3 – Azure网站上的RavenDb – 访问被拒绝
- asp.net-mvc-4 – 如何防止复杂类型的默认绑定器?
- asp.net 使用驻留在页面中的Cache缓存常用可定时更新的数据