1
給定一個結構,它看起來像:XSLT 1.0 GROUPBY與嵌套結構
<feed>
<entry>
<summary>lorem ipsum 1</summary>
<updated>2013-11-20T18:40:00Z</updated>
<author>Jon</author>
</entry>
<entry>
<summary>lorem ipsum 2</summary>
<updated>2013-11-19T19:40:00Z</updated>
<author>Jon</author>
</entry>
<entry>
<summary>lorem ipsum 3</summary>
<updated>2013-11-19T23:40:00Z</updated>
<author>Rebecca</author>
</entry>
<entry>
<summary>lorem ipsum 4</summary>
<updated>2013-11-18T05:40:00Z</updated>
<author>Paul</author>
</entry>
</feed>
我要創建具有類似於這種結構的輸出中的XSLT(名稱並不重要。)
<blah>
<today>
<date>2013-11-20</date>
<post>
<summary>lorem ipsum 1</summary>
<updated>2013-11-20T18:40:00Z</updated>
<author>Jon</author>
</post>
</today>
<today>
<date>2013-11-19</date>
<post>
<summary>lorem ipsum 2</summary>
<updated>2013-11-19T19:40:00Z</updated>
<author>Jon</author>
</post>
<post>
<summary>lorem ipsum 3</summary>
<updated>2013-11-19T23:40:00Z</updated>
<author>Rebecca</author>
</post>
</today>
<today>
<date>2013-11-18</date>
<post>
<summary>lorem ipsum 4</summary>
<updated>2013-11-18T05:40:00Z</updated>
<author>Paul</author>
</post>
</today>
</blah>
我明白我可能應該使用methods defined for grouping之一,但我還沒有弄明白。
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:key name="groupbydate" match="entry" use="updated"/>
<xsl:template match="feed">
<blah>
<xsl:apply-templates
select="entry[
generate-id()
= generate-id(key('groupbydate', updated)[1])]" />
</blah>
</xsl:template>
<xsl:template match="entry">
<!-- GET the date -->
<xsl:variable name="date" select="substring(updated,1,10)"/>
<today>
<date><xsl:value-of select="$date"/></date>
</today>
<xsl:for-each select="entry">
<xsl:if test="preceding-sibling::entry[substring(updated,1,10) = $date]">
<post>
<xsl:copy-of select="entry"/>
</post>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
兩種。非常感謝你,因爲它幫助我瞭解和解決我的問題。深深體會。 – karlcow