xsl automatically show xml data without hardcoding

后端 未结 1 563
耶瑟儿~
耶瑟儿~ 2020-12-22 12:39

this is the xml data i have






        
相关标签:
1条回答
  • 2020-12-22 13:19

    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>

    0 讨论(0)
提交回复
热议问题