例如,源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>
现在在xsl之后,它应该是下面的内容,它应该根据所有区域的年份总和所有值,并从中计算出最大值。
ex for 2012 : Region1 + Region2 + Region3 + Region4
40.989 + 29.105 + 17.274 + 16.184 = val1
ex for 2013 : Region1 + Region2 + Region3 + Region4
46.876 + 30.869 + 18.912 + 17.507 = val2
ex for 2014 : Region1 + Region2 + Region3 + Region4
53.299 + 32.892 + 20.627 + 18.602 = val3
ex for 2015 : Region1 + Region2 + Region3 + Region4
60.517 + 35.259 + 22.487 + 20.135 = val4
ex for 2016 : Region1 + Region2 + Region3 + Region4
69.149 + 38.011 + 24.492 + 21.764 = val5
我想在变量名(val1,val2,val3,val4,val5)
中输出最多MaxValue
作为输出。
<xsl:variable name='MaxValue'></xsl:variable>
由于
答案 0 :(得分:0)
您的值也可以使用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 1.0 样式表:
可以获得上述结果<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 > 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 > 0]">
<xsl:sort select="." data-type="number" order="ascending"/>
<xsl:if test="position()=last()">
<xsl:value-of select="sum(//rows/row/col[@id > 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>
maxValue
变量的计算方法是,添加每个区域的值,按升序排序,然后获取最后一个值。
如果您使用 XSLT 2.0 ,则不必执行此操作,因为您可以使用max()
功能。这些是绝对XPath表达式中的val1
... val5
变量,maxValue
是使用max()
函数计算的。
<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))"/>
这是一个 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 > 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 > 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>