convert single digit to two digits

Discussion in 'XML' started by ofuuzo1@yahoo.no, Apr 12, 2008.

  1. Guest

    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
     
    , Apr 12, 2008
    #1
    1. Advertising

  2. 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, Apr 12, 2008
    #2
    1. Advertising

  3. 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:eek:utput 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/
     
    Martin Honnen, Apr 12, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,801
    darshana
    Oct 26, 2008
  2. draq
    Replies:
    10
    Views:
    188
    Christer Nilsson
    Dec 13, 2005
  3. draq
    Replies:
    3
    Views:
    96
    James Edward Gray II
    Dec 12, 2005
  4. April
    Replies:
    16
    Views:
    201
    Ted Zlatanov
    Jul 7, 2008
  5. Thomas Andersson
    Replies:
    2
    Views:
    110
    Dr.Ruud
    Aug 14, 2010
Loading...

Share This Page