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

XSL模板能否在*ALL*模式下匹配?

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

    编辑:使用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 年前

    有没有一种方法可以编写XSL1.0 模板在所有方面都是匹配的

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

    1. 编写模板 .

    2. 在moded模板中有一个 <xsl:apply-templates> 没有模式属性

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

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

    总结 :一组不同模式的模板仍然可以发布 <xsl:apply templates>

        3
  •  1
  •   Alex    16 年前

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