当前位置: 代码迷 >> XML/SOAP >> 如何用xsl把xml里面的内容按日期归类呢
  详细解决方案

如何用xsl把xml里面的内容按日期归类呢

热度:318   发布时间:2012-03-14 12:01:12.0
怎么用xsl把xml里面的内容按日期归类呢?
xml中是这样的:
<file year="2002" month="6" day="25">文件A</file>
<file year="2008" month="2" day="2">文件B</file>
<file year="2002" month="6" day="1">文件C</file>
<file year="2008" month="11" day="11">文件D</file>
<file year="2008" month="2" day="25">文件E</file>

我想把同年同月的合并放在一起:
2002年6月:
 1日 "文件A"
 25日 "文件C"

2008年2月:
 2日 "文件B"
 25日 "文件E"

2008年11月:
 11日 "文件D"

不知道xsl该怎么写?
另外,我的日期应该拆开year\month\day属性,还是直接date="2002-12-03"这样好?

------解决方案--------------------
XML code
    <xsl:template match="root">
        <xsl:copy>
            <xsl:for-each-group select="file" group-by="concat(@year, '/', @month)">
                <files groupname="{current-grouping-key()}">
                    <xsl:value-of select="current-group()"/>
                </files>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

------解决方案--------------------
file.xml:
XML code
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <file year="2002" month="6" day="25">文件A</file>
    <file year="2008" month="2" day="2">文件B</file>
    <file year="2002" month="6" day="1">文件C</file>
    <file year="2008" month="11" day="11">文件D</file>
    <file year="2008" month="2" day="25">文件E</file>
</root> 
  相关解决方案