Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   convert single digit to two digits (http://www.velocityreviews.com/forums/t605226-convert-single-digit-to-two-digits.html)

ofuuzo1@yahoo.no 04-12-2008 11:46 AM

convert single digit to two digits
 
I have the following xml file and I want to concat the values and if
a value is less that two digits, it is converted to two digits

<date>
<day>1 </day>
<month>11</month>
<year>2008</year>
<date>
.....

The result will be 01112008

How can I do it using xslt?

Thanks
Ofuuzo


Martin Honnen 04-12-2008 12:48 PM

Re: convert single digit to two digits
 
ofuuzo1@yahoo.no wrote:
> I have the following xml file and I want to concat the values and if
> a value is less that two digits, it is converted to two digits
>
> <date>
> <day>1 </day>

^
There is a space there, does that belong there?

> <month>11</month>
> <year>2008</year>
> <date>
> ....
>
> The result will be 01112008
>
> How can I do it using xslt?


Well with XSLT 2.0 you can write a function e.g.

<xsl:function name="my:pad" as="xs:string">
<xsl:param name="input" as="xs:string"/>
<xsl:variable name="n" as="xs:string"
select="normalize-space($input)"/>
<xsl:sequence
select="if (string-length($n) &lt; 2) then
concat('0', $n) else $n"/>
</xsl:function>

and use it like this:

<xsl:template match="date">
<xsl:value-of select="*/my:pad(.)" separator=""/>
</xsl:template>

XSLT 2.0 is supported by Saxon (<http://saxon.sourceforge.net/>),
Gestalt (<http://gestalt.sourceforge.net>) and AltovaXML
(<http://www.altova.com/altovaxml.html>)

Let us know whether that helps or whether you want an XSLT 1.0 solution.


--

Martin Honnen
http://JavaScript.FAQTs.com/

Martin Honnen 04-12-2008 01:02 PM

Re: convert single digit to two digits
 
ofuuzo1@yahoo.no wrote:
> I have the following xml file and I want to concat the values and if
> a value is less that two digits, it is converted to two digits
>
> <date>
> <day>1 </day>
> <month>11</month>
> <year>2008</year>
> <date>
> ....
>
> The result will be 01112008
>
> How can I do it using xslt?


Here is an XSLT 1.0 solution with a named template:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="text"/>

<xsl:template name="pad">
<xsl:param name="input"/>
<xsl:param name="length" select="2"/>
<xsl:variable name="n" select="normalize-space($input)"/>
<xsl:variable name="padchars" select="'00000000000000000000'"/>
<xsl:value-of select="concat(substring($padchars, 1, $length -
string-length($n)), $n)"/>
</xsl:template>

<xsl:template match="date">
<xsl:for-each select="*">
<xsl:call-template name="pad">
<xsl:with-param name="input" select="."/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


--

Martin Honnen
http://JavaScript.FAQTs.com/


All times are GMT. The time now is 01:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.