Solved

xslt test decimal length

Posted on 2013-05-26
2
391 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

708 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

10 Experts available now in Live!

Get 1:1 Help Now