首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将xml转换为具有排序元素的表

将xml转换为具有排序元素的表
EN

Stack Overflow用户
提问于 2013-11-17 22:26:47
回答 2查看 802关注 0票数 1

我有以下xml文件

代码语言:javascript
复制
?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=“星期一”上发生的课程将出现在表的第一行上,并且与其他几天的课程不同)--我已经这样做了:

代码语言:javascript
复制
     <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>

我对如何使它们分类和着色有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-18 06:12:08

XML:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>

输出:

代码语言:javascript
复制
<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来给其他日子颜色。

代码语言:javascript
复制
<xsl:when test="Day = 'Tuesday'">
       <td bgcolor="#C66666">
            <xsl:value-of select="Day" />
       </td>

您可能需要查看这些链接,以了解更多关于排序和颜色的信息。

sort.asp

choose.asp

票数 0
EN

Stack Overflow用户

发布于 2013-11-18 20:19:21

假设您在说“排序”时,您是指按时间顺序排列的,那么我建议为Lecture元素创建一个模板,在其中使用Day元素的谓词使其按顺序排列。然后,您可以使用属性的条件只对周一类进行着色。

下面是模板的样子

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20037002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档