首页 > 解决方案 > xsl:for-each 在表列中

问题描述

我刚刚开始学习 XML/XSL,并且在我的一项作业中遇到了障碍。尝试在此处进行谷歌搜索和搜索,但我似乎找不到具有基本解决方案的问题。所以我想要做的是在列而不是行中显示天气节点。无论我如何尝试编辑我的 tr 或 td,输出始终是单列。不知道我哪里出错了......

期望的输出图片在这里

XML

<weather>
 <year>2019</year>  
 <month>2</month>
 <date>23</date>
 <dayOfWeek>THU</dayOfWeek> 
 <forecast>Plenty of sunshine</forecast>
 <overallCode>sunny</overallCode>
 <hightemperature scale="">25</hightemperature>
 <lowtemperature scale="">11</lowtemperature>
</weather>

<weather>
 <year>2019</year>  
 <month>2</month>
 <date>24</date>
 <dayOfWeek>WED</dayOfWeek> 
 <forecast>Partly sunny</forecast>
 <overallCode>partlySunny</overallCode> 
 <hightemperature scale="">21</hightemperature>
 <lowtemperature scale="">10</lowtemperature>
</weather>

<weather>
 <year>2019</year>  
 <month>2</month>
 <date>25</date>
 <dayOfWeek>TUE</dayOfWeek> 
 <forecast>A morning shower, then rain</forecast>
 <overallCode>rain</overallCode>
 <hightemperature scale="">19</hightemperature>
 <lowtemperature scale="">10</lowtemperature>
</weather>

XSL

<table border="1">

<xsl:for-each select="weather">
<xsl:sort select="date"/>

<tr>
<td>
            <font color="blue">
                <xsl:value-of select="dayOfWeek" /> 
            </font>
                <xsl:text>  </xsl:text>
                <xsl:value-of select="month" />
                <xsl:text>/</xsl:text>
                <xsl:value-of select="date" />
</td>
</tr>                   


<tr>
<td>                
    <img>
        <xsl:attribute name="src">
        <xsl:text>images/</xsl:text>
        <xsl:value-of select="overallCode"/>
        <xsl:text>.png</xsl:text>
        </xsl:attribute>

        <xsl:attribute name="width">
        <xsl:text>60px</xsl:text>
        </xsl:attribute>
    </img>
</td>
</tr>

<tr>
<td>
    <font size="6"><b><xsl:value-of select="hightemperature" />
    <xsl:text>&#176;</xsl:text></b></font>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="lowtemperature" />
    <xsl:text>&#176;</xsl:text>
</td>
</tr>

<tr>
<td><xsl:value-of select="forecast" /></td>
</tr>


</xsl:for-each>
</table>

如果我的代码让你发笑/生气,请原谅我,我还在学习!

标签: xmlxslthtml-table

解决方案


长期以来,移调一直是模式的一个很好的用例。

这个输入

<root>
  <weather>
    <year>2019</year>
    <month>2</month>
    <date>23</date>
    <dayOfWeek>THU</dayOfWeek>
    <forecast>Plenty of sunshine</forecast>
    <overallCode>sunny</overallCode>
    <hightemperature scale="">25</hightemperature>
    <lowtemperature scale="">11</lowtemperature>
  </weather>
  <weather>
    <year>2019</year>
    <month>2</month>
    <date>24</date>
    <dayOfWeek>WED</dayOfWeek>
    <forecast>Partly sunny</forecast>
    <overallCode>partlySunny</overallCode>
    <hightemperature scale="">21</hightemperature>
    <lowtemperature scale="">10</lowtemperature>
  </weather>
  <weather>
    <year>2019</year>
    <month>2</month>
    <date>25</date>
    <dayOfWeek>TUE</dayOfWeek>
    <forecast>A morning shower, then rain</forecast>
    <overallCode>rain</overallCode>
    <hightemperature scale="">19</hightemperature>
    <lowtemperature scale="">10</lowtemperature>
  </weather>
</root>

而这种转变

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" />
    <xsl:key name="kByName" match="weather/*" use="name()" />
    <xsl:template match="text()|weather[position()!=1]" />
    <xsl:template match="root">
        <table>
            <xsl:apply-templates />
        </table>
    </xsl:template>
    <xsl:template match="dayOfWeek|overallCode|hightemperature|forecast">
        <tr>
            <xsl:apply-templates
                select="key('kByName',name())" mode="cell" />
        </tr>
    </xsl:template>
    <xsl:template match="dayOfWeek" mode="cell">
        <td>
            <font color="blue">
                <xsl:value-of select="." />
            </font>
            <xsl:value-of select="concat(' ',../month,'/',../date)" />
        </td>
    </xsl:template>
    <xsl:template match="overallCode" mode="cell">
        <td>
            <img src="images/{.}.png" width="60px" />
        </td>
    </xsl:template>
    <xsl:template match="hightemperature" mode="cell">
        <td>
            <font size="6">
                <b>
                    <xsl:value-of select="concat(.,'&#176;')" />
                </b>
            </font>
            <xsl:value-of
                select="concat('/',../lowtemperature,'&#176;')" />
        </td>
    </xsl:template>
    <xsl:template match="forecast" mode="cell">
        <td>
            <xsl:value-of select="." />
        </td>
    </xsl:template>
</xsl:stylesheet>

结果

<table>
   <tr>
      <td>
         <font color="blue">THU</font> 2/23</td>
      <td>
         <font color="blue">WED</font> 2/24</td>
      <td>
         <font color="blue">TUE</font> 2/25</td>
   </tr>
   <tr>
      <td>Plenty of sunshine</td>
      <td>Partly sunny</td>
      <td>A morning shower, then rain</td>
   </tr>
   <tr>
      <td>
         <img src="images/sunny.png" width="60px"/>
      </td>
      <td>
         <img src="images/partlySunny.png" width="60px"/>
      </td>
      <td>
         <img src="images/rain.png" width="60px"/>
      </td>
   </tr>
   <tr>
      <td>
         <font size="6">
            <b>25°</b>
         </font>/11°</td>
      <td>
         <font size="6">
            <b>21°</b>
         </font>/10°</td>
      <td>
         <font size="6">
            <b>19°</b>
         </font>/10°</td>
   </tr>
</table>

推荐阅读