3

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That was exactly what i needed. Thank you that made XSLT much more clear to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.