Solved

xslt test decimal length

Posted on 2013-05-26
2
395 Views
Last Modified: 2013-05-28
Hi experts,

I would like to test a numeric value coming from an XML node.

I have this field in a xsl form

<input name="vpreven" type="text" id="vpreven" size="15" maxlength="{//vpreven
/@length}" style="text-align:right" > 
 <xsl:if test="(//vpreven!=0)">        
 <xsl:attribute name="value">
    <xsl:value-of select="format-number(//vpreven,'#,###,###,###,###.000000')"/>   </xsl:attribute>
  </xsl:if>
  </input>

I get as result 6 decimal numbers e.g. 10.234567
 
I would like to test the decimal length in order to put the right format
e.g
10.543 -> <xsl:value-of select="format-number(//vpreven,'#,###,###,###,###.000')"/>
10.2 -> <xsl:value-of select="format-number(//vpreven,'#,###,###,###,###.0')"/>
10.87655 -> <xsl:value-of select="format-number(//vpreven,'#,###,###,###,###.00000')"/>

Thank you for you comments,

Regards
0
Comment
Question by:alcani
2 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 39198539
you can use string-length(substring-after(string(//vpreven), '.')) in the test

but I think
format-number(//vpreven,'#,###,###,###,###.#')
will do exactly what you want, without testing
0
 

Author Comment

by:alcani
ID: 39202987
Thanks! I do it like this,

<xsl:choose>
        <xsl:when test="(string-length(substring-after(string(//vpreven), '.')) = 3 )">
        <xsl:attribute name="value">
        <xsl:value-of select="format-number(//vpreven,'#,###,###,###,###.000')"/>
        </xsl:attribute>
        </xsl:when>
        <xsl:when test="(string-length(substring-after(string(//vpreven), '.')) = 4 )">
        <xsl:attribute name="value">
        <xsl:value-of select="format-number(//vpreven,'#,###,###,###,###.0000')"/>
        </xsl:attribute>
        </xsl:when>
        <xsl:when test="(string-length(substring-after(string(//vpreven), '.')) = 5 )">
        <xsl:attribute name="value">
        <xsl:value-of select="format-number(//vpreven,'#,###,###,###,###.00000')"/>
        </xsl:attribute>
        </xsl:when>
        <xsl:when test="(string-length(substring-after(string(//vpreven), '.')) = 6 )">
        <xsl:attribute name="value">
        <xsl:value-of select="format-number(//vpreven,'#,###,###,###,###.000000')"/>  
        </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
        <xsl:attribute name="value">
        <xsl:value-of select="format-number(//vpreven,'#,###,###,###,###.00')"/>
        </xsl:attribute>
        </xsl:otherwise>
 </xsl:choose>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XML XSLT If loop 4 21
Enter key on html form page 11 48
Materializecss - Different Behaviors in Browser's 1 20
Bad <form> statement? 9 26
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now