Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:03:56+00:00 2026-06-04T05:03:56+00:00

I have an XML feed of events whose dates I would like to interact

  • 0

I have an XML feed of events whose dates I would like to interact with.

Source XML:

<?xml version="1.0" encoding="UTF-8"?>
<events>
  <event>
    <!-- various elements -->
    <start_datetime value="2012-02-09 10:00:00"/>
    <end_datetime value="2012-02-09 11:00:00"/>
    <!-- various elements -->
  </event>
  <event>
    <!-- various elements -->
    <start_datetime value="2012-02-09 10:00:00"/>
    <end_datetime value="2012-02-10 15:00:00"/>
    <!-- various elements -->
  </event>
  <!-- other events -->
</events>

Notice that /events/event[1] is an event that starts and ends on the same day; /events/event[2], on the other hand, spans two days. Here’s what I would like to accomplish:

  1. For events that are on the same day, leave the datetimes alone and merely transform those attributes into child elements.
  2. For events that span multiple days, I want to create multiple <event> elements that (a) match the overall span of time and (b) where appropriate, span a full day’s worth of time.

So, my ideal XSLT would produce:

Desired XML:

<?xml version="1.0" encoding="UTF-8"?>
<events>
  <event>
    <!-- various elements -->
    <start_datetime>2012-02-09 10:00:00</start_datetime>
    <end_datetime>2012-02-09 11:00:00</end_datetime>
    <!-- various elements -->
  </event>
  <event>
    <!-- various elements -->
    <start_datetime>2012-02-09 10:00:00</start_datetime>
    <end_datetime>2012-02-09 23:59:59</end_datetime>
    <!-- various elements -->
  </event>
  <event>
    <!-- various elements -->
    <start_datetime>2012-02-10 00:00:00</start_datetime>
    <end_datetime>2012-02-10 15:00:00</end_datetime>
    <!-- various elements -->
  </event>
  <!-- other events -->
</events>

Notice how my rules are met:

  • Because /events/event[1] occurs over the same day, we leave it alone (other than the trivial task of changing attribute values into child elements).
  • /events/event[2] spans two days, which means it needs two <event> blocks (one from the starting datetime to 11:59:59pm on that date and one from 00:00:00 on the ending date to the ending datetime).

Final Considerations:

  • This needs to be accomplished in XSLT 1.0.

  • I am not opposed to using EXSLT’s date functions – however, if they can be avoided, that would be preferable.

Clear as mud? As always, thanks for your help. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:03:58+00:00Added an answer on June 4, 2026 at 5:03 am

    Here is an XSLT 1.0 solution, that doesn’t use EXSLT date functions, but which makes use of a named template to add one to a given date, using string manipulation functions to extra the year, month and day from a date string

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:template match="@*|node()">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
       </xsl:template>
    
       <xsl:template match="event" name="event">
          <xsl:param name="start_datetime" select="start_datetime/@value"/>
          <xsl:variable name="end_datetime" select="end_datetime/@value"/>
          <event>
             <xsl:apply-templates select="start_datetime/preceding-sibling::node()"/>
             <start_datetime>
                <xsl:value-of select="$start_datetime"/>
             </start_datetime>
             <end_datetime>
                <xsl:choose>
                   <xsl:when test="substring($start_datetime, 1, 10) = substring($end_datetime ,1, 10)">
                      <xsl:value-of select="$end_datetime"/>
                   </xsl:when>
                   <xsl:otherwise>
                      <xsl:value-of select="concat(substring($start_datetime, 1, 10), ' 23:59:59')"/>
                   </xsl:otherwise>
                </xsl:choose>
             </end_datetime>
             <xsl:apply-templates select="end_datetime/following-sibling::node()"/>
          </event>
          <xsl:if test="substring($start_datetime, 1, 10) != substring($end_datetime ,1, 10)">
             <xsl:call-template name="event">
                <xsl:with-param name="start_datetime">
                   <xsl:call-template name="addOneToDate">
                      <xsl:with-param name="date" select="$start_datetime"/>
                   </xsl:call-template>
                </xsl:with-param>
             </xsl:call-template>
          </xsl:if>
       </xsl:template>
    
       <xsl:template name="addOneToDate">
          <xsl:param name="date"/>
          <xsl:variable name="year" select="number(substring($date, 1, 4))"/>
          <xsl:variable name="month" select="number(substring($date, 6, 2))"/>
          <xsl:variable name="day" select="number(substring($date, 9, 2))"/>
          <xsl:variable name="daysInMonth">
             <xsl:choose>
                <xsl:when test="$month = 2">
                   <xsl:choose>
                      <xsl:when test="($year div 4 = 0 and $year div 100 != 0) or ($year div 400 = 0)">29</xsl:when>
                      <xsl:otherwise>28</xsl:otherwise>
                   </xsl:choose>
                </xsl:when>
                <xsl:when test="$month = 4 or $month = 6 or $month = 9 or $month = 11">30</xsl:when>
                <xsl:otherwise>31</xsl:otherwise>
             </xsl:choose>
          </xsl:variable>
          <xsl:choose>
             <xsl:when test="$day != $daysInMonth">
                <xsl:value-of select="concat($year, '-', format-number($month, '00'), '-', format-number($day + 1, '00'), ' 00:00:00')"/>
             </xsl:when>
             <xsl:when test="$month = 12">
                <xsl:value-of select="concat($year + 1, '-01-01 00:00:00')"/>
             </xsl:when>
             <xsl:otherwise>
                <xsl:value-of select="concat($year, '-', format-number($month + 1, '00'), '-01 00:00:00')"/>
             </xsl:otherwise>
          </xsl:choose>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your sample XML, the following is output

    <events>
       <event>
          <!-- various elements -->
          <start_datetime>2012-02-09 10:00:00</start_datetime>
          <end_datetime>2012-02-09 11:00:00</end_datetime>
          <!-- various elements -->
       </event>
       <event>
          <!-- various elements -->
          <start_datetime>2012-02-09 10:00:00</start_datetime>
          <end_datetime>2012-02-09 23:59:59</end_datetime>
          <!-- various elements -->
       </event>
       <event>
          <!-- various elements -->
          <start_datetime>2012-02-10 00:00:00</start_datetime>
          <end_datetime>2012-02-10 15:00:00</end_datetime>
          <!-- various elements -->
       </event>
       <!-- other events -->
    </events>
    

    I am sure you would agree using EXSLT would be simpler….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have XML shaped like the following: <?xml version=1.0 encoding=UTF-8?> <feed xmlns=http://www.w3.org/2005/Atom xmlns:openSearch=http://a9.com/-/spec/opensearch/1.1/ xmlns:docs=http://schemas.google.com/docs/2007
I have an XML feed that looks like this: <?xml version=1.0 encoding=UTF-8?> <productFeed version=1.0
I have an XML feed coming in: <?xml version=1.0 encoding=UTF-8?><product> <name>John</name> <contact_email>john@johnson.com</contact_email> <contact_telephone>01234 567</contact_telephone>
I've got XML feed from public google calendar. Looks like this: <?xml version='1.0' encoding='UTF-8'?>
I have a XML file with code like <?xml version=1.0 encoding=UTF-8?> <?xml-stylesheet href=rss2.xsl type=text/xsl
I have this XML : <?xml version=1.0 encoding=utf-8?> <rss version=2.0 xmlns:atom=http://www.w3.org/2005/Atom> <channel> <atom:link href=http://www.montagnaconamore.it/rss/news.xml
I am really struggling on this. I have an XML feed: <?xml version=1.0 encoding=ISO-8859-1?>
I have an XML feed that looks something like this: I can parse the
I have my XML feed being created from an associative array. Using new DOMDocument('1.0','UTF-8');
I have a feed.xml file that looks something like this. What I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.