XSLT - 按日期分组元素

时间:2011-04-26 14:10:55

标签: xslt xslt-1.0

我想要做的是显示XML文件中所有博客帖子的列表,其中包含所有博客帖子标题及其发布日期的列表。我正在寻找的输出类型是:

HTML输出:

<ul>
  <h3>February 2011</h3>
  <li>Blog Title - 09/02/2011</li>
  <li>1 More Blog Title - 19/02/2011</li>
  <h3>March 2011</h3>
  <li>More Blogging - 15/03/2011</li>
  <h3>April 2011</h3>
  <li>Another Title - 29/04/2011</li> 
</ul>

来自以下XMLL

XML:

<BlogPosts>
  <Post>
    <Title>Blog Title</Title>
    <CreatedBy>A Another</CreatedBy>
    <PublishedDate>09/02/2011</PublishedDate>
    <Url>Http://url.com</Url>
    <BlogBody>Some text here...<BlogBody>
  </Post>
  <Post>
    <Title>1 More Blog Title</Title>
    <CreatedBy>A Another</CreatedBy>
    <PublishedDate>19/02/2011</PublishedDate>
    <Url>Http://url.com</Url>
    <BlogBody>Some text here...<BlogBody>
  </Post>
  <Post>
    <Title>More Blogging</Title>
    <CreatedBy>A Another</CreatedBy>
    <PublishedDate>15/03/2011</PublishedDate>
    <Url>Http://url.com</Url>
    <BlogBody>Some text here...<BlogBody>
  </Post>
  <Post>
    <Title>Another Title</Title>
    <CreatedBy>A Another</CreatedBy>
    <PublishedDate>29/04/2011</PublishedDate>
    <Url>Http://url.com</Url>
    <BlogBody>Some text here...<BlogBody>
  </Post>
</BlogPosts>

有没有办法可以按日期对BlogPosts进行分组,以及使用XSLT输出它们的月份名称?

1 个答案:

答案 0 :(得分:1)

此样式表生成所需的输出:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:ns="my:ns">
    <xsl:key name="byMonthYear" match="BlogPosts/Post"
        use="substring-after(PublishedDate, '/')" />
    <ns:months>
        <m id="01">January</m>
        <m id="02">February</m>
        <m id="03">March</m>
        <m id="04">April</m>
        <m id="05">May</m>
        <m id="06">June</m>
        <m id="07">July</m>
        <m id="08">August</m>
        <m id="09">September</m>
        <m id="10">October</m>
        <m id="11">November</m>
        <m id="12">December</m>
    </ns:months>
    <xsl:variable name="months" select="document('')/*/ns:months/*" />
    <xsl:template match="/">
        <ul><xsl:apply-templates /></ul>
    </xsl:template>
    <xsl:template match="BlogPosts/Post" />
    <xsl:template
        match="BlogPosts/Post[generate-id()=generate-id(key('byMonthYear', 
                    substring-after(PublishedDate, '/'))[1])]">
        <xsl:variable name="year"
            select="substring-after(
                        substring-after(PublishedDate, '/'), '/')" />
        <xsl:variable name="month"
            select="substring-before(
                        substring-after(PublishedDate, '/'), '/')" />
        <xsl:variable name="monthName" select="$months[@id=$month]" />
        <h3>
            <xsl:value-of select="concat($monthName, ' ', $year)" />
        </h3>
        <xsl:apply-templates
            select="key('byMonthYear', substring-after(PublishedDate, '/'))"
            mode="titles" />
    </xsl:template>
    <xsl:template match="BlogPosts/Post" mode="titles">
        <li>
            <xsl:value-of select="concat(Title, ' - ', PublishedDate)" />
        </li>
    </xsl:template>
</xsl:stylesheet>

我们按月和年分组,并使用查找表将月份的数字转换为相应的名称。请注意使用substring-beforesubstring-after进行日期解析。

在一个迂腐的方面,HTML列表不能包含<li>以外的元素,因此您所需的输出不是有效的HTML。