[Last Call] Learn how to a build a cloud-first strategyRegister Now


XSL Formatting data

Posted on 2006-05-16
Medium Priority
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
  • 2
LVL 60

Expert Comment

by:Geert Bormans
ID: 16689653
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


Author Comment

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

Is that correct?
LVL 60

Accepted Solution

Geert Bormans earned 2000 total points
ID: 16690349
> 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="http://www.w3.org/1999/XSL/Transform" 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

830 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