Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
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,

2015-10-12T11:07:39.924
to be
2015-10-12T12:07:39.924

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

<Block>
<PlaylistItem>
    <StartTime>2015-10-12T11:59:42.201</StartTime>
</PlaylistItem>
<PlaylistItem>
    <StartTime>2015-10-12T11:07:39.924</StartTime>
</PlaylistItem>
<PlaylistItem>
    <StartTime>2015-10-12T20:29:42.211</StartTime>
</PlaylistItem>
<PlaylistItem>
    <StartTime>2015-10-12T09:39:58.901</StartTime>
</PlaylistItem>
<PlaylistItem>
    <StartTime>2015-10-12T12:04:50.551</StartTime>
</PlaylistItem>
</Block>


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.
Posted
Updated 5-Nov-15 10:56am
v4
Comments
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 http://www.w3schools.com/xml/default.asp

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

This solution should work for you:

HTML
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    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:element>
    </xsl:template>
    
    <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:choose>
                    <xsl:when test="$hour &lt; 23">
                        <xsl:value-of
                        select="concat(substring-before(StartTime, 'T'), 'T', $hour+1,
                        substring(substring-after(StartTime, 'T'), 3))" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of
                        select="concat(substring-before(StartTime, 'T'), 'T', 00,
                        substring(substring-after(StartTime, 'T'), 3))" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:element>
    </xsl:template>
</xsl:stylesheet>


There are probably more elegant ways of doing it, but that you can find out for yourself.
 
Share this answer
 
v3
Comments
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