XSLT - 按日期对元素进行分组
我想要做的是显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该样式表产生所需的输出:
我们按月份和年份进行分组,并使用查找表将月份的数字转换为其相应的名称。请注意使用
substring-before
和substring-after
进行日期解析。需要注意的是,HTML 列表不能包含
以外的元素,因此您所需的输出不是有效的 HTML。
This stylesheet produces the desired output:
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
andsubstring-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.