将多个具有相似匹配项的 XSLT 模板组合在一起
Combine multiple XSLT templates with similar matches
我在下面有这个 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>
有没有办法将这些模板合并到一个嵌套块中,使用一个看起来像“斜体 | 粗体 | 下划线”的变量,同时也反映 中的变化?谢谢
组合匹配模式的常用方法是
<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>
我在下面有这个 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>
有没有办法将这些模板合并到一个嵌套块中,使用一个看起来像“斜体 | 粗体 | 下划线”的变量,同时也反映
组合匹配模式的常用方法是
<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>