我有一個XML程序及其首映日期。作爲我的XSLT報告的一部分,我想按財政年度(7月1日至6月30日)劃分結果。最終的輸出結果將顯示在Excel電子表格中的不同選項卡上(但這很簡單)。 我用Munchean分組來劃分我的年份,但是輸出結果是基於我的數據中的@year屬性,我要麼失去第一個或最後一個財政年度,這取決於我的for-each循環。 的示例數據(格式由導出數據庫設置):如何按xslt的財政年度分組?
<?xml version="1.0" encoding="UTF-8"?>
<slots>
<SLOT oid="3229327812">
<schedule_date>
<DATE year="2016" month="6" day="1" monthname="June" dateindays="42155"/>
</schedule_date>
<schedule_channel>
<CHANNEL name="Channel 1"/>
</schedule_channel>
<programme>
<PROG_DETAIL prog_title="Amber Anchor"/>
</programme>
</SLOT>
<SLOT oid="3229327813">
<schedule_date>
<DATE year="2016" month="6" day="30" monthname="June" dateindays="42184"/>
</schedule_date>
<schedule_channel>
<CHANNEL name="Channel 1"/>
</schedule_channel>
<programme>
<PROG_DETAIL prog_title="Big Bang"/>
</programme>
</SLOT>
<SLOT oid="3229327815">
<schedule_date>
<DATE year="2016" month="7" day="30" monthname="July" dateindays="42214"/>
</schedule_date>
<schedule_channel>
<CHANNEL name="Channel 1"/>
</schedule_channel>
<programme>
<PROG_DETAIL prog_title="Car Crash"/>
</programme>
</SLOT>
<SLOT oid="3229327814">
<schedule_date>
<DATE year="2016" month="7" day="1" monthname="July" dateindays="42185"/>
</schedule_date>
<schedule_channel>
<CHANNEL name="Channel 2"/>
</schedule_channel>
<programme>
<PROG_DETAIL prog_title="Deep Dodo"/>
</programme>
</SLOT>
<SLOT oid="3229327815">
<schedule_date>
<DATE year="2017" month="1" day="5" monthname="January" dateindays="42365"/>
</schedule_date>
<schedule_channel>
<CHANNEL name="Channel 2"/>
</schedule_channel>
<programme>
<PROG_DETAIL prog_title="Eerie Earl"/>
</programme>
</SLOT>
<SLOT oid="3229327815">
<schedule_date>
<DATE year="2017" month="7" day="5" monthname="July" dateindays="42531"/>
</schedule_date>
<schedule_channel>
<CHANNEL name="Channel 1"/>
</schedule_channel>
<programme>
<PROG_DETAIL prog_title="Fall Flat"/>
</programme>
</SLOT>
</slots>
XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kfyear1" use="@year" match="slots/SLOT/schedule_date/DATE[@month<7]"/>
<xsl:key name="kfyear2" use="@year" match="slots/SLOT/schedule_date/DATE[@month>6]"/>
<xsl:template match="/">
<body>
<FinancialYear>
<!--<xsl:for-each select="/slots/SLOT/schedule_date/DATE[generate-id() = generate-id(key('kfyear1',@year)[1])]">-->
<xsl:for-each select="/slots/SLOT/schedule_date/DATE[generate-id() = generate-id(key('kfyear2',@year)[1])]">
<xsl:variable name="fy" select="@year + (@month > 6)"/>
<YEAR>
<xsl:text>Financial Year: </xsl:text><xsl:value-of select="$fy"/>
</YEAR>
<premieres>
<xsl:variable name="fy" select="@year + (@month > 6)"/>
<xsl:call-template name="FinYear">
<xsl:with-param name="fyr" select="$fy"/>
</xsl:call-template>
</premieres>
</xsl:for-each>
</FinancialYear>
</body>
</xsl:template>
<xsl:template name="FinYear">
<xsl:param name="fyr"/>
<xsl:for-each select="/slots/SLOT[(schedule_date/DATE/@year+(schedule_date/DATE/@month > 6)=$fyr)]">
<PREMIERE>
<xsl:value-of select="schedule_channel/CHANNEL/@name"/><xsl:text>: </xsl:text>
<xsl:value-of select="programme/PROG_DETAIL/@prog_title"/><xsl:text>: </xsl:text>
<xsl:value-of select="schedule_date/DATE/@day"/><xsl:text> </xsl:text>
<xsl:value-of select="schedule_date/DATE/@monthname"/><xsl:text> </xsl:text>
<xsl:value-of select="schedule_date/DATE/@year"/>
</PREMIERE>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
在我的for-each循環,我得到的財政年2016 & 2017年使用的關鍵 'kfyear1'。使用關鍵'kfyear2',我獲得了2017年財務年度& 2018.我如何獲得所有三個財務年度? 我懷疑我需要放棄for-each循環,並用模板來完成,但我無法弄清楚。
編輯:增加預期的結果:
<?xml version="1.0"?>
<body>
<FinancialYear>
<YEAR>2016</YEAR>
<premieres>
<PREMIERE>Channel 1: Amber Anchor: 1 June 2016</PREMIERE>
<PREMIERE>Channel 1: Big Bang: 30 June 2016</PREMIERE>
</premieres>
</FinancialYear>
<FinancialYear>
<YEAR>2017</YEAR>
<premieres>
<PREMIERE>Channel 1: Car Crash: 30 July 2016</PREMIERE>
<PREMIERE>Channel 2: Deep Dodo: 1 July 2016</PREMIERE>
<PREMIERE>Channel 2: Eerie Earl: 5 January 2017</PREMIERE>
</premieres>
</FinancialYear>
<FinancialYear>
<YEAR>2018</YEAR>
<premieres>
<PREMIERE>Channel 1: Fall Flat: 5 July 2017</PREMIERE>
</premieres>
</FinancialYear>
</body>
謝謝蒂姆,這有助於模板的東西。不幸的是,按照書面規定,XSLT將2016年和2017年財政年度合併爲一年。 – Hockney
我的歉意。我有六月份開始的財政年度,而不是七月份。我糾正了我的答案! –
啊哈!謝謝蒂姆 - 所以你使用的關鍵是正確設置財政年度(我試圖做但失敗),然後祖先把它拉到一起。我需要讓我的頭繞着xpath軸。 – Hockney