首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在xslt中按财政年度分组?

如何在xslt中按财政年度分组?
EN

Stack Overflow用户
提问于 2016-05-16 14:15:16
回答 1查看 135关注 0票数 0

我有一个XML的节目和他们的首映日期。作为报告XSLT的一部分,我希望将结果除以财政年度(7月1日至6月30日)。最后的输出将是excel电子表格中的不同选项卡(但这是容易的部分)。我使用Munchean分组来划分我的年份,但是输出是基于数据中的@年份属性,根据我的for-每个循环,我要么失去了第一个财政年度,要么失去了最后一个财政年度。示例数据(格式由导出数据库设置):

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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&lt;7]"/>
    <xsl:key name="kfyear2" use="@year" match="slots/SLOT/schedule_date/DATE[@month&gt;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 &gt; 6)"/>
                    <YEAR>
                        <xsl:text>Financial Year: </xsl:text><xsl:value-of select="$fy"/>
                    </YEAR>
                    <premieres>
                        <xsl:variable name="fy" select="@year + (@month &gt; 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 &gt; 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>

使用我的“每个循环”中的“kfyear1”键,我将获得2016-2017年的财政年度。我用关键的“第二年”获得2017年和2018年的财政年度。我如何获得所有三个财政年度?我怀疑我需要放弃for-each循环,并使用模板来完成这一切,但我无法确定如何实现。

编辑:添加预期结果:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-16 15:42:12

在这里,你只需要一个键,就可以按财政年度将你的日期分组。

代码语言:javascript
复制
<xsl:key name="slot_by_year" match="DATE" use="@year + number(@month > 6)" />

因此,2016年的一年、6个月、2017年和5个月的"slot_by_year“值都是2016年。

然后,为了获得不同的财政年度(通过获取每个财政年度的DATE第一次出现),您将这样做

代码语言:javascript
复制
<xsl:for-each select="SLOT/schedule_date/DATE[generate-id() = generate-id(key('slot_by_year', @year + number(@month > 6))[1])]"> 

然后要在本财政年度内获得SLOT元素,您可以这样做( $fy设置为当前财政年度)

代码语言:javascript
复制
<xsl:apply-templates select="key('slot_by_year', $fy)/ancestor::SLOT"/>

试试这个XSLT

代码语言:javascript
复制
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml" indent="yes" />
  <xsl:key name="slot_by_year" match="DATE" use="@year + number(@month > 6)" />

  <xsl:template match="/slots">
    <body>
      <xsl:for-each select="SLOT/schedule_date/DATE[generate-id() = generate-id(key('slot_by_year', @year + number(@month > 6))[1])]">
        <xsl:variable name="fy" select="@year + number(@month > 6)"/>
        <financial_year>
          <YEAR><xsl:value-of select="$fy" /></YEAR>
          <premieres>
            <xsl:apply-templates select="key('slot_by_year', $fy)/ancestor::SLOT"/>
          </premieres>
        </financial_year>
      </xsl:for-each>
    </body>
  </xsl:template>

  <xsl:template match="SLOT">
    <PREMIERE>
      <xsl:value-of select="schedule_channel/CHANNEL/@name"/>
      <xsl:text>: </xsl:text>
      <xsl:value-of select="programme/PROG_DETAIL/@prog_title"/>
    </PREMIERE>
  </xsl:template>
</xsl:stylesheet>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37256082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档