2013-11-25 79 views
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> 

回答

1

定義你的密鑰

<xsl:key name="groupbydate" match="entry" use="substring-before(updated, 'T')"/> 

,那麼你需要使用例如它

<xsl:template match="feed"> 
    <blah> 
     <xsl:apply-templates 
      select="entry[ 
       generate-id() 
       = generate-id(key('groupbydate', substring-before(updated, 'T'))[1])]" /> 
    </blah> 
</xsl:template> 

那麼你處理與

<xsl:template match="entry"> 
    <today> 
    <date><xsl:value-of select="substring-before(updated, 'T')"/></date> 
    <xsl:apply-templates select="key('groupbydate', substring-before(updated, 'T'))" mode="entry"/> 
    </today> 
</xsl:template> 

在組中的每個第一項和每個組中的項目與

<xsl:template match="entry" mode="entry"> 
    <post> 
    <xsl:copy-of select="node()"/> 
    </post> 
</xsl:template> 
+1

兩種。非常感謝你,因爲它幫助我瞭解和解決我的問題。深深體會。 – karlcow