我有以下xml文件
?xml version="1.0"?>
<Schedule>
<Lesson>
<Title>Maths</Title>
<Lecture Classroom="100">
<Day>Tuesday</Day>
<Time>12:00</Time>
</Lecture>
<Lecture Classroom="101">
<Day>Thursday</Day>
<Time>11:00</Time>
</Lecture>
</Lesson>
<Lesson>
<Title>Scientific Computing</Title>
<Lecture Classroom="103">
<Day>Monday</Day>
<Time>09:00</Time>
</Lecture>
</Lesson>
}我只想让元素以表格的形式出现,但我希望它们按日进行排序,并按组着色(例如,在Day=“星期一”上发生的课程将出现在表的第一行上,并且与其他几天的课程不同)--我已经这样做了:
<table border="1">
<tr bgcolor="#888888 ">
<th>Title</th>
<th>Professor</th>
<th>Day</th>
</tr>
<xsl:for-each select="Schedule/Lesson">
<tr bgcolor="#F00000 ">
<td><xsl:value-of select="Title"/> </td>
<td><xsl:value-of select="Professor"/> </td>
<td><xsl:value-of select="Lecture/Day"/> </td>
</tr>
</xsl:for-each>
</table>我对如何使它们分类和着色有什么想法吗?
发布于 2013-11-18 06:12:08
XML:
<?xml version="1.0"?>
<Schedule>
<Lesson>
<Title>Maths</Title>
<Lecture Classroom="100">
<Day>Tuesday</Day>
<Time>12:00</Time>
</Lecture>
<Lecture Classroom="101">
<Day>Thursday</Day>
<Time>11:00</Time>
</Lecture>
</Lesson>
<Lesson>
<Title>Scientific Computing</Title>
<Lecture Classroom="103">
<Day>Monday</Day>
<Time>09:00</Time>
</Lecture>
</Lesson>
</Schedule>XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#888888">
<th>Title</th>
<th>Professor</th>
<th>Day</th>
</tr>
<xsl:for-each select="Schedule/Lesson/Lecture">
<xsl:sort select="Day" />
<tr>
<td>
<xsl:value-of select="../Title" />
</td>
<td>
<xsl:value-of select="@Classroom" />
</td>
<xsl:choose>
<xsl:when test="Day = 'Monday'">
<td bgcolor="#F00000">
<xsl:value-of select="Day" />
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of select="Day" />
</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>输出:
<html>
<body>
<table border="1">
<tr bgcolor="#888888">
<th>Title</th>
<th>Professor</th>
<th>Day</th>
</tr>
<tr>
<td>Scientific Computing</td>
<td>103</td>
<td bgcolor="#F00000">Monday</td>
</tr>
<tr>
<td>Maths</td>
<td>101</td>
<td>Thursday</td>
</tr>
<tr>
<td>Maths</td>
<td>100</td>
<td>Tuesday</td>
</tr>
</table>
</body>
</html>意思是'Mondays‘将以红色颜色表示。
如果需要的话,只需添加更多的when test来给其他日子颜色。
<xsl:when test="Day = 'Tuesday'">
<td bgcolor="#C66666">
<xsl:value-of select="Day" />
</td>您可能需要查看这些链接,以了解更多关于排序和颜色的信息。
sort.asp
choose.asp
发布于 2013-11-18 20:19:21
假设您在说“排序”时,您是指按时间顺序排列的,那么我建议为Lecture元素创建一个模板,在其中使用Day元素的谓词使其按顺序排列。然后,您可以使用属性的条件只对周一类进行着色。
下面是模板的样子
<xsl:template match="Schedule">
<table>
<tr>
<td>Title</td>
<td>Professor</td>
<td>Day</td>
</tr>
<xsl:apply-templates select="Lesson/Lecture[Day='Monday']"/>
<xsl:apply-templates select="Lesson/Lecture[Day='Tuesday']"/>
<xsl:apply-templates select="Lesson/Lecture[Day='Wednesday']"/>
<xsl:apply-templates select="Lesson/Lecture[Day='Thursday']"/>
<xsl:apply-templates select="Lesson/Lecture[Day='Friday']"/>
</table>
</xsl:template>
<xsl:template match="Lecture">
<tr>
<td>
<xsl:value-of select="../Title"/>
</td>
<td>
<xsl:value-of select="@Classroom"/>
</td>
<td>
<xsl:if test="Day = 'Monday'">
<xsl:attribute name="style">background-color:red;</xsl:attribute>
</xsl:if>
<xsl:value-of select="Day"/>
</td>
</tr>
</xsl:template>https://stackoverflow.com/questions/20037002
复制相似问题