0

I am trying to extract the day in my XSLT stylesheet from the datetime format of XML file. I am using XSLT 1.0 and XPATH not Python, that's my limitation. Here is the one example of my code on XML document:

 <forecast-period index="3" start-time-local="2010-08-09T00:00:00+10:00" end-time-local="2010-08-10T00:00:00+10:00" start-time-utc="2010-08-08T14:00:00Z" end-time-utc="2010-08-09T14:00:00Z">
            <element type="forecast_icon_code">12</element>
            <element type="air_temperature_minimum" units="Celsius">8</element>
            <element type="air_temperature_maximum" units="Celsius">15</element>
            <text type="precis">Chance of light rain.</text>
        </forecast-period>

I want to extract the forecast of Monday from the list. I tried to search on google and found day-of-the-week function works for this but couldn't implement properly. I am new to this XSLT and XML. So, I am not getting idea how print the day from the give time.

Can you give me some idea?

nirmalgyanwali
  • 624
  • 1
  • 6
  • 14
  • You don't explain why the `day-of-the-week` function couldn't be implemented. You'll need some kind of calculation to obtain the day of the week based on the timestamps you have in the document. – Pedro Romano Sep 30 '12 at 23:02
  • None of the dates above is a Monday. You'll have to give us more information if you want a better answer. – Mu Mind Sep 30 '12 at 23:22
  • Ya, I have copied only some lines of code only. The whole xml file is too big to paste here. I just want to get idea how to parse timestamp from xml and find the day. I have to make the list of weather information for each days. – nirmalgyanwali Sep 30 '12 at 23:25
  • So you're not using python at all? Was the python tag just a typo? – Mu Mind Sep 30 '12 at 23:31
  • @MuMind Ya, that was a mistake. But I had already removed that tag. – nirmalgyanwali Sep 30 '12 at 23:38
  • @PedroRomano Ya, I have already mentioned in my question that I am new to XML and XSLT. However, i tried to implement that function, got no output. – nirmalgyanwali Sep 30 '12 at 23:42
  • Are you using XSLT **1.0** or **2.0**? – Pedro Romano Sep 30 '12 at 23:59

2 Answers2

0

You don't give a lot of information here, but I usually turn to the lxml package for such things. If you know the path to the data that you want, it should be very simple. I suggest you review the following lxml documentation and edit your question as needed with more specific questions to accomplish your task. I hope this helps. If you post additional info I will try to refine my answer.

David S
  • 12,967
  • 12
  • 55
  • 93
0

You seem to be asking two questions: how to parse out the timestamp from XML, and how to find the day of week from the timestamp. For the first, you should use an XML parser, such as lxml or python's built-in xml.parsers.expat.

Once you have the timestamp string, it's easy to convert to a weekday name using the python-dateutil library:

import dateutil.parser
start_time_local = "2010-08-07T00:00:00+10:00"
weekday_name = dateutil.parser.parse(start_time_local).strftime('%A')
Mu Mind
  • 10,935
  • 4
  • 38
  • 69
  • Good point... I almost get the feeling he wants to go the other direction. Like, I know what day that I need the data for (this Monday), but how to I get it out of the xml file. I could be wrong here. The question is still a bit confusing to me. Maybe he will post more information. – David S Sep 30 '12 at 23:28
  • Thank you for your help. I am only using XSLT to extract the data, that's my limitation. So, couldn't do experiment with your code in python. – nirmalgyanwali Sep 30 '12 at 23:29