我的任务是尝试合并两个不同的"组"进入1,然后按日期排序。 Muenchian分组是我认为是进行初始分组的最佳方式,但后来我需要将它们组合起来,并按日期排序,这样我就可以遍历集合结果并输出它们。我受限于XSLT 1.0。以下是一些示例代码:
<?xml version="1.0" encoding = "UTF-8"?>
<events>
<event>
<name>Event1</name>
<history>
<pastevent>
<date>01/01/1999</date>
</pastEvent>
<pastevent>
<date>01/01/2000</date>
</pastEvent>
<pastevent>
<date>01/01/2001</date>
</pastEvent>
<pastevent>
<date>01/01/2002</date>
</pastEvent>
</history>
<currentEvents>
<date>01/01/2019</date>
<date>01/01/2019</date>
<date>01/01/2019</date>
<date>01/01/2019</date>
<date>01/01/2019</date>
<date>01/01/2019</date>
</currentEvents>
</event>
<event>
<name>Event2</name>
<history>
<pastevent>
<date>02/01/1999</date>
</pastEvent>
<pastevent>
<date>02/01/2000</date>
</pastEvent>
<pastevent>
<date>02/01/2001</date>
</pastEvent>
<pastevent>
<date>02/01/2002</date>
</pastEvent>
</history>
<currentEvents>
<date>02/01/2019</date>
<date>02/01/2019</date>
<date>02/01/2019</date>
<date>02/01/2019</date>
<date>02/01/2019</date>
<date>02/01/2019</date>
</currentEvents>
</event>
</events>
我知道我可以为过去的事件和当前日期创建组,但是如何将两者组合成一个组以获得类似于此的输出:
Events
Date Event
01/01/1999 Event1
02/01/1999 Event2
01/01/2000 Event1
02/01/2000 Event2
正如我所说的,我可以开始创建2个独立的Muenchian小组来开展工作。 1个当前事件组和1个过去事件组,但我如何将它们变成1个单一组?
谢谢!
答案 0 :(得分:1)
除了过去的事件日期和当前事件日期的两个单独的组,只需将所有日期分组,无论它们出现在何处。
您还可以重新排列日期值以进行排序。请注意,在我的示例中,我假设您的日期格式为MM/DD/YYYY
。如果我假设不正确,您可能需要更改排序选择。
示例...
XML输入(将<pastevent>
更改为<pastEvent>
以使其格式正确)
<events>
<event>
<name>Event1</name>
<history>
<pastEvent>
<date>01/01/1999</date>
</pastEvent>
<pastEvent>
<date>01/01/2000</date>
</pastEvent>
<pastEvent>
<date>01/01/2001</date>
</pastEvent>
<pastEvent>
<date>01/01/2002</date>
</pastEvent>
</history>
<currentEvents>
<date>01/01/2019</date>
<date>01/01/2019</date>
<date>01/01/2019</date>
<date>01/01/2019</date>
<date>01/01/2019</date>
<date>01/01/2019</date>
</currentEvents>
</event>
<event>
<name>Event2</name>
<history>
<pastEvent>
<date>02/01/1999</date>
</pastEvent>
<pastEvent>
<date>02/01/2000</date>
</pastEvent>
<pastEvent>
<date>02/01/2001</date>
</pastEvent>
<pastEvent>
<date>02/01/2002</date>
</pastEvent>
</history>
<currentEvents>
<date>02/01/2019</date>
<date>02/01/2019</date>
<date>02/01/2019</date>
<date>02/01/2019</date>
<date>02/01/2019</date>
<date>02/01/2019</date>
</currentEvents>
</event>
</events>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:key name="dates" match="date" use="."/>
<xsl:template match="/events">
<xsl:text>Events
</xsl:text>
<xsl:text>Date			Event
</xsl:text>
<xsl:for-each select=".//date[count(.|key('dates',.)[1])=1]">
<xsl:sort
select="concat(substring(.,7,4),substring(.,1,2),substring(.,4,2))"
data-type="number"
order="ascending"/>
<xsl:value-of select="concat(.,'		',ancestor::event/name,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
Events
Date Event
01/01/1999 Event1
02/01/1999 Event2
01/01/2000 Event1
02/01/2000 Event2
01/01/2001 Event1
02/01/2001 Event2
01/01/2002 Event1
02/01/2002 Event2
01/01/2019 Event1
02/01/2019 Event2