xsl은 하드 코딩없이 xml 데이터를 자동으로 표시합니다.
Nov 03 2020
이것은 내가 가지고있는 xml 데이터입니다.
<?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에 나열된 달만 테이블에 데이터 만 표시하고 싶습니다.
이것은 내가 지금 가지고있는 xsl 코드이며 하드 코딩되어 있으므로 apr 데이터 만 제거하면 첫 번째 열이 표시되지만 두 번째 열은 비어 있습니다. 그 달이 xml에 없으면 다음 달로 건너 뛰는 방식으로 코딩하는 방법.
<?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>
답변
EmilioMarino Nov 03 2020 at 23:51
두 가지 기능으로 xsl을 수정해야합니다. 우선 muenchian 그룹화 를 사용하여 월을 그룹화 해야합니다. Afteer는 통화 템플릿을 사용합니다. 이것은 내가 수정 한 xsl입니다.
<?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>