xsl无需硬编码即可自动显示xml数据 [英] xsl automatically show xml data without hardcoding

查看:56
本文介绍了xsl无需硬编码即可自动显示xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我拥有的xml数据

this is the xml data i have

<?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,feb,apr和jun中列出的月份,不对其进行硬编码

i would like to show only the data in a table, with only those month that are listed in xml, feb, apr and jun only, without hardcoding it

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

this is the xsl code i have now, and it is hardcoded in, hence, if i were to remove the only apr data, the first column will be shown, but 2nd column will be empty. how to code in a way that, if that month is not on xml, it will skip to the next one.

<?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>

推荐答案

您必须使用两个功能来修改xsl. 首先,您必须使用

You have to modify the xsl with two features. First of all you have to use muenchian grouping to group the months Afteer use a call template This is your xsl, modified by me.

<?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="html" indent="yes" encoding="UTF-8"/>
  <xsl:key name="group-month" match="weather" use="month" />
 
  <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>

          <xsl:variable name="allWeather" select="weather" />
          <xsl:for-each select="weather[generate-id(.)=generate-id(key('group-month',month))]">
                <xsl:sort select="month"/>
            <!--Feb-->
            <tr bgcolor="LightCyan">

              <!--first column-->
              <th>
                <xsl:call-template name="monthName">
                  <xsl:with-param name="month" select = "month" />
                </xsl:call-template>
                <xsl:value-of select="year"></xsl:value-of>

              </th>

              <!--column 2-->
              <td>
                <!--unlisted list-->
                <xsl:variable name="month" select="month" />
                <xsl:for-each select="$allWeather[month=$month]">
                <ul style="padding-left:20px">
                 
                    <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>
                    

                </ul>
              </xsl:for-each>
              </td>


            </tr>
          </xsl:for-each>
        

        </table>

      </body>

    </html>

  </xsl:template>

  <xsl:template name="monthName">
    <xsl:param name = "month" />
    <xsl:if test="month=02">
      <xsl:text>Feb-</xsl:text>
    </xsl:if>

  
  </xsl:template>

</xsl:stylesheet>

这篇关于xsl无需硬编码即可自动显示xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆