user1358072
user1358072

Reputation: 447

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>

Upvotes: 0

Views: 2649

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117140

In XSLT 2.0, you could do simply:

replace(CustNo, '\D', '')

to remove any non-digit characters.

Even in XSLT 1.0, you could use:

substring-after(CustNo, ':')

to accommodate both your examples.

Upvotes: 1

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

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

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

Upvotes: 0

Related Questions