xslt remove string and special character from number in xml

时间:2016-04-25 08:50:31

标签: regex xml xslt xslt-1.0 xslt-2.0

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>

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

在XSLT 2.0中,您可以简单地执行:

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

删除任何非数字字符。

即使在XSLT 1.0中,您也可以使用:

substring-after(CustNo, ':')

以适应您的示例。