xml 如何在xslt中的同一节点下获得最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17963067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get maximum value under same node in xslt
提问by CodeGuru
I have a xml like below :
我有一个像下面这样的 xml:
<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>HourlyReport</Name>
<Id>8</Id>
<TotalResults>1</TotalResults>
<TotalPages>1</TotalPages>
<Items>
<Item>
<Id>1</Id>
<Hour0>23</Hour0>
<Hour1>12</Hour1>
<Hour2>7</Hour2>
<Hour3>18</Hour3>
<Hour4>32</Hour4>
.
.
.
<Hour20>28</Hour20>
<Hour21>39</Hour21>
<Hour22>51</Hour22>
<Hour23>49</Hour23>
</Item>
</Items>
</Report>
i Need maximum value from above XML using xslt. In above case maximum value is 51. How i can get that? Also is it possible to get this maximum value in any xslt variable, so i can use it some where else. I am not getting any way. You can use any xslt version 1.0 or 2.0 .
我需要使用xslt从上面的 XML 中获得最大值。在上述情况下,最大值为51。我怎么能得到那个?也有可能在任何 xslt 变量中获得这个最大值,所以我可以在其他地方使用它。我没有办法。您可以使用任何 xslt 版本 1.0 或 2.0 。
回答by Martin Honnen
Given XSLT 2.0 it should suffice to use
鉴于 XSLT 2.0 应该足以使用
<xsl:variable name="max" select="max(/Report/Items/Item/*[starts-with(local-name(), 'Hour')]/xs:integer(.)"/>
(where the stylesheet would need to declare xmlns:xs="http://www.w3.org/2001/XMLSchema").
(样式表需要声明的地方xmlns:xs="http://www.w3.org/2001/XMLSchema")。
And with XSLT 1.0 I would simply sort and take the maximum value as in
使用 XSLT 1.0 我会简单地排序并取最大值,如
<xsl:variable name="max">
<xsl:for-each select="/Report/Items/Item/*[starts-with(local-name(), 'Hour')]">
<xsl:sort select="." data-type="number" order="descending"/>
<xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
</xsl:for-each>
</xsl:variable>
回答by Ben L
This XSL:
这个 XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<!-- Putting the maximum hour from the list into a variable. -->
<xsl:variable name="max-hour">
<xsl:call-template name="find-max">
<!-- Select the list of hour elements you want to look at. -->
<xsl:with-param name="hours" select="//*[contains(local-name(), 'Hour')]"/>
</xsl:call-template>
</xsl:variable>
<xsl:template match="*">
<!-- Displaying the result you are after. -->
<result>
<xsl:value-of select="$max-hour"/>
</result>
</xsl:template>
<!-- This template works recursively on the list of hours. -->
<xsl:template name="find-max">
<xsl:param name="hours"/>
<!-- The value of the first hour in this list. -->
<xsl:variable name="this-hour">
<xsl:value-of select="$hours[position() = 1]"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$hours">
<!-- The maximum value of the remaining hours in this list. -->
<xsl:variable name="other-hours">
<xsl:call-template name="find-max">
<xsl:with-param name="hours" select="$hours[position() != 1]"/>
</xsl:call-template>
</xsl:variable>
<!-- Return the maximum of this hour and the remaining hours. -->
<xsl:choose>
<xsl:when test="$other-hours > $this-hour">
<xsl:value-of select="$other-hours"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$this-hour"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<!-- We've reached the last hour in the list. -->
<xsl:otherwise/>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
gives you the following output:
为您提供以下输出:
<result>51</result>
回答by Michael Kay
In XSLT 2.0, with Item as the context node, use
在 XSLT 2.0 中,以 Item 作为上下文节点,使用
max(*[starts-with(local-name(), 'Hour')])
回答by YellowDoc
In XSLT 1 you can use following-sibling
在 XSLT 1 中,您可以使用以下兄弟姐妹
<xsl:value-of select="/Report/Items/Item/*[starts-with(name(), 'Hour')][not(.<following-sibling::*[starts-with(name(), 'Hour')])]"/>

