asp.net - 根据参数应用不同的 XSLT 模板

标签 asp.net xslt

我拥有的?

我有一个 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 的匹配模式中使用参数或变量的值。但是,您可以从单个模板有条件地应用模板,如下所示:

<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>

...然后只需设置模板以分别匹配每种类型的节点。模板将仅应用于与您匹配的内容 select表达式,它基于参数。

有条件地应用模板的示例
<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 - 根据参数应用不同的 XSLT 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5587197/

相关文章:

xml - 如何仅使用 XSLT 提取 "for eached"元素的子元素

asp.net - Jquery 在 asp.net webforms 中显示成功消息

c# - 如何检查文件夹中的文件名

javascript - 如何在 C# 中调用 javascript 函数

xml - CDATA 存在的原因是什么?

xslt - 使用 XSL-FO、Apache FOP 将元素右对齐

c# - 清理字符串以防止相对 URI 路径

c# - 单个 ASPX 页面 - 无代码隐藏 - 页面指令

xml - 使用 XSLT 将 XML namespace 转换为顶级前缀

xml - 将命名空间添加到在其子项中具有命名空间的 xml,并通过 xslt 在其顶部添加新元素