XSLT按日期排序不起作用

时间:2016-01-11 14:12:59

标签: xslt

我正在尝试获取某个地区的最新税务数据。我正在使用以下输入和xslt转换:

<?xml version='1.0' encoding='UTF-8'?>
<root>
    <row>
        <Applies_To>Abbott Twp (Pennsylvania:Potter)</Applies_To>
        <Tax_Code>42530035</Tax_Code>
        <Tax>Local City Withholding (Resident)</Tax>
        <Start_Date>2011-07-01</Start_Date>
        <Tax_Rate>0.005</Tax_Rate>
    </row>
    <row>
        <Applies_To>Abbott Twp (Pennsylvania:Potter)</Applies_To>
        <Tax_Code>42530035</Tax_Code>
        <Tax>Local City Withholding (Resident)</Tax>
        <Start_Date>2015-01-01</Start_Date>
        <Tax_Rate>0.099</Tax_Rate>
    </row>
    <row>
        <Applies_To>Abbott Twp (Pennsylvania:Potter)</Applies_To>
        <Tax_Code>42530035</Tax_Code>
        <Tax>Local City Withholding (Work)</Tax>
        <Start_Date>2011-07-01</Start_Date>
        <Tax_Rate>0</Tax_Rate>
    </row>
    <row>
        <Applies_To>Abbottstown Boro (Pennsylvania:Adams)</Applies_To>
        <Tax_Code>42010033</Tax_Code>
        <Tax>Local City Withholding (Resident)</Tax>
        <Start_Date>2011-07-01</Start_Date>
        <Tax_Rate>0.005</Tax_Rate>
    </row>
    <row>
        <Applies_To>Abbottstown Boro (Pennsylvania:Adams)</Applies_To>
        <Tax_Code>42010033</Tax_Code>
        <Tax>Local City Withholding (Work)</Tax>
        <Start_Date>2012-07-01</Start_Date>
        <Tax_Rate>0.01</Tax_Rate>
    </row>
    <row>
        <Applies_To>Abbottstown Boro (Pennsylvania:Adams)</Applies_To>
        <Tax_Code>42010033</Tax_Code>
        <Tax>xxxx</Tax>
        <Start_Date>2012-07-01</Start_Date>
        <Tax_Rate>0.01</Tax_Rate>
        
    </row>

</root>

XSLT:

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

    <xsl:template match="/">
        <root>
            <xsl:for-each-group select="*/row" 
                group-by="Tax_Code">
                <xsl:sort order="descending" select="Start_Date"/>
                <row>
                    <Tax_Code>
                        <xsl:attribute name="Code" select="current-grouping-key()"/>
                            <xsl:for-each-group select="current-group()" group-by="Tax">
                                <xsl:call-template name="Tax_Code"/>
                            </xsl:for-each-group>  
                    </Tax_Code>
                </row>
            </xsl:for-each-group>
        </root>
    </xsl:template>
    
    <xsl:template name="Tax_Code">
        <Tax_Rate>
            <xsl:attribute name="taxtype">
                <xsl:value-of select="Tax"/>
            </xsl:attribute>
            <xsl:value-of select="Tax_Rate"/>
        </Tax_Rate>
        <date>
            <xsl:value-of select="Start_Date"></xsl:value-of>
        </date>

    </xsl:template>

</xsl:stylesheet>

然后我得到以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <row>
      <Tax_Code Code="42530035">
         <Tax_Rate taxtype="Local City Withholding (Resident)">0.005</Tax_Rate>
         <date>2011-07-01</date>
         <Tax_Rate taxtype="Local City Withholding (Work)">0</Tax_Rate>
         <date>2011-07-01</date>
      </Tax_Code>
   </row>
   <row>
      <Tax_Code Code="42010033">
         <Tax_Rate taxtype="Local City Withholding (Resident)">0.005</Tax_Rate>
         <date>2011-07-01</date>
         <Tax_Rate taxtype="Local City Withholding (Work)">0.01</Tax_Rate>
         <date>2012-07-01</date>
         <Tax_Rate taxtype="xxxx">0.01</Tax_Rate>
         <date>2012-07-01</date>
      </Tax_Code>
   </row>
</root>

正如您所看到的,输出没有税码42530035的最新税率。即使我尝试添加position()= 1,我仍然没有获得最多的条目。我找不到解释为什么排序不起作用的解释。

2 个答案:

答案 0 :(得分:2)

使用

   <date>
        <xsl:value-of select="max(current-group()/xs:date(Start_Date))"></xsl:value-of>
    </date>

输出您所拥有的组中的最高日期。使用xsl:sort的顺序仅应用于外部分组,仅适用于组,在内部组中,您当前输出组中第一个项目的日期(按原始文档顺序)。

答案 1 :(得分:2)

最内层xsl:for-each-group中的项目按文档顺序返回。如果您要处理第一个已排序的项目,可以使用xsl:for-eachxsl:sort current-group()个项目,然后只处理第一个项目:

<xsl:for-each-group select="current-group()" group-by="Tax">                        
  <xsl:for-each select="current-group()">
    <xsl:sort order="descending" select="Start_Date" />
    <xsl:if test="position() = 1">
      <xsl:apply-templates select="." mode="Tax_Code"/>
    </xsl:if>    
  </xsl:for-each>
</xsl:for-each-group>  

完整样式表:

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

    <xsl:template match="/">
        <root>
            <xsl:for-each-group select="*/row" 
                group-by="Tax_Code">
                <xsl:sort order="descending" select="Start_Date"/>
                <row>
                    <Tax_Code>
                        <xsl:attribute name="Code" select="current-grouping-key()"/>
                        <xsl:for-each-group select="current-group()" group-by="Tax">

                            <xsl:for-each select="current-group()">
                                <xsl:sort order="descending" select="Start_Date" />
                                <xsl:if test="position() = 1">
                                    <xsl:apply-templates select="." mode="Tax_Code"/>
                                </xsl:if>    
                            </xsl:for-each>

                        </xsl:for-each-group>  
                    </Tax_Code>
                </row>
            </xsl:for-each-group>
        </root>
    </xsl:template>

    <xsl:template name="Tax_Code" match="row" mode="Tax_Code">
        <Tax_Rate>
            <xsl:attribute name="taxtype">
                <xsl:value-of select="Tax"/>
            </xsl:attribute>
            <xsl:value-of select="Tax_Rate"/>
        </Tax_Rate>
        <date>
            <xsl:value-of select="Start_Date"/>
        </date>      
    </xsl:template>

</xsl:stylesheet>