我希望有人能夠幫助我,因爲我一直在試圖弄清楚如何將數據集轉換爲分組在多個級別上的表結構。第一級是事件日期,第二級是公司名稱,最後一級是用戶。xsl用於對多層結果進行分組的轉換
以下是XML。
<xmlData>
<records>
<record>
<userid>1</userid>
<usersname>Jane Doe</usersname>
<companyname>Company A</companyname>
<eventdate>01 FEB 2017</eventdate>
<jeventdate>2457786</jeventdate>
</record>
<record>
<userid>3</userid>
<usersname>Jane Doe</usersname>
<companyname>Company B</companyname>
<eventdate>01 FEB 2017</eventdate>
<jeventdate>2457786</jeventdate>
</record>
<record>
<userid>2</userid>
<usersname>Joe Smith</usersname>
<companyname>Company B</companyname>
<eventdate>01 DEC 2016</eventdate>
<jeventdate>2457724</jeventdate>
</record>
<record>
<userid>2</userid>
<usersname>Joe Smith</usersname>
<companyname>Company B</companyname>
<eventdate>01 JAN 2017</eventdate>
<jeventdate>2457755</jeventdate>
</record>
<record>
<userid>2</userid>
<usersname>Joe Smith</usersname>
<companyname>Company B</companyname>
<eventdate>01 FEB 2017</eventdate>
<jeventdate>2457786</jeventdate>
</record>
</records>
</xmlData>
我試圖得到的結果顯示在以下簡單的HTML輸出中。
<h1>01 DEC 2016</h1>
<h2>Company B</h2>
<table>
<tr><td>2</td><td>Joe Smith</td></tr>
</table>
<h1>01 JAN 2017</h1>
<h2>Company B</h2>
<table>
<tr><td>2</td><td>Joe Smith</td></tr>
</table>
<h1>01 FEB 2017</h1>
<h2>Company A</h2>
<table>
<tr><td>1</td><td>Jane Doe</td></tr>
</table>
<h2>Company B</h2>
<table>
<tr><td>3</td><td>Dave Dodd</td></tr>
<tr><td>2</td><td>Joe Smith</td></tr>
</table>
我遇到的問題實際上是兩部分。首先是獲得三個層次的深度。第二個是獲取所有記錄,而不僅僅是前幾個。
這是我一直在使用的XSL。
<xsl:key name="monthof" match="record" use="eventdate"/>
<xsl:key name="companyof" match="record" use="concat(eventdate,'|',companyname)"/>
<xsl:key name="userof" match="record" use="concat(eventdate,'|',companyname,'|',usersname)"/>
<xsl:template match="xmlData/records">
<xsl:for-each select="record[key('monthof',eventdate)]">
<xsl:sort select="jeventdate"/>
<xsl:variable name="lstEventDate" select="key('monthof',eventdate)" />
<h2><xsl:value-of select="eventdate"/></h2>
<xsl:for-each select="key('companyof',concat(eventdate,'|',companyname))">
<xsl:sort select="companyname"/>
<h3>
<xsl:value-of select="companyname"/>
</h3>
<table>
<xsl:for-each select="key('userof',concat(eventdate,'|',companyname,'|',usersname))">
<tr>
<td><xsl:value-of select="userid"/></td>
<td><xsl:value-of select="usersname"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
這裏是我得到的輸出。
01 DEC 2016
Company B
2 Jane Doe
01 JAN 2017
Company B
2 Jane Doe
01 FEB 2017
Company A
1 Joe Smith
01 FEB 2017
Company B
1 Joe Smith
Company B
3 Dave Dodd
01 FEB 2017
Company B
1 Joe Smith
Company B
2 Jane Doe
開始:http://www.jenitennison.com/xslt/grouping/muenchian.html然後做了多層次Muenchian分組的例子搜索在這裏。 –
對於分組問題,您需要指定XSLT 1.0或2.0,因爲答案會非常不同。 –