asp.net – 根据参数应用不同的XSLT模板
发布时间:2020-09-21 14:35:36 所属栏目: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 – 无法加载文件或程序集App_Licenses
- asp.net-mvc – LabelFor和TextBoxFor不生成相同的id
- asp.net – 如何打破VB.NET中的“if”块
- asp.net-mvc – ASP.Net MVC – HTTP状态代码(即303,401,40
- 如何排序. .NET中的resx(资源文件)
- asp.net-mvc-4 – 如何开发一个ASP.NET Web API接受一个复杂
- asp.net – MS Chart for .NET预定义调色板颜色列表?
- asp.net – 有谁知道如何摆脱我的.net网络服务的jsdebug请求
- asp.net – 我们可以在视图状态中放置哪些类型的对象?
- 用ADO.NET处理层次数据
推荐文章
站长推荐
热点阅读