Search

I’m getting XML from a third-party that looks something like this:

<data>
    <itemList>
        <item>
            <startTime>2010-01-12T06:05:52-06:00</startTime>
            <value>3.2187</value>
        </item>
        <item>
            <startTime>2010-01-14T07:01:32-06:00</startTime>
            <value>3.3474</value>
        </item>
        <item>
            <startTime>2010-01-17T21:45:30-06:00</startTime>
            <value>3.4601</value>
        </item>
    </itemList>
</data>

I would like to output the values as a JavaScript array like this, filling in the absent date values with a “0”:

[ { "value": 3.2187 }, { "value": 0 }, { "value": 3.3474 }, { "value": 0 }, { "value": 0 }, { "value": 3.4601 } ]

Is this feasible in XSLT? Or should I be looking for a different approach altogether?

I don’t understand how you get from 3 sets of values in the XML to 6 in the JavaScript. How do you define an absent value?

EDIT: got it, it’s based on dates to the “0” are missing days?

I think you’d want to find the difference between the dates in the first and last item elements to establish a number of days. You then loop for this number (in your example six times) adding a day to the date each time, cross reference back to your XML to see if you have a value for the date.

You’ll need to use the EXSLT date:difference() and date:add() functions:

http://www.exslt.org/date/functions/difference/index.html

Thanks for the pointers, Nick. I’ll get started and post the results here.

I had the same requirement some time ago and created a custom datasource that just prints out dates of a given time range to iterate over. The XML is grouped by year, month and number of week, spiced with some additional information like the day of year. I can clean up the datasource a bit an post it here if you want to go this way.

Thanks Jonas, I would be very interested in seeing that, if you don’t mind spending the time to clean it up.

Brian, here is a link to the custom datasource. I removed the grouping by week and put the number of the week as a parameter of the day element. You can use start- and end-parameters, they run through strtotime(), but I didn’t do any error checking there.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details