XSLT - 按日期对元素进行分组

发布于 2024-11-04 01:16:54 字数 1711 浏览 0 评论 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>

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

What Im trying to do is display a list of all blog posts from an XML file which contains a list of all blog post titles and the dates they were published. The kind of output Im looking for is:

HTML Output:

<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>

From the following 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>

Is there a way I can group BlogPosts by date as well as outputting the name of the month they are from using XSLT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌倦 2024-11-11 01:16:54

该样式表产生所需的输出:

<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 列表不能包含

  • 以外的元素,因此您所需的输出不是有效的 HTML。
  • This stylesheet produces the desired output:

    <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>
    

    We group by month and year and use a lookup table to translate a month's digits into its corresponding name. Note the use of substring-before and substring-after for date parsing.

    On a pedantic side note, HTML lists cannot contain elements other than <li>, so your desired output is not valid HTML.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文