5 Replies Latest reply: Jun 20, 2012 9:47 AM by Jason_(A_Non) RSS

    Date formatting in xpath

    944542
      Hi,

      I have a requirement to display the nearest date in the xml using xpath. So i used the following function to display the nearest date and it works,

      min(/virtualMachine/diskVolume/capacityDepletionDate/xs:date(xs:dateTime(.)))

      Now the prob is, I need to display the date in specific format. I need in the format of Aug 20, 2012.

      But the above function gives me result as 2012-08-20+05:30. So could someone help me to format this output in the xml?

      Thanks,
      Anand.
        • 1. Re: Date formatting in xpath
          odie_63
          Hi,

          Which XQuery processor are you using?
          Maybe it already implements the fn:format-date() function?

          If not, you have to use a custom function :
          declare function local:format-date($dt as xs:date) as xs:string
          {
           let $months := ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
           return concat( $months[fn:month-from-date($dt)]
                        , " "
                        , fn:day-from-date($dt)
                        , ", "
                        , fn:year-from-date($dt) )
          };
          
          let $dt := xs:date("2012-08-20+05:30")
          return local:format-date($dt)
          fn:month-from-date and alike have their counterparts working with dateTime datatype (fn:month-from-dateTime etc.), so ultimately, you can also pass directly a dateTime to the function without having to first truncate it.
          • 2. Re: Date formatting in xpath
            944542
            Hi,

            Thanks for your reply.
            I am not sure about which Xquery processor we are using. Actually I need to format the date in my xml itself. The following is the piece of my xml,

            <extraColumns>
            <alias>min(/virtualMachine/diskVolume/depletionDate/xs:date(xs:dateTime(.)))</alias>
            <columnHeading>Date</columnHeading>
            <unit>none</unit>
            </extraColumns>

            So in the <alias> tag, if i want to display the date in the specific format (Aug 20, 2012) what changes I need to make in the <alias> tag?

            Thanks,
            Anand.
            • 3. Re: Date formatting in xpath
              odie_63
              I am not sure about which Xquery processor we are using.
              Let's make this simpler : which product (and version) are you working on?
              Actually I need to format the date in my xml itself. The following is the piece of my xml,

              <extraColumns>
              <alias>min(/virtualMachine/diskVolume/depletionDate/xs:date(xs:dateTime(.)))</alias>
              <columnHeading>Date</columnHeading>
              <unit>none</unit>
              </extraColumns>
              Are you sure you're using XQuery?
              If so, as explained, declare a function at the top of the XQuery expression and use it in the main body.
              • 4. Re: Date formatting in xpath
                944542
                Hi Odie,

                I am working on Eclipse Helios service release 2.

                I am not using Xquery, so I cant declare any function as you mentioned. I need to change the format in the xml itself. So if there is any function to format the date in the xml itself, pls let me know.
                Already I used the min() function to get the nearest date. Now i need to format the date in the xml itself. I should not touch any other code.

                Thanks,
                Anand.
                • 5. Re: Date formatting in xpath
                  Jason_(A_Non)
                  So if you are not using XQuery, then why did you post in the XQuery forum?

                  Regardless, what tools are you using? Are you doing this only in XSLT and if so are you using XPath 1.0 or 2.0? We need to know what tools you are using to accomplish your goal so that you can get an appropriate answer.