English 中文(简体)
How to group items by date range in XSLT?
原标题:

I have a bunch of data that looks a little like this:

<item>
  <colour>Red</colour>
  <date_created>2009-10-10 12:01:55</date_created>
  <date_sold>2009-10-20 22:32:12</date_sold>
</item>
<item>
  <colour>Blue</colour>
  <date_created>2009-11-01 13:21:00</date_created>
  <date_sold>2009-11-21 12:32:12</date_sold>
</item>
<item>
  <colour>Blue</colour>
  <date_created>2009-10-29 21:23:02</date_created>
  <date_sold>2009-10-20 02:02:22</date_sold>
</item>
<item>
  <colour>Red</colour>
  <date_created>2009-11-02 09:11:51</date_created>
  <date_sold>2009-11-20 09:15:53</date_sold>
</item>
<item>
  <colour>Red</colour>
  <date_created>2009-10-18 11:00:55</date_created>
  <date_sold>2009-10-20 11:12:22</date_sold>
</item>

Now what I would like to be able to do is to run that through an XSLT stylesheet such that I get ouput looking like this:

Colour | In stock 1 week | In stock 2 weeks | In stock 3 weeks
Red    |       1         |        3         |       2
Blue   |       0         |        2         |       1

Currently I have a stylesheet that uses basic muenchian grouping to show that 30% of stock was Red and 70% blue, but I can t see a way to find the number of nodes withing a given date range.

Is there a way to use keys to select a range? Do I need to create some kind of intermediate data node? Is there a different route that shows I m barking up the wrong tree with both those suggestions? Is this even possible with XSLT or do I need to find a way to change the data source?

问题回答

If you can use EXSLT date and time functions, you could use something like the following to get items by time in stock (in weeks, rounded up):

<xsl:key 
  name="items-by-weeks-in-stock" 
  match="//item"
  use="ceiling(date:seconds(date:difference(translate(date_created,    ,  T ),
                                            translate(date_sold,    ,  T )))
               div 604800)"/>




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签