Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > convert single digit to two digits

Reply
Thread Tools

convert single digit to two digits

 
 
ofuuzo1@yahoo.no
Guest
Posts: n/a
 
      04-12-2008
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      04-12-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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="myad" as="xs:string">
<xslaram 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="*/myad(.)" 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/
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      04-12-2008
(E-Mail Removed) 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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslutput method="text"/>

<xsl:template name="pad">
<xslaram name="input"/>
<xslaram 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
As a percentage of world inhabitants, the white population willplummet to a single digit (9.76%) by 2060 from a high-water mark of 27.98% in1950 tg DVD Video 3 10-02-2008 09:02 AM
Need help identifying a virus - single digit graphic floats "on-top" of screen mrf Computer Support 2 10-25-2006 01:57 AM
Calculating single-digit summands draq Ruby 10 12-13-2005 05:27 PM
Calculating single-digit summands draq Ruby 3 12-12-2005 03:18 AM



Advertisments