Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 将xml转换为具有排序元素的表_Html_Xml_Sorting_Xslt_Html Table - Fatal编程技术网

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

Html 将xml转换为具有排序元素的表,html,xml,sorting,xslt,html-table,Html,Xml,Sorting,Xslt,Html Table,我有以下xml文件 ?xml version="1.0"?> <Schedule> <Lesson> <Title>Maths</Title> <Lecture Classroom="100"> <Day>Tuesday</Day>

我有以下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>
}
?xml version=“1.0”>
数学
星期二
12:00
星期四
11:00
科学计算
星期一
09:00
}
我只想将元素设置为表格形式,但我希望它们按天分类并分组着色(例如,在day=“Monday”发生的课程位于表格的第一行,并且颜色与其他几天的课程不同) 我已经做到了:

     <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>

标题
教授
白天
有没有关于如何使它们正确排序和着色的想法?

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>
<?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>
您可能希望查看这些链接以了解有关排序和颜色的更多信息


假设你说的“排序”是指按时间顺序排列,那么我建议为
teaching
元素创建一个模板,使用
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>

标题
教授
白天
背景色:红色;
<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>