【问题标题】:Combine multiple XSLT templates with similar matches将多个具有相似匹配项的 XSLT 模板组合起来
【发布时间】:2021-05-01 11:29:11
【问题描述】:

我在下面有这个 XSLT

<xsl:template match="word[@italic = 'y']">
        <p>
                <xsl:attribute name="i">yes</xsl:attribute>
                <xsl:apply-templates/>
        </p>
</xsl:template>

<xsl:template match="word[@bold = 'y']">
        <p>
                <xsl:attribute name="b">yes</xsl:attribute>
                <xsl:apply-templates/>
        </p>
</xsl:template>

<xsl:template match="word[@underline = 'y']">
        <p>
                <xsl:attribute name="u">yes</xsl:attribute>
                <xsl:apply-templates/>
        </p>
</xsl:template>

有没有办法将这些模板组合在一个嵌套块中,使用类似于“斜体 | 粗体 | 下划线”的变量,同时还反映 中的变化?谢谢。

【问题讨论】:

    标签: xml xslt xml-parsing xslt-2.0 xslt-grouping


    【解决方案1】:

    组合匹配模式的常用方法是

    <xsl:template match="word[@italic = 'y'] | word[@bold = 'y'] | word[@underline = 'y']">
    

    至于转换属性,你就不能用吗

    <xsl:template match="word">
      <p>
        <xsl:apply-templates select="@* | node()"/>
      </p>
    </xsl:template>
    

    为属性加上模板,例如

    <xsl:template match="word/@italic[. = 'y']">
      <xsl:attribute name="i">yes</xsl:attribute>
    </xsl:template>
    

    等等?

    或许

    <xsl:template match="word/@italic[. = 'y'] | word/@bold[. = 'y'] | word/@underline[. = 'y']">
      <xsl:attribute name="{substring(local-name(), 1, 1)}">yes</xsl:attribute>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 2016-04-13
      相关资源
      最近更新 更多