Ernesto Jorge
Ernesto Jorge

Reputation: 25

XSL replace special character by a String

I am new to XSL and I have the following XML node that I need to parse to regular text:

<GeneralRemark Source="A3" SourceRef="LD8PN5">
    <ElementNumber Source="A3" SourceElement="14">9</ElementNumber>
    <Text>SPECIAL CHARACTER € EURO SIGN</Text>
</GeneralRemark>

I need to read the data in text node, and in the output replace € by EUR. My app does not support €, so I can't use it.

This is how I am reading the GeneralRemark node now:

<xsl:if test="../PNRViewRS/GeneralRemark/Text">
    <xsl:for-each select="../PNRViewRS/GeneralRemark/Text">
        <xsl:text>RM </xsl:text>
        <xsl:value-of select="."/>                  
        <xsl:value-of select="$cr"/>                        
    </xsl:for-each>
</xsl:if>

Any tips on how to approach this will be appreciated.

Upvotes: 0

Views: 48

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117100

If the text always contains exactly one occurrence of the pattern you want to replace, you could use:

<xsl:value-of select="substring-before(., '€')"/> 
<xsl:text>EUR</xsl:text>                 
<xsl:value-of select="substring-after(., '€')"/> 

Otherwise you will need to call a named recursive template to replace all possible occurrences of the pattern. See an example here: https://stackoverflow.com/a/30339654/3016153

Upvotes: 1

Related Questions