XSL Formatting data

Posted on 2006-05-16
Last Modified: 2008-03-10
How can I format data through an XSL file?  For instance, I have 123456789 in the XML file, but I was XSL to render it as 123-45-6789.  Or monetary formatting - in VB the mask I would have used would be $#,##0.00.
Question by:SkipFire
    LVL 60

    Expert Comment

    by:Geert Bormans
    Hi SkipFire,

    some of what you want to do can be done with the function format-number()

    <xsl:value-of select="format-number(sum(//miles-flown), '##,###')"/>

    you could combine this with
    <xsl:decimal-format> that is a top level argument that allows you to pass it a pattern as an attribute
    and also a name to be used as a named formatter later

    <xsl:decimal-format name="f1" decimal-separator=":" grouping-separator="/"/>
    <xsl:value-of select="format-number(blue div orange, '#.##', 'f2')"/>

    more examples in the XSLT spec

    LVL 4

    Author Comment

    So I assume that for SSN it would just be:
    <xsl:value-of select="format-number(SSN), '###-##-####')"/>

    Is that correct?
    LVL 60

    Accepted Solution

    > Is that correct?

    I don't think it has that level of power
    Maybe you can create a named template for SSN processing,
    like this

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="" version="1.0">
        <xsl:template match="/">
            <xsl:call-template name="formatSSN">
                <xsl:with-param name="myNum" select="//test"/>
        <xsl:template name="formatSSN">
            <xsl:param name="myNum"/>
                <xsl:when test="string-length($myNum) = '9'">
                    <xsl:value-of select="substring($myNum, 1,2)"/>
                    <xsl:value-of select="substring($myNum, 3,4)"/>
                    <xsl:value-of select="substring($myNum, 7,3)"/>
                <xsl:otherwise>Not a valid SSN</xsl:otherwise>

    string formatting is clumsy in XSLT


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Setup a configurable messaging system 3 54
    xml 1 754
    mouse cursor on my laptop keeps jumpin around 12 75
    RSS Feed Enclosure URL 1 46
    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    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. 
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 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

    17 Experts available now in Live!

    Get 1:1 Help Now