代码之家  ›  专栏  ›  技术社区  ›  Dirk Vollmar

XSL模板可以在*ALL*模式下匹配吗?

  •  14
  • Dirk Vollmar  · 技术社区  · 16 年前

    有没有一种方法可以编写一个在所有模式下都匹配的XSL 1.0模板?

    或者,我是否必须为每个现有模式编写一个单独的模板(包括未来添加的模式的其他模板)?

    以下是我所拥有的:

    <xsl:apply-templates mode="mode1" />
        ...
    <xsl:apply-templates mode="mode2" />
        ...
    <!-- Do not process text content of nodes no matter in what mode -->
    <!-- Is there a way to have only one template here? -->
    <xsl:template match="text()" mode="mode1" />
    <xsl:template match="text()" mode="mode2" />
    
    3 回复  |  直到 16 年前
        1
  •  7
  •   annakata    16 年前

    预定义模式: #all (但仅在XSLT 2.0中可用)。

    编辑:用1.0复制共享模式行为

    <xsl:template match="/">
        <xsl:variable name="choice" select="'a'"/><!-- input seed here -->
        <xsl:choose>
            <xsl:when test="$choice='a'">
                <xsl:apply-templates mode="a"/>
            </xsl:when>
            <xsl:when test="$choice='b'">
                <xsl:apply-templates mode="b"/>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="*" mode="a">
        [A]
        <xsl:apply-templates />
    </xsl:template>
    
    <xsl:template match="*" mode="b">
        [B]
        <xsl:apply-templates />
    </xsl:template>
    
    <xsl:template match="text()">
        [ALL]
    </xsl:template>
    
        2
  •  2
  •   Dimitre Novatchev    16 年前

    是否有方法编写XSL 1.0 全部匹配的模板 模式

    是的,为了做到这一点,应该遵循这两条规则 :

    1. 编写模板 没有模式属性 .

    2. 在模式模板中有一个 <xsl:apply-templates> 用法说明 没有模式属性 这将导致模板为1。以上被选中进行处理

    这直接源于 XSLT 1.0 规格, which says :

    如果A xsl:apply-templates 元素具有 一 mode 属性,则仅适用 这些模板规则来自 xsl:template 具有a的元素 模式 具有相同值的属性;如果a xsl:应用模板 元素不 有一个...,有... 模式 属性,然后应用 仅限于来自的模板规则 xsl:template 没有的元素 一 模式 属性。

    总结 :仍然可以发布一组不同模式的模板 <xsl:apply模板> 以这种方式(如上所述),以便在每种情况下选择相同的特定单个模板进行处理。

        3
  •  1
  •   Alex    16 年前

    如果你想让模板在所有模式下都匹配,那么为什么要使用模式?如果你不使用模式,那么模板将一直被使用。模式的原因是有条件地对同一数据类型执行不同的操作。看起来你想要无模特。

    推荐文章