xml - How to add decimal numbers in xslt 1.0 in substrings -




i have following input xml.

<?xml version = "1.0" encoding = "utf-8"?> <values>     <value>sometext;+123.23;+100.23</value>     <value>sometext;+004.23;+444.12</value> </values> 

i newbie in xsl 1.0. add 2 decimal numbers received in strings receiving nan error. how do that. following output above input xml decimal values summed instead receiving nan errors.

<?xml version="1.0"?> <results>     <result>+223.46</result>     <result>+448.35</result> </results> 

i have xsl file follows,

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match = "/">  <results> <xsl:for-each select="values/value">   <result>      <xsl:value-of select="number(substring(.,10,7)) +     number(substring(.,18,7))" />   </result> </xsl:for-each> </results> </xsl:template> 

a number not allowed contain + sign.

try instead:

<xsl:value-of select="substring(., 11, 6) + substring(., 19, 6)" /> 

(assuming of course positions , lengths constant - , numbers positive).


my own preference do:

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>  <xsl:template match="/values">      <results>         <xsl:for-each select="value">             <xsl:variable name="a" select="substring-before(substring-after(., ';'), ';')" />             <xsl:variable name="b" select="substring-after(substring-after(., ';'), ';')" />             <result>                 <xsl:value-of select="translate($a, '+', '') + translate($b, '+', '')" />             </result>         </xsl:for-each>     </results> </xsl:template>  </xsl:stylesheet> 

which work positive , negative numbers alike, , not depend on length of 1 of 3 tokens in given string.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -