DHANU RACHURI
DHANU RACHURI

Reputation: 1

Replace & with "and" in xslt

I am trying to work on an XSLT where I want to replace all occurrences of special character & with and in XML. One of the few things I have tried

<xsl:element name="ext_udf_str1">
<xsl:value-of select="replace(/*:SyncCLShipment/*:DataArea/*:CLShipment/*:Shipment/*:ShipmentHeader/*:PickMsgLine1,'&amp;','and')"/>
</xsl:element>

Upvotes: 0

Views: 114

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52858

Your replace() should work, but there's no way to know why it doesn't because you haven't given a minimal, complete, and verifiable example.

However, if you're using replace() you're using at least XSLT 2.0 so using a character map is another option.

Example (This is XSLT 3.0. It works in 2.0 if you replace the xsl:mode with the identity transform template.):

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" use-character-maps="so"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:character-map name="so">
        <xsl:output-character character="&amp;" string="and"/>
    </xsl:character-map>
    
    <xsl:mode on-no-match="shallow-copy"/>
    
</xsl:stylesheet>

Fiddle: http://xsltfiddle.liberty-development.net/jyyho7z/2

Upvotes: 1

Related Questions