Html 如何使用xsl查找最高值
我想找到4月份的最高温度,xml还有其他月份。 我如何在xsl中编写代码来检索最高值? 这是我的XML代码Html 如何使用xsl查找最高值,html,xml,xslt,Html,Xml,Xslt,我想找到4月份的最高温度,xml还有其他月份。 我如何在xsl中编写代码来检索最高值? 这是我的XML代码 <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="THISISA3.xsl"?> <forecast qTime="28/10/20 10:00 PM" qLocation="Singapore&qu
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="THISISA3.xsl"?>
<forecast qTime="28/10/20 10:00 PM" qLocation="Singapore">
<weather yyyymmdd="20200430">
<year>2020</year>
<month>04</month>
<date>30</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>32.6</highest>
<lowest>28.4</lowest>
</weather>
<weather yyyymmdd="20200421">
<year>2020</year>
<month>04</month>
<date>21</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>32.2</highest>
<lowest>29.8</lowest>
</weather>
</forecast>
最好提供所需的输出,并向社区展示为获得结果所做的努力,以帮助解决特定问题,而不是编写整个代码。 但是,如果您是xslt新手,那么下面带有注释的代码可以帮助您入门并理解xslt中的内容的使用
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- key is used to identify nodes within an expression -->
<xsl:key name="highTemp" match="highest" use="."/>
<!-- identity template to result the transformation in source XML doc itself -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- matching a root node -->
<xsl:template match="forecast">
<!-- iterating through each 'weather' element -->
<xsl:for-each select="weather">
<!-- selecting node which matches the variable having required highest temperature -->
<xsl:apply-templates select=".[highest=$monthApril]"/>
</xsl:for-each>
</xsl:template>
<xsl:variable name="monthApril">
<!-- iterate only 'weather' elements which are from month of April -->
<xsl:for-each select="//weather[substring(@yyyymmdd,5,2) = '04']">
<!-- sorting in descending order so that node having highest temp comes first -->
<xsl:sort select="key('highTemp', highest)" order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="highest"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
</xsl:stylesheet>
如果您指定了所需的输出内容、变量的设置方式以及使用的xslt版本,那就太好了。
下面是一个示例XSLT 1.0模板(它的名称为'HighestTempFormMonth'),能够指定所需月份(可以在模板的参数'month'中指定),并且能够在您想要执行此操作的任何位置使用所需月份调用此模板
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:output method="xml" indent="yes" encoding="Utf-8"/>
<xsl:variable name="requiredMonth" select="'04'"/>
<xsl:template match="/">
<HighestTemperature>
<value>
<xsl:call-template name="highestTempForMonth">
<xsl:with-param name="month" select="$requiredMonth"/>
</xsl:call-template>
</value>
</HighestTemperature>
</xsl:template>
<xsl:template name="highestTempForMonth">
<xsl:param name="month"/>
<xsl:for-each select="/forecast/weather[month = $month]/highest">
<xsl:sort select="." data-type="number" order="descending"/>
<xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
有一个XSLT2.0等价物(模板部分稍微简单一点)示例
对于这两个示例,如果指定月份没有值,则将指定具有空值的“value”标记。如果有必要,可以通过简单的条件更改此值。如果有人需要帮助,您至少应该发布xslt,以显示您迄今为止尝试了什么以及出现了什么错误。您是否可以分享这是否有帮助或与您的预期输出不同?monthApril做了什么?这背后的含义是什么?它是一个变量(根据您的需求创建),它迭代从四月开始的所有“天气”元素,并获取温度最高的节点。用于与匹配,仅打印与此变量匹配的节点(具有最高温度)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all" version="2.0" >
<xsl:output method="xml" indent="yes" encoding="Utf-8"/>
<xsl:variable name="requiredMonth" select="'04'"/>
<xsl:template match="/">
<HighestTemperature>
<value>
<xsl:call-template name="highestTempForMonth">
<xsl:with-param name="month" select="$requiredMonth"/>
</xsl:call-template>
</value>
</HighestTemperature>
</xsl:template>
<xsl:template name="highestTempForMonth">
<xsl:param name="month"/>
<xsl:value-of select="max(/forecast/weather[month = $month]/highest/xs:double(.))"/>
</xsl:template>
</xsl:stylesheet>