Friday, 15 August 2014

xml - How to Convert String into decimal in XSLT 2.0 to last two Decimal places -



xml - How to Convert String into decimal in XSLT 2.0 to last two Decimal places -

i want convert string value decimal using xslt 2.0 lastly 2 decimal places. want alter value of country code element decimal.the input string length fixed i.e 11.

input xml:

<getpersonresponse xmlns="http://www.example.com/xsd/person_01_requestresponse_001"> <person xsi:type="ns1:person" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <ns2:personid xmlns:ns2="http://www.example.com/xsd/person_01">4224</ns2:personid> <ns3:firstname xmlns:ns3="http://www.example.com/xsd/person_01">omer</ns3:firstname> <ns4:lastname xmlns:ns4="http://www.example.com/xsd/person_01">khalid</ns4:lastname> <ns5:fathername xmlns:ns5="http://www.example.com/xsd/person_01">khalid</ns5:fathername> <ns6:religion xmlns:ns6="http://www.example.com/xsd/person_01">islam</ns6:religion> <ns7:mothertongue xmlns:ns7="http://www.example.com/xsd/person_01">urrdu</ns7:mothertongue> <ns8:dateofbirth xmlns:ns8="http://www.example.com/xsd/person_01">1993-02-02</ns8:dateofbirth> <ns9:gender xmlns:ns9="http://www.example.com/xsd/person_01" xsi:type="ns1:gendertype">male</ns9:gender> <ns10:currentaddress xmlns:ns10="http://www.example.com/xsd/person_01" xsi:type="ns1:addressdetail"> <ns10:addressdetailid>194</ns10:addressdetailid> <ns10:streetno>5</ns10:streetno> <ns10:houseno>361</ns10:houseno> <ns10:town>johar</ns10:town> <ns10:district>lahore</ns10:district> <ns10:city>lahore</ns10:city> <ns10:state>punjab</ns10:state> <ns10:country>pakistan</ns10:country> <ns10:postal>54046543245</ns10:postal> </ns10:currentaddress> <ns11:homeaddress xmlns:ns11="http://www.example.com/xsd/person_01" xsi:type="ns1:addressdetail"> <ns11:addressdetailid>195</ns11:addressdetailid> <ns11:streetno>5</ns11:streetno> <ns11:houseno>361</ns11:houseno> <ns11:town>johar</ns11:town> <ns11:district>lahore</ns11:district> <ns11:city>lahore</ns11:city> <ns11:state>punjab</ns11:state> <ns11:country>pakistan</ns11:country> <ns11:postal>54046543245</ns11:postal> </ns11:homeaddress> <ns12:height xmlns:ns12="http://www.example.com/xsd/person_01">6</ns12:height> <ns13:weight xmlns:ns13="http://www.example.com/xsd/person_01">100</ns13:weight> <ns14:cnic xmlns:ns14="http://www.example.com/xsd/person_01">35302</ns14:cnic> <ns15:contactinfo xmlns:ns15="http://www.example.com/xsd/person_01" xsi:type="ns1:contactdetail"> <ns15:homephone>454545</ns15:homephone> <ns15:cellphone>3343434</ns15:cellphone> <ns15:workplacephone>34343434</ns15:workplacephone> <ns15:email>omer@gmail.com</ns15:email> </ns15:contactinfo> <ns16:maritalstatus xmlns:ns16="http://www.example.com/xsd/person_01">single</ns16:maritalstatus> <ns17:nationality xmlns:ns17="http://www.example.com/xsd/person_01">pakistani</ns17:nationality> </person> <responsecode>person module success - 00</responsecode> <responsemessage>data fetched successfully</responsemessage> </getpersonresponse>

input xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns="http://www.example.com/xsd/person_01_requestresponse_001" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m0="http://www.example.com/xsd/person_01" xmlns:m="http://www.example.com/cdm/xsd/person_01_requestresponse_001" > <xsl:output method="xml" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <xsl:apply-templates select="//m0:cnic"/> </xsl:template> <xsl:template match="//m0:cnic"> <m:getpersonresponse> <m:personid> <m:nationalidentitynumber> <xsl:value-of select="//m0:cnic"/> </m:nationalidentitynumber> <m:countrycode> <xsl:attribute name="name"><xsl:value-of select="//m0:postal"/></xsl:attribute> <xsl:attribute name="type">xs:decimal</xsl:attribute> </m:countrycode> </m:personid> </m:getpersonresponse> </xsl:template> </xsl:stylesheet>

current xml output:

<?xml version="1.0" encoding="utf-8"?> <m:getpersonresponse xmlns:m="http://www.example.com/cdm/xsd/person_01_requestresponse_001" xmlns="http://www.example.com/xsd/person_01_requestresponse_001" xmlns:m0="http://www.example.com/xsd/person_01" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <m:personid> <m:nationalidentitynumber>35302</m:nationalidentitynumber> <m:countrycode name="54046543245" type="xs:decimal" /> </m:personid> </m:getpersonresponse>

required xml output:

<?xml version="1.0" encoding="utf-8"?> <m:getpersonresponse xmlns:m="http://www.example.com/cdm/xsd/person_01_requestresponse_001" xmlns="http://www.example.com/xsd/person_01_requestresponse_001" xmlns:m0="http://www.example.com/xsd/person_01" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <m:personid> <m:nationalidentitynumber>35302</m:nationalidentitynumber> <m:countrycode name="540465432.45" type="xs:decimal" /> </m:personid> </m:getpersonresponse>

note: there approach in mind can somehow convert our string decimal , split 100. how done?

as said in comments, can purely string manipulation , avoid danger of losing leading/trailing zeros in process of converting string number:

<xsl:variable name="postal" select="//m0:postal" /> <xsl:value-of select="concat(substring($postal, 1, 9), '.', substring($postal, 10, 2))"/>

xml xslt xslt-2.0

No comments:

Post a Comment