【问题标题】:XSLT - Group elements by dateXSLT - 按日期对元素进行分组
【发布时间】:2011-08-13 02:12:43
【问题描述】:

我试图做的是显示一个 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>

有没有一种方法可以按日期对博客帖子进行分组,并使用 XSLT 输出它们所在月份的名称?

【问题讨论】:

    标签: xslt 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 列表不能包含 &lt;li&gt; 以外的元素,因此您想要的输出不是有效的 HTML。

    【讨论】:

    • +1 正确答案。是的,正确的层次结构应该是 (ul (li (h3) (ul (li))))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多