我应该如何在xslt中分组数据?

我应该如何在xslt中分组数据?,xslt,xslt-1.0,xslt-grouping,Xslt,Xslt 1.0,Xslt Grouping,我需要按时段对历史进行分组,但我无法执行此分组。 有人能帮忙吗。我尝试使用xsl键,但它只对第一个响应执行操作。 你能提出不同的建议吗。是否有如下预期输出所示的分组方法 输入 XXXX ZZZZ 预期产量 <TEST> <RESPONSE> <NUMBER>XXXX</NUMBER> <HISTORY> <Period Year="2013" Month="A

我需要按时段对历史进行分组,但我无法执行此分组。 有人能帮忙吗。我尝试使用xsl键,但它只对第一个响应执行操作。 你能提出不同的建议吗。是否有如下预期输出所示的分组方法

输入


XXXX
ZZZZ
预期产量

<TEST>
     <RESPONSE>
        <NUMBER>XXXX</NUMBER>
        <HISTORY>
             <Period Year="2013" Month="Apr" Value="77"></Period>
             <Period Year="2013" Month="Mar" Value="99"></Period>
             <Period Year="2013" Month="Feb" Value="88"></Period>
             <Period Year="2012" Month="Jan" Value="11"></Period>
             <Period Year="2012" Month="Mar" Value="22"></Period>
             <Period Year="2011" Month="Apr" Value="444"></Period>
         </HISTORY>
         <GROUP-HISTORY>
                <YEAR Value="2013">
                        <Months Month="Apr" Value="77"/>
                        <Months Month="Mar" Value="99"/>
                        <Months Month="Feb" Value="88"/>
                </YEAR>
                <YEAR Value="2012">
                        <Months Month="Jan" Value="11"/>
                        <Months Month="Mar" Value="22"/>
                </YEAR>
                <YEAR Value="2011">
                        <Months Month="Apr" Value="444"/>       
                </YEAR>
         </GROUP-HISTORY>
     </RESPONSE>
     <RESPONSE>
        <NUMBER>ZZZZ</NUMBER>
        <HISTORY>
             <Period Year="2016" Month="Jan" Value="999"></Period>
             <Period Year="2016" Month="Mar" Value="454"></Period>
             <Period Year="2015" Month="Dec" Value="234"></Period>
             <Period Year="2014" Month="Jan" Value="767"></Period>
             <Period Year="2014" Month="Sep" Value="667"></Period>
             <Period Year="2013" Month="May" Value="112"></Period>
         </HISTORY>
         <GROUP-HISTORY>
                <YEAR Value="2016">
                        <Months Month="Jan" Value="999"/>
                        <Months Month="Mar" Value="454"/>   
                </YEAR>
                <YEAR Value="2015">
                        <Months Month="Dec" Value="234"/>       
                </YEAR>
                <YEAR Value="2014">
                        <Months Month="Jan" Value="767"/>   
                        <Months Month="Sep" Value="667"/>                               
                </YEAR>
                <YEAR Value="2013">
                        <Months Month="May" Value="112"/>       
                </YEAR>
         </GROUP-HISTORY>
     </RESPONSE>
</TEST>

XXXX
ZZZZ
示例xslt

<xsl:stylesheet xmlns:xalan="http://xml.apache.org/xalan"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:key name="years" match="/TEST/RESPONSE/HISTORY/Period" use="@Year"/>
    <xsl:template match="TEST">
        <xsl:element name="TEST">
            <xsl:apply-templates select="RESPONSE"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="HISTORY">
        <xsl:element name="GROUP-HISTORY">
            <xsl:for-each
                select="/TEST/RESPONSE/HISTORY/Period[generate-id(.) = generate-id(key('years', @Year)[1])]">
                <xsl:sort select="@Year" order="descending"/>
                <xsl:variable name="currY" select="@Year"/>
                <xsl:element name="Year">
                    <xsl:attribute name="Value">
                        <xsl:value-of select="$currY"/>
                    </xsl:attribute>
                    <xsl:for-each select="/TEST/RESPONSE/HISTORY/Period[@Year = $currY]">
                        <xsl:element name="Months">
                            <xsl:attribute name="Month">
                                <xsl:value-of select="@Month"/>
                            </xsl:attribute>
                            <xsl:attribute name="Value">
                                <xsl:value-of select="@Value"/>
                            </xsl:attribute>
                        </xsl:element>
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

主要问题是您需要说明密钥中的
编号
,否则您将在整个文档中对所有匹配年份进行分组

<xsl:key name="years" match="Period" use="concat(../../NUMBER, '|', @Year)"/>
试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:key name="years" match="Period" use="concat(../../NUMBER, '|', @Year)"/>

    <xsl:template match="node() | @*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="HISTORY">
        <xsl:call-template name="identity" />
        <GROUP-HISTORY>
            <xsl:for-each select="Period[generate-id(.) = generate-id(key('years', concat(../../NUMBER, '|', @Year))[1])]">
                <xsl:sort select="@Year" order="descending"/>
                <xsl:variable name="currY" select="@Year"/>
                <Year Value="{$currY}">
                    <xsl:for-each select="key('years', concat(../../NUMBER, '|', $currY))">
                        <Months Month="{@Month}" Value="{@Value}" />
                    </xsl:for-each>
                </Year>
            </xsl:for-each>
        </GROUP-HISTORY>
    </xsl:template>
</xsl:stylesheet>

请注意:

  • 您不需要在
    xsl:key
    中指定匹配元素的完整路径(除非您有其他同名但不希望匹配的不同路径的元素)
  • 您的模板匹配
    TEST
    是不必要的,因为身份模板也会做同样的事情
  • 如果元素的名称是静态的,则不需要使用
    xsl:element
    。直接写出元素标记
  • 可以使用简化属性的创建

  • 您能发布您的XSLT吗,这样我们就可以纠正它存在的任何问题?如果您使用的是
    xsl:key
    ,那么您实际上可能并不适合脱离解决方案。非常感谢。您可以在上面找到xslt Hi Tim,谢谢您的输入,但我在某些情况下得到的数字是重复的,那么我应该如何处理这个问题呢?您可以在上一节看到它的作用。您是否在XSLT中添加了更多模板?我尝试过,但仍然面临问题您只需在输入中用XXXX替换ZZZZ并运行,您将获得不同于预期的输出,那么,在您的实际XML中是否可以有多个具有相同编号的
    响应
    元素?是的,我在实际XML中有多个具有相同编号的响应元素
    <xsl:for-each select="Period[generate-id(.) = generate-id(key('years', concat(../../NUMBER, '|', @Year))[1])]">
    
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
        <xsl:key name="years" match="Period" use="concat(../../NUMBER, '|', @Year)"/>
    
        <xsl:template match="node() | @*" name="identity">
            <xsl:copy>
                <xsl:apply-templates select="node() | @*"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="HISTORY">
            <xsl:call-template name="identity" />
            <GROUP-HISTORY>
                <xsl:for-each select="Period[generate-id(.) = generate-id(key('years', concat(../../NUMBER, '|', @Year))[1])]">
                    <xsl:sort select="@Year" order="descending"/>
                    <xsl:variable name="currY" select="@Year"/>
                    <Year Value="{$currY}">
                        <xsl:for-each select="key('years', concat(../../NUMBER, '|', $currY))">
                            <Months Month="{@Month}" Value="{@Value}" />
                        </xsl:for-each>
                    </Year>
                </xsl:for-each>
            </GROUP-HISTORY>
        </xsl:template>
    </xsl:stylesheet>