Cod*_*uru 12 xml xslt xslt-2.0 xslt-1.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>
<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>
Run Code Online (Sandbox Code Playgroud)
我需要使用xslt从XML上面获取最大值.在上述情况下,最大值为51.我怎么能得到那个?也可以在任何xslt变量中获得这个最大值,所以我可以在其他地方使用它.我没有任何办法.您可以使用任何xslt版本1.0或2.0.
Mar*_*nen 17
鉴于XSLT 2.0,它应该足够使用
<xsl:variable name="max" select="max(/Report/Items/Item/*[starts-with(local-name(), 'Hour')]/xs:integer(.)"/>
Run Code Online (Sandbox Code Playgroud)
(样式表需要声明的地方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>
Run Code Online (Sandbox Code Playgroud)