Xml 如何在xslt中获得同一节点下的最大值
我有一个如下所示的xml:Xml 如何在xslt中获得同一节点下的最大值,xml,xslt,xslt-1.0,xslt-2.0,Xml,Xslt,Xslt 1.0,Xslt 2.0,我有一个如下所示的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> <TotalP
<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>
小时报告
8.
1.
1.
1.
23
12
7.
18
32
.
.
.
28
39
51
49
我需要使用xslt从上述XML中获取最大值。在上述情况下,最大值为51。我怎么能得到呢?也可以在任何xslt变量中获得这个最大值,因此我可以在其他地方使用它。我没有办法。您可以使用任何xslt版本1.0或2.0。此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>
提供以下输出:
<result>51</result>
51
在XSLT 2.0中,以项作为上下文节点,使用
max(*[starts-with(local-name(), 'Hour')])
给定XSLT2.0,使用它就足够了
<xsl:variable name="max" select="max(/Report/Items/Item/*[starts-with(local-name(), 'Hour')]/xs:integer(.)"/>
在XSLT1中,可以使用以下同级
<xsl:value-of select="/Report/Items/Item/*[starts-with(name(), 'Hour')][not(.<following-sibling::*[starts-with(name(), 'Hour')])]"/>
好的。模板,甚至是递归模板,几乎总是比每个或其他循环构造都要快。谢谢!你让我开心!它在xslt 1.0中运行良好:)
<xsl:value-of select="/Report/Items/Item/*[starts-with(name(), 'Hour')][not(.<following-sibling::*[starts-with(name(), 'Hour')])]"/>