xml - 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>°</xsl:text></b></font>
<xsl:text>/</xsl:text>
<xsl:value-of select="lowtemperature" />
<xsl:text>°</xsl:text>
</td>
</tr>
<tr>
<td><xsl:value-of select="forecast" /></td>
</tr>
</xsl:for-each>
</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(.,'°')" />
</b>
</font>
<xsl:value-of
select="concat('/',../lowtemperature,'°')" />
</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>
推荐阅读
- r - 如何为两个分类变量创建列联表(交叉表)?
- video - 有没有一种简单有效的方法来读取文件并将它们转换为指定的 DXGI_Format 并解码输出大小?
- asp.net-mvc - 添加 turf.js 脚本标签
- node.js - 当 html 被先前的 ajax 发布操作的结果替换时,Ajax 发布操作不起作用
- javascript - ForEach 循环仅将最后一个值应用于变量
- excel - 使用 Excel VBA 是否可以将整个工作表的 NumberFormat 复制到新工作表?
- python - SocketIo 与 Django 的集成
- python - 仅使用 randint 创建指定范围内的随机偶数列表
- paraview - 导出顶点连接(三角形)和顶点颜色
- python - 使用 python 和一个名为 ArcREST 的包正确导入包