问题
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:
- For events that are on the same day, leave the datetimes alone and merely transform those attributes into child elements.
- 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:
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....
来源:https://stackoverflow.com/questions/10689099/expanding-datetime-ranges-in-xslt-1-0