XSLT template

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    #1

    XSLT template

    Hello,

    I'm having problems with an XSLT template I'm developing.

    I have a date in an odd format which I'm trying to convert into something
    more readable. The original date is in the format YYYY-MM-DD, and I'd like
    to convert it to DD MON YY.

    I pass the date into my template thusly in my XSLT stylesheet (date-updated
    is the data I'm passing in):

    <xsl:call-template name="formatDat e">
    <xsl:with-param name="string" select="date-updated" />
    </xsl:call-template>

    .... and the template is thus:

    <xsl:template name="formatDat e">
    <xsl:param name="inDate" />
    <!-- Day -->
    <xsl:variable name="day" select="number( substring($inDa te, 9, 2))" />
    <!-- Month -->
    <xsl:variable name="month" select="number( substring($inDa te, 6, 2))" />
    <xsl:variable name="converted-month">
    <xsl:choose>
    <xsl:when test="$month=fo rmat-number('01', '00')">
    <xsl:value-of select="Jan" />
    </xsl:when>
    ....
    <xsl:when test="$month='1 2'">
    <xsl:value-of select="Dec" />
    </xsl:when>
    </xsl:choose>
    </xsl:variable>
    <!-- Year -->
    <xsl:variable name="year" select="number( substring($inDa te, 3, 2))" />
    <!-- Formatted Date -->
    <xsl:value-of select="concat( $inDate, ': ', format-number($day, '00'), '
    ', $converted-month, ' ', format-number($year, '00'))" />
    </xsl:template>

    The output I get is:

    : NaN NaN

    NaN = Not a number, of course, but I can't see why such an error should be
    raised.

    Thanks for any assistance,

    Mark


  • Martin Honnen

    #2
    Re: XSLT template



    Mark wrote:

    [color=blue]
    > <xsl:call-template name="formatDat e">
    > <xsl:with-param name="string" select="date-updated" />
    > </xsl:call-template>
    >
    > ... and the template is thus:
    >
    > <xsl:template name="formatDat e">
    > <xsl:param name="inDate" />[/color]

    I think you need
    <xsl:param name="string" />
    here or
    <xsl:with-param name="inDate" select="date-updated" />
    above, the name of the parameter needs to be the same in xsl:with-param
    and xsl:param.


    --

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

    Comment

    • Mark

      #3
      Re: XSLT template

      > I think you need[color=blue]
      > <xsl:param name="string" />
      > here or
      > <xsl:with-param name="inDate" select="date-updated" />
      > above, the name of the parameter needs to be the same in xsl:with-param
      > and xsl:param.[/color]

      Yes, you're right - thanks.

      Mark


      Comment

      • Mark

        #4
        Re: XSLT template

        > Yes, you're right - thanks.

        Another problem - the month comes in as a 2 digit number (e.g. 01 for Jan).

        How can I convert it to the text "Jan"?

        I've tried the following:

        <xsl:variable name="month" select="number( substring($inDa te, 6, 2))"/>
        <xsl:variable name="converted-month">
        <xsl:choose>
        <xsl:when test="$month='1 '">
        <xsl:value-of select="Jan"/>
        </xsl:when>
        ....
        </xsl:choose>
        </xsl:variable>

        .... but the match is never made: it always produces no output.

        I've tried outputting $month, and that produces the correct month number
        (e.g. "1", rather than "01"), so the correct data is going in. I just can't
        seem to get the comparison right.

        Thanks for any information,

        Mark


        Comment

        • A. Bolmarcich

          #5
          Re: XSLT template

          In article <40f3e4b9$0$131 62$afc38c87@new s.easynet.co.uk >, Mark wrote:[color=blue]
          > Another problem - the month comes in as a 2 digit number (e.g. 01 for Jan).
          >
          > How can I convert it to the text "Jan"?
          >
          > I've tried the following:
          >
          > <xsl:variable name="month" select="number( substring($inDa te, 6, 2))"/>
          > <xsl:variable name="converted-month">
          > <xsl:choose>
          > <xsl:when test="$month='1 '">
          > <xsl:value-of select="Jan"/>
          > </xsl:when>
          > ...
          > </xsl:choose>
          > </xsl:variable>
          >
          > ... but the match is never made: it always produces no output.[/color]

          The match is made, but <xsl:value-of select="Jan"/> is the value of the
          element named "Jan". Because there is no element named "Jan", the value
          is empty. If you want the value to be a string use

          <xsl:value-of select="'Jan'"/>

          If you want the value to be a text node use

          <xsl:text>Jan </xsl:text>

          Comment

          • Mark

            #6
            Re: XSLT template

            > The match is made, but <xsl:value-of select="Jan"/> is the value of the[color=blue]
            > element named "Jan". Because there is no element named "Jan", the value
            > is empty. If you want the value to be a string use
            >
            > <xsl:value-of select="'Jan'"/>
            >
            > If you want the value to be a text node use
            >
            > <xsl:text>Jan </xsl:text>[/color]

            Great - thank you very much.

            Mark


            Comment

            • Deirdre Saoirse Moen

              #7
              Re: XSLT template

              Mark <mark.harris.no spam@ukonline.c o.uk.nospam> wrote:[color=blue]
              > I have a date in an odd format which I'm trying to convert into
              > something more readable. The original date is in the format
              > YYYY-MM-DD, and I'd like to convert it to DD MON YY.[/color]

              As an aside, what's so "odd" about the ISO (8601) date format?

              http://www.hermetic.ch/cal_stud/formats.htm

              --
              _Deirdre web: http://deirdre.net blog: http://deirdre.org/blog/
              "Memes are a hoax! Pass it on!"

              Comment

              Working...