使用xslt在xml中进行条件替换

时间:2013-07-05 18:02:54

标签: xml xpath xml-parsing xslt-1.0 xslt-2.0

我有一个类型为

的xml
<A>
    <B1>
        <C1 attri= "xyz"/>
    </B1>

    <B2>
        <C2>
            <D2> replace </D2>
        </C2>
    </B2>
<A>

我的任务是检查C1属性,如果是“xyz”,则将文本“replace”替换为“New Text”。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

<xsl:template match="D2">
  <xsl:copy>
    <xsl:choose>
      <xsl:when test="/A/B1/C1@attri = 'xyz'">
        <xsl:text>New Text</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

样式表的其余部分可以是身份转换。

相关问题