将自定义 css 类插入到 xslt 变量中 [英] Inserting custom css class into a xslt variable

查看:31
本文介绍了将自定义 css 类插入到 xslt 变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有值的 xsl 变量

I have a xsl variable that has value

<代码>

<a href="https://www.google.com" target="_blank">说明技巧</a>

<p> <a href="https://www.google.com" target="_blank">Illustrating tips</a> </p>

如何插入一个 css 类来生成变量的值,如下所示.我也想删除段落标签.

How can I insert a css class to make the value of the variable, which will look like below. I also want to remove the paragraph tags.

<a href="https://www.google.com" target="_blank" class="titleClass">Illustrating tips</a>

推荐答案

假设你有

<xsl:variable name="frag">
  <p> <a href="https://www.google.com" target="_blank">Illustrating tips</a> </p>
</xsl:variable>

然后使用 mode 来转换它,例如

then use a mode to transform it, e.g.

<xsl:template match="p" mode="m1">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="a[@href]" mode="m1">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:attribute name="class">titleClass</xsl:attribute>
    <xsl:apply-templates mode="m1"/>
  </xsl:copy>
</xsl:template>

<xsl:variable name="transformed-frag">
  <xsl:apply-templates select="$frag/node()" mode="m1"/>
</xsl:variable>

或者直接输出结果

<xsl:apply-templates select="$frag/node()" mode="m1"/>

如果 a 元素可以包含多个纯文本,您将需要为它们添加模板以在模式中复制它们,例如通用身份转换模板或该模式下的身份转换模板例如

If the a element can contain more than plain text, you will need to add templates for them copying them in the mode, like a general identity transformation template or like an identity transformation template in that mode e.g.

<xsl:template match="@* | node()" mode="m1">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()" mode="#current"/>
  </xsl:copy>
</xsl:template>

这篇关于将自定义 css 类插入到 xslt 变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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