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

问题描述 投票:1回答:1

我需要按期间对历史进行分组,但我无法执行此分组。有人可以请帮助。我尝试使用xsl键但其执行操作仅用于第一次响应。你能不能建议任何不同的方法。是否存在任何分组方法,如下面的预期输出所示。

输入

<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>
     </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>
     </RESPONSE>
</TEST>

预期产出

<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>

示例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>
xslt xslt-1.0 xslt-grouping
1个回答
1
投票

主要问题是您需要在密钥中考虑NUMBER,否则您将在整个文档中对所有匹配年份进行分组

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

另外,对于你的第一个xsl:for-each,你用/TEST/RESPONSE/HISTORY/Period开始选择表达式,当你确实需要它相对于当前的HISTORY时,它还将检查文档中的所有Periods,如下所示:

<xsl:for-each select="Period[generate-id(.) = generate-id(key('years', concat(../../NUMBER, '|', @Year))[1])]">

试试这个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>

请注意:

  1. 您不需要在xsl:key中指定匹配元素的完整路径(除非您有其他具有相同名称但您不想匹配的路径的元素)。
  2. 你的模板匹配TEST是不必要的,因为身份模板会做同样的事情。
  3. 您不需要使用xsl:element,其中元素的名称是静态的。只需直接写出元素标签即可。
  4. 您可以使用Attribute Value Templates来简化属性的创建。
© www.soinside.com 2019 - 2024. All rights reserved.