根据参数应用不同的 XSLT 模板 [英] Applying different XSLT template depending on a parameter

查看:34
本文介绍了根据参数应用不同的 XSLT 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么?

我有一个 ASP.NET 项目,其中有一个 XSLT 文件,其中定义了许多模板.一次仅使用一个模板,具体取决于用户选择以在网页上显示内容.它看起来像这样:

I am have an ASP.NET project in which, I have an XSLT file with many templates defined. Only one template will be used at a time depending on the user selection to display the content on a web page. It looks something like this:

    <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 作为参数传递,并能够将相应的模板应用于数据.

I want to pass the template name TemplateName as a parameter and to be able apply the respective template on the data.

有人可以告诉我如何实现这一目标吗?

Can someone please tell me how can I achieve this?

推荐答案

您不能在 XSLT 1.0 的匹配模式中使用参数或变量的值.但是,您可以从单个模板有条件地应用模板,如下所示:

You cannot use the value of a param or variable in a match pattern in XSLT 1.0. However, you could apply templates conditionally from a single template, like this:

<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 表达式匹配的内容.

...and then just set-up templates to match each type of node individually. Templates will be applied only to those things that match your select expression, which is based on the param.

有条件应用模板的示例

<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 的值.(注意这个例子使用了一个变量,但是思路是一样的.)

...based on the value of $TemplateName. (Note that this example uses a variable, but the idea is the same.)

使用模式分离模板

如果您确实需要在每种情况下使用完全不同的模板,请使用模式.此样式表:

If you really need an entirely different template in each case, then use modes. This 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: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

根据参数的值使用所需的模板.

The desired template is used, based on the value of the param.

这篇关于根据参数应用不同的 XSLT 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆