xsl muestra automáticamente datos xml sin codificación
Nov 03 2020
estos son los datos xml que tengo
<?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>
me gustaría mostrar solo los datos en una tabla, con solo los meses que se enumeran en xml, feb, apr y jun solo, sin codificarlos
este es el código xsl que tengo ahora, y está codificado, por lo tanto, si eliminara los únicos datos de abril, se mostrará la primera columna, pero la segunda columna estará vacía . cómo codificar de una manera que, si ese mes no está en xml, salte al siguiente.
<?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>
Respuestas
EmilioMarino Nov 03 2020 at 23:51
Tienes que modificar el xsl con dos características. Primero que nada tienes que usar la agrupación muenchian para agrupar los meses. Después usa una plantilla de llamada. Este es tu xsl, modificado por mí.
<?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>