George E
George E

Reputation: 33

XSLT replace an element that exists inside another element value

I am new to XSLT (using XSLT v1.0) and I have the following input:

<SUMMARY>
<TITLE>Lorem ipsum dolor <QUOTE ID="replace with this string"/> sit vel eu. 
</TITLE>
<P> Lorem ipsum dolor <QUOTE ID="replace with this string"/> sit vel eu. </P>
</SUMMARY>

<REFERENCE>
<TI>Lorem ipsum dolor <QUOTE ID="replace with this string"/> sit vel eu. 
</TI>
<P> Lorem ipsum dolor <QUOTE ID="replace with this string"/> sit vel eu. </P>
</REFERENCE>

How could I replace all the occurrences of the element QUOTE inside my XML input, with a String that is the value of the QUOTE/ID attribute.

Upvotes: 3

Views: 883

Answers (1)

kjhughes
kjhughes

Reputation: 111726

Add to the identity transform a special template to handle QUOTE:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="QUOTE">
      <xsl:value-of select="@ID"/>
  </xsl:template>

</xsl:stylesheet>

The identity transformation will copy everything to the output XML, and the special QUOTE template will copy over the value of its @ID attribute in place of the QUOTE element.

Upvotes: 1

Related Questions