XSLT 替换属性值和文本节点中的文本 [英] XSLT replacing text in attribute value and text nodes

查看:37
本文介绍了XSLT 替换属性值和文本节点中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 文档,当该值出现在文本节点或名为 message 的属性中时,我正在尝试转换该文档并对某些值进行字符串替换.我的 xsl 文件在下面,但主要问题是当替换发生在 message 属性中时,它实际上替换了整个属性,而不仅仅是该属性的值,所以

I have an XML document that I'm trying to transform and do a string replace of certain values when that value occurs in either a text node or an attribute named message. My xsl file is below, but the main issue is that when the replace occurs in the message attribute, it actually replaces the whole attribute and not just the value of that attribute, so

<mynode message="hello, replaceThisText"></mynode>

变成

<mynode>hello, withThisValue</mynode>

代替

<mynode message="hello, withThisValue"></mynode>

当文本出现在像

<mynode>hello, replaceThisText</mynode>

然后它按预期工作.

我没有做过大量的 XSLT 工作,所以我有点卡在这里.任何帮助,将不胜感激.谢谢.

I haven't done a ton of XSLT work, so I'm a bit stuck here. Any help would be appreciated. Thanks.

<xsl:template match="text()|@message">
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text"><xsl:value-of select="."/></xsl:with-param>
        <xsl:with-param name="replace" select="'replaceThisText'"/>             
        <xsl:with-param name="by" select="'withThisValue'"/>
    </xsl:call-template>
</xsl:template>

<!-- string-replace-all from http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx -->
<xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)" />
        <xsl:value-of select="$by" />
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text"
          select="substring-after($text,$replace)" />
          <xsl:with-param name="replace" select="$replace" />
          <xsl:with-param name="by" select="$by" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>

推荐答案

你的模板匹配一个属性,但它不输出一个.

Your template matches an attribute, but it doesn't output one.

请注意,属性的值不是节点.因此,您必须为文本节点和属性使用单独的模板:

Note that the value of an attribute is not a node. Therefore, you must use separate templates for text nodes and for the attribute:

<xsl:template match="text()">
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="."/>
        <xsl:with-param name="replace" select="'replaceThisText'"/>             
        <xsl:with-param name="by" select="'withThisValue'"/>
    </xsl:call-template>
</xsl:template>

<xsl:template match="@message">
    <xsl:attribute name="message">
        <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="."/>
            <xsl:with-param name="replace" select="'replaceThisText'"/>             
            <xsl:with-param name="by" select="'withThisValue'"/>
        </xsl:call-template>
    </xsl:attribute>
</xsl:template>

这篇关于XSLT 替换属性值和文本节点中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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