xsl secara otomatis menampilkan data xml tanpa hardcoding

Nov 03 2020

ini adalah data xml yang saya miliki

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

saya hanya ingin menampilkan data dalam tabel, dengan hanya bulan yang terdaftar di xml, feb, apr dan jun saja, tanpa hardcoding

ini adalah kode xsl yang saya miliki sekarang, dan itu adalah kode keras, oleh karena itu, jika saya menghapus satu-satunya data apr, kolom pertama akan ditampilkan, tetapi kolom ke-2 akan kosong . cara membuat kode sedemikian rupa, jika bulan itu tidak ada di xml, maka bulan itu akan melompat ke bulan berikutnya.

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

Jawaban

EmilioMarino Nov 03 2020 at 23:51

Anda harus memodifikasi xsl dengan dua fitur. Pertama-tama Anda harus menggunakan pengelompokan muenchian untuk mengelompokkan bulan-bulan setelah menggunakan template panggilan Ini adalah xsl Anda, dimodifikasi oleh saya.

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