Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据年份添加值并在xslt中获得最大值?_Xslt - Fatal编程技术网

如何根据年份添加值并在xslt中获得最大值?

如何根据年份添加值并在xslt中获得最大值?,xslt,Xslt,例如,源xml是: <root> <header> <row> <col id="0" attr1="RegionName">RegionName</col> <col id="1" attr1="2012">2012</col> <col id="2" attr1="2013">2013</col> <col

例如,源xml是:

<root>
<header>
    <row>
        <col id="0" attr1="RegionName">RegionName</col>
        <col id="1" attr1="2012">2012</col>
        <col id="2" attr1="2013">2013</col>
        <col id="3" attr1="2014">2014</col>
        <col id="4" attr1="2015">2015</col>
        <col id="5" attr1="2016">2016</col>
     </row>
</header>
<rows>
    <row id="1">
        <col id="0" attr1="RegionName">Region1</col>
        <col id="1" attr1="2012">40.989</col>
        <col id="2" attr1="2013">46.876</col>
        <col id="3" attr1="2014">53.299</col>
        <col id="4" attr1="2015">60.517</col>
        <col id="5" attr1="2016">69.149</col>
     </row>
    <row id="2">
        <col id="0" attr1="RegionName">Region2</col>
        <col id="1" attr1="2012">29.105</col>
        <col id="2" attr1="2013">30.869</col>
        <col id="3" attr1="2014">32.892</col>
        <col id="4" attr1="2015">35.259</col>
        <col id="5" attr1="2016">38.011</col>
    </row>
    <row id="3">
        <col id="0" attr1="RegionName">Region3</col>
        <col id="1" attr1="2012">17.274</col>
        <col id="2" attr1="2013">18.912</col>
        <col id="3" attr1="2014">20.627</col>
        <col id="4" attr1="2015">22.487</col>
        <col id="5" attr1="2016">24.492</col>
    </row>
    <row id="4">
        <col id="0" attr1="RegionName">Region4</col>
        <col id="1" attr1="2012">16.184</col>
        <col id="2" attr1="2013">17.507</col>
        <col id="3" attr1="2014">18.602</col>
        <col id="4" attr1="2015">20.135</col>
        <col id="5" attr1="2016">21.764</col>
    </row>
  </rows>
</root>
我希望val1、val2、val3、val4、val5的最大值作为变量名MaxValue中的输出


谢谢

您的值也可以使用XPath表达式计算。如果您希望以如下格式显示结果数据:

<result>
   <subtotal region="2012">103.55199999999999</subtotal>
   <subtotal region="2013">114.16400000000002</subtotal>
   <subtotal region="2014">125.42</subtotal>
   <subtotal region="2015">138.398</subtotal>
   <subtotal region="2016">153.416</subtotal>
   <max-value>153.416</max-value>
</result>
下面是一个XSLT 2.0解决方案,它使用XPath 2.0 for表达式获取max函数的总数:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="header/row/col">
        <subtotal region="{@attr1}">
            <xsl:value-of select="sum(//rows/row/col[@id &gt; 0][@id=current()[@attr1=current()/@attr1]/@id])"/>
        </subtotal>
    </xsl:template>

    <xsl:template match="root">
        <result>
            <xsl:variable name="maxValue" 
                 select="max(for $col in header/row/col return sum(//rows/row/col[@id &gt; 0][@id=$col[@attr1=$col/@attr1]/@id]))"/>
            <xsl:apply-templates select="header/row/col[@id>0]"/>
            <max-value><xsl:value-of select="$maxValue"/></max-value>
        </result>
    </xsl:template>

</xsl:stylesheet>

您可以使用sum//rows/row/col[@id=//header/row/col[@attr1='2012']]/@id]等表达式对具有相同id的所有列求和,对2012年的列求和。为此编写XSLT非常简单。您想要的输出是什么样子的?你想把结果放在哪里?嗨,谢谢你的回复。但是我的要求是从Val1,val2,var3,val4和val5中得到最大值。103.5519999999911416400000000002125.42138.398153.416634.9499999998抱歉。我一定是把总数和总数弄混了:我更新了答案,现在得到了最大的结果。
<result>
   <subtotal region="2012">103.55199999999999</subtotal>
   <subtotal region="2013">114.16400000000002</subtotal>
   <subtotal region="2014">125.42</subtotal>
   <subtotal region="2015">138.398</subtotal>
   <subtotal region="2016">153.416</subtotal>
   <max-value>153.416</max-value>
</result>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="header/row/col">
        <subtotal region="{@attr1}">
            <xsl:value-of select="sum(//rows/row/col[@id &gt; 0][@id=current()[@attr1=current()/@attr1]/@id])"/>
        </subtotal>
    </xsl:template>

    <xsl:template match="root">
        <result>
            <xsl:variable name="maxValue">
                <xsl:for-each select="header/row/col[@id &gt; 0]">
                    <xsl:sort select="." data-type="number" order="ascending"/>
                    <xsl:if test="position()=last()">
                        <xsl:value-of select="sum(//rows/row/col[@id &gt; 0][@id=current()[@attr1=current()/@attr1]/@id])"/>
                    </xsl:if>
                </xsl:for-each>
            </xsl:variable>
            <xsl:apply-templates select="header/row/col[@id>0]"/>
            <max-value><xsl:value-of select="$maxValue"/></max-value>
        </result>
    </xsl:template>

</xsl:stylesheet>
<xsl:variable name="val1" select="sum(//rows/row/col[@id=//header/row/col[@attr1='2012']/@id])"/>
<xsl:variable name="val2" select="sum(//rows/row/col[@id=//header/row/col[@attr1='2013']/@id])"/>
<xsl:variable name="val3" select="sum(//rows/row/col[@id=//header/row/col[@attr1='2014']/@id])"/>
<xsl:variable name="val4" select="sum(//rows/row/col[@id=//header/row/col[@attr1='2015']/@id])"/>
<xsl:variable name="val5" select="sum(//rows/row/col[@id=//header/row/col[@attr1='2016']/@id])"/>

<xsl:variable name="maxValue" select="max(($val1,$val2,$val3,$val4,$val5))"/>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="header/row/col">
        <subtotal region="{@attr1}">
            <xsl:value-of select="sum(//rows/row/col[@id &gt; 0][@id=current()[@attr1=current()/@attr1]/@id])"/>
        </subtotal>
    </xsl:template>

    <xsl:template match="root">
        <result>
            <xsl:variable name="maxValue" 
                 select="max(for $col in header/row/col return sum(//rows/row/col[@id &gt; 0][@id=$col[@attr1=$col/@attr1]/@id]))"/>
            <xsl:apply-templates select="header/row/col[@id>0]"/>
            <max-value><xsl:value-of select="$maxValue"/></max-value>
        </result>
    </xsl:template>

</xsl:stylesheet>