Click here to Skip to main content
15,791,560 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First, thanks for your time for having a look at my question.
I am trying to come up with a code that loops through a template,
finds an element called start_time and call the value of the element and subtract an hour for the time change due to daylight savings. And there are multiple start times and that's why I am going to have to use a loop.

This is the original xml.
There are other elements existing inside PlaylistItem, but I have removed them for
simplicity. The desired output is, for example,

to be

shifted back by an hour for every one of them inside PlaylistItem inside Block.
Your help will be very much appreciated.


I am very new to this XSLT world and I only have a basic piece of logic for this problem. So my logic here is to
1)iterate through the Block
2)access PlaylistItem, access StartTime
3)grab this string
4)use substring and grab the hour characters in the string
5)subtract one from it and then save it
6)make sure this applies to every startTime inside every Playlist in Block.

I'm trying to search for the proper syntax for this and then implement it but
right at the moment I don't have any valid executable code that I can post here.
Please help! And I'll be updating this post as I try to write the code myself.
Thanks again.
Updated 5-Nov-15 11:56am
Patrice T 5-Nov-15 15:37pm    
What have you done ?
Where are you stuck ?
Patrice T 5-Nov-15 16:36pm    
You should check your example.
I think daylight saving is 1 hour change, not 1 minute.
miembro7 5-Nov-15 16:52pm    
I have modified the question thanks for pointing it out!
Patrice T 5-Nov-15 16:54pm    
you welcome
Patrice T 5-Nov-15 17:01pm    
You will find good material there

XSLT is recursive by nature so you don't need a loop.

This solution should work for you:

<xsl:stylesheet version="2.0" xmlns:xsl="">
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="Block">
        <xsl:element name="Block">
            <xsl:apply-templates select="PlaylistItem" />
    <xsl:template match="PlaylistItem">
        <xsl:element name="PlaylistItem">
            <xsl:variable name="hour">
                select="substring(substring-after(StartTime, 'T'), 1, 2)" />
            <xsl:element name="StartTime">
                    <xsl:when test="$hour &lt; 23">
                        select="concat(substring-before(StartTime, 'T'), 'T', $hour+1,
                        substring(substring-after(StartTime, 'T'), 3))" />
                        select="concat(substring-before(StartTime, 'T'), 'T', 00,
                        substring(substring-after(StartTime, 'T'), 3))" />

There are probably more elegant ways of doing it, but that you can find out for yourself.
Share this answer
miembro7 6-Nov-15 14:43pm    
I'm afraid this is causing an error..
miembro7 6-Nov-15 14:57pm    
Can you tell me if this code is causing you an error too?
George Jonsson 6-Nov-15 17:31pm    
What is the error you get???
The code works on the XML data you present in the original question.
If the real data is different it will probably not work without modifications.
Have a look here: Add/Subtract time in XSLT[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900