Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html xsl自动显示xml数据,无需硬编码_Html_Xml_Xslt - Fatal编程技术网

Html xsl自动显示xml数据,无需硬编码

Html xsl自动显示xml数据,无需硬编码,html,xml,xslt,Html,Xml,Xslt,这是我拥有的xml数据 <?xml version="1.0" encoding="UTF-8"?> <!--xsl file link--> <?xml-stylesheet type="text/xsl" href="test.xsl"?> <!--xsd file link--> <forecast qTime="28/10/20 10:00

这是我拥有的xml数据

<?xml version="1.0" encoding="UTF-8"?>

<!--xsl file link-->
<?xml-stylesheet type="text/xsl" href="test.xsl"?>

<!--xsd file link-->
<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="20200218">
    <year>2020</year>   
    <month>02</month>
    <date>18</date>
    <comment>Plenty of sunshine</comment>
    <code>sunny</code>
    <highest>34.6</highest>
    <lowest>30.5</lowest>
  </weather>
  <weather yyyymmdd="20200210">
    <year>2020</year>   
    <month>02</month>
    <date>10</date>
    <comment>Partly sunny</comment>
    <code>partlySunny</code>
    <highest>33.1</highest>
    <lowest>29.2</lowest>
  </weather>
  <weather yyyymmdd="20200616">
    <year>2020</year>   
    <month>06</month>
    <date>16</date>
    <comment>Considerable clouds</comment>
    <code>cloudy</code>
    <highest>30.5</highest>
    <lowest>25.4</lowest>
  </weather>
</forecast>
我只想在一个表中显示数据,只显示xml中列出的月份,只显示二月、四月和六月,而不硬编码它

这是我现在拥有的xsl代码,它是硬编码的,因此,如果我要删除唯一的apr数据,第一列将显示,但第二列将为空。如何以一种方式编码,如果那个月不在xml上,它将跳到下一个月

<?xml version="1.0"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<xsl:template match="/forecast">
  

<!--start of xsl-->
<html>

<!--start of the table-->
    <body>

    <table>
        <tr bgcolor="LightSalmon">
            <th>Date</th>
            <th>Weather data</th>
        </tr>
            
<!--Feb-->
        <tr bgcolor="LightCyan">

<!--first column-->
        <th> Feb 20 </th>
        
<!--column 2-->
        <td>
<!--unlisted list-->
        <ul style="padding-left:20px">
            <xsl:for-each select="weather">
            <xsl:sort select="date"/>
            <xsl:if test="month=02"> <!--show the following details is month = "02"-->
                <li>
                    <xsl:value-of select="date"/> 
                    <xsl:text>/</xsl:text>
                    <xsl:value-of select="month"/>
                    <xsl:text>/</xsl:text>
                    <xsl:value-of select="substring(year, string-length(year)-1)" />
                    <xsl:text>, from </xsl:text>
                    <xsl:value-of select="lowest"/>
                    <xsl:text>°C to </xsl:text>
                    <xsl:value-of select="highest"/>
                    <xsl:text>°C, </xsl:text>
                    <xsl:value-of select="comment"/>
                
                </li>
            </xsl:if>
            </xsl:for-each>
        </ul>
        </td>
        

        </tr> <!--end of feb row-->
        
        
<!--Apr-->
        <tr bgcolor="Bisque">
        
<!--first column-->
        <th> Apr 20 </th>
        
<!--column 2-->
        <td>
<!--unlisted list-->
        <ul style="padding-left:20px">
            <xsl:for-each select="weather">
            <xsl:sort select="date"/>
            <xsl:if test="month=04"> 
                <li>
                    <xsl:value-of select="date"/> 
                    <xsl:text>/</xsl:text>
                    <xsl:value-of select="month"/>
                    <xsl:text>/</xsl:text>
                    <xsl:value-of select="substring(year, string-length(year)-1)" />
                    <xsl:text>, from </xsl:text>
                    <xsl:value-of select="lowest"/>
                    <xsl:text>°C to </xsl:text>
                    <xsl:value-of select="highest"/>
                    <xsl:text>°C, </xsl:text>
                    <xsl:value-of select="comment"/>
                </li>   
                
            </xsl:if>
            </xsl:for-each>
        </ul>
        </td>
        

        </tr> <!--end of apr row-->

    </table>

    </body>
    
</html>

</xsl:template> 
</xsl:stylesheet>

日期
天气数据
2月20日
  • / / 从…起 摄氏度至 摄氏度,
4月20日
  • / / 从…起 摄氏度至 摄氏度,

您必须使用两个特性修改xsl。 首先,你必须使用分组月份 Afteer使用呼叫模板 这是您的xsl,由我修改


日期
天气数据
  • / / 从…起 摄氏度至 摄氏度,
二月-
当同一个月有两个天气要素,最高和最低的值不同时,您希望显示什么?您的解决方案假设他使用msxml引擎。Muenchian分组对您的解决方案仍然有效。我仅在将日期转换为月份时使用c。您可以使用新版本的xsl吗?否则,您可以使用模板和调用模板来定义数字月份和名称的映射。请看我的答案中的xsl。我编辑ittry
  • 好的,我试着解释一下。im do loop of$allWeather=是一个变量,它包含节点weather[]=方括号是一个过滤器month=是$allWeather组中的所有月份(二月、二月、四月、六月)$month=是一个变量,其值为循环中的实际天气/月份ex.02。简单地说,我正在对所有天气信息进行循环,但对实际值进行过滤。首先,每个组有3个元素(2月、4月、6月)。当我转到第二个时,我说:循环天气($allWeather),但仅针对2月(月=$month)