我有一个如下所示的 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>
我需要使用 xslt 从上面的 XML 中获取最大值。在上述情况下,最大值为 51。我怎么能得到那个?也有可能在任何 xslt 变量中获得这个最大值,所以我可以在其他地方使用它。我没有任何办法。您可以使用任何 xslt 版本 1.0 或 2.0。
最佳答案
鉴于 XSLT 2.0 它应该足以使用
<xsl:variable name="max" select="max(/Report/Items/Item/*[starts-with(local-name(), 'Hour')]/xs:integer(.)"/>
(样式表需要声明 xmlns:xs="http://www.w3.org/2001/XMLSchema"
)。
对于 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>
关于xml - xslt中如何获取同一节点下的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17963067/