Search code examples
regexxmlxsltxslt-1.0xslt-2.0

xslt remove string and special character from number in xml


During xslt transformation, I have tried to clean string and special from customer number, but it doesnt work. I need to remove c: or s: from the number. Thanks :)

Xml File 1

<Data>
 <Request>
   <CustNo>c:222</CustNo>
 </Request>
</Data>

Xml File 2

<Data>
 <Request>
   <CustNo>s:333</CustNo>
 </Request>
</Data>

XSLT

        <?xml version="1.0" encoding="iso-8859-1" ?>
        <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" >
            <xsl:output method="xml" version="1.0" indent="yes"/>
            <xsl:template match="Request" >
                <TestPayment>
                    <Transactions>
                        <Transaction  custno="{CustNo}" >           
                        </Transaction>
                    </Transactions>
                </TestPayment>
            </xsl:template>
             <xsl:template match="Request/CustNo">
                    <xsl:value-of select="regex=(^c:[a-zA-Z0-9]*$,^s:[a-zA-Z0-9]*$ )"/>
             </xsl:template>
        </xsl:template>
        </xsl:stylesheet>

Result of Xml File 1:

    <?xml version="1.0" encoding="utf-8"?>
    <TestPayment>
      <Transactions>
        <Transaction custno="222">
        </Transaction>
      </Transactions>
    </TestPayment>

Result of Xml File 2:

    <?xml version="1.0" encoding="utf-8"?>
    <TestPayment>
      <Transactions>
        <Transaction custno="333">
        </Transaction>
      </Transactions>
    </TestPayment>

Solution

  • why don't you try a double-translate function?

    <Transaction custno="{translate(CustNo, translate(CustNo, '0123456789', ''), '')}" >