convert single digit to two digits

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ofuuzo1@yahoo.no

    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

    #2
    Re: convert single digit to two digits

    ofuuzo1@yahoo.n o 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="normali ze-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.sourcefor ge.net/>),
    Gestalt (<http://gestalt.sourcef orge.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

    Comment

    • Martin Honnen

      #3
      Re: convert single digit to two digits

      ofuuzo1@yahoo.n o 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:styleshe et
      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="normali ze-space($input)"/>
      <xsl:variable name="padchars" select="'000000 00000000000000' "/>
      <xsl:value-of select="concat( substring($padc hars, 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

      Comment

      • szomiz

        #4
        Re: convert single digit to two digits

        Uzytkownik <ofuuzo1@yahoo. nonapisal w wiadomosci
        news:8cd38f0f-2c9d-47c7-a74e-020bf0029351@m7 3g2000hsh.googl egroups.com...
        >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>
        ....
        substring-after(format-number(day div 100, '0.00'),'.')

        sz.


        Comment

        • szomiz

          #5
          Re: convert single digit to two digits

          Uzytkownik <ofuuzo1@yahoo. nonapisal w wiadomosci
          news:8cd38f0f-2c9d-47c7-a74e-020bf0029351@m7 3g2000hsh.googl egroups.com...
          >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>
          ....
          substring-after(format-number(day div 100, '0.00'),'.')


          <xsl:template match="day/text()[. &lt; 10]">
          <xsl:value-of select="concat( '0',.)"/>

          sz.



          Comment

          Working...