XConfusion
XConfusion

Reputation: 345

conditional replace in xml using xslt

I have an xml of type

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

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

My task is to check the C1 attribute, and if it is "xyz", replace the text "replace" with "New Text". Any ideas?

Upvotes: 0

Views: 1136

Answers (1)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

<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>

The rest of the stylesheet can be the identity transform.

Upvotes: 1

Related Questions