tags:

views:

163

answers:

1

I could use some help creating an XSL template that will take a string of numbers (i.e., 123456789) and format that string into a Social Security Number format (i.e., 123-45-6789). I found one example on the Internet, but it seemed overcomplicated.

I'm new to XSLT, so please keep that in mind when replying. Thank you!

+2  A: 

XSLT 1.0's string functions are a bit limited, but fortunately this isn't too hard:

Assuming < ssn >123456789< /ssn>:

<xsl:template match="ssn">    
    <xsl:value-of select="substring(., 0, 4)"/>
    <xsl:text>-</xsl:text>
    <xsl:value-of select="substring(., 4, 2)"/>
    <xsl:text>-</xsl:text>
    <xsl:value-of select="substring(., 6, 4)"/>    
</xsl:template>

In XSLT 2.0, concat() can take more than two arguments, so it's a single line:

  <xsl:template match="ssn">
        <xsl:value-of select="concat(substring(., 0, 4), '-', substring(., 4, 2), '-', substring(., 6, 4))" />    
   </xsl:template>
James Sulak
In XSLT 1.0 you could also nest concat()s: <xsl:template match="ssn"> <xsl:value-of select="concat(substring(., 0, 4), concat('-', concat(substring(., 4, 2), concat('-', substring(., 6,4)))))"/> </xsl:template>But your option is much more readable :)
ChuckB