xsl mostra automaticamente i dati xml senza hardcoding

Nov 03 2020

questi sono i dati xml che ho

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

vorrei mostrare solo i dati in una tabella, con solo quei mesi che sono elencati solo in xml, feb, apr e jun, senza codificarli

questo è il codice xsl che ho ora ed è hardcoded, quindi, se dovessi rimuovere gli unici dati apr, verrà mostrata la prima colonna, ma la seconda colonna sarà vuota . come programmare in modo tale che, se quel mese non è su xml, salti a quello successivo.

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

Risposte

EmilioMarino Nov 03 2020 at 23:51

Devi modificare xsl con due funzionalità. Prima di tutto devi usare il raggruppamento muenchiano per raggruppare i mesi dopo aver usato un modello di chiamata Questo è il tuo xsl, modificato da 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>