【问题标题】:Custom sort in xml file and coloring table rowsxml文件和着色表行中的自定义排序
【发布时间】:2021-02-24 15:03:06
【问题描述】:

大家好,我有这个 XML 文件:

<?xml version="1.0"?> 
<?xml-stylesheet type="text/xsl" href="10_schedule.xsl"?>
  <Schedule> 
    <Lesson>
            <Title>Artificial Intelligence</Title>
            <Lecture Classroom="BA">
                <Day>Wednesday</Day>
                <Time>09-11</Time>
            </Lecture>
            <Lecture Classroom="BA">
                <Day>Thursday</Day>
                <Time>09-11</Time>
            </Lecture>
            <Professor>Peter</Professor>
    </Lesson>
    <Lesson>
            <Title>Constraint Satisfaction Problems</Title>
            <Lecture Classroom="B3">
                <Day>Monday</Day>
                <Time>19-21</Time>
            </Lecture>
    </Lesson>       
    <Lesson>
            <Title>Knowledge Representation in Web</Title>
            <Lecture Classroom="P200">
                <Day>Friday</Day>
                <Time>15-17</Time>
            </Lecture>
            <Professor>David</Professor>
    </Lesson>
    

还有这个 XSL 文件:

  enter code here<?xml version="1.0" encoding="ISO-8859-1"?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
   <html>
    <body>
        <h2>Schedule</h2>
            <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 data-type="number" select="string-length(substringbefore('|Monday|Tuesday|Wednesday|Thursday|Friday|',@Day))"/>
                    <tr>
                        <td>
                            <xsl:value-of select="../Title" />
                        </td>
                        <td>
                            <xsl:value-of select="../Professor"/>
                        </td>
                        <xsl:choose>
                        <xsl:when test="Day = 'Monday' " >
                                <td bgcolor="#7d78fc">
                                    <xsl:value-of select="Day"/>
                                    <xsl:value-of select="Title"/>
                                    <xsl:value-of select="Professor"/>
                                </td>
                            </xsl:when> 
                            <xsl:when test="Day = 'Tuesday' ">
                                <td bgcolor="#f6fc78">
                                    <xsl:value-of select="Day"/>
                                    <xsl:value-of select="Title"/>
                                    <xsl:value-of select="Professor"/>
                                    <xsl:apply-templates select="Title"/>
                                </td>
                            </xsl:when>                                 
                            <xsl:when test="Day = 'Wednesday' ">
                                <td bgcolor="#990033">
                                    <xsl:value-of select="Day"/>
                                    <xsl:value-of select="Title"/>
                                    <xsl:value-of select="Professor"/>
                                </td>
                            </xsl:when>
                            <xsl:when test="Day = 'Friday' ">
                                <td bgcolor="#00ccff">
                                    <xsl:value-of select="Day"/>
                                    <xsl:value-of select="Title"/>
                                    <xsl:value-of select="Professor"/>
                                </td>
                            </xsl:when>
                            <xsl:when test="Day = 'Thursday' ">
                                <td bgcolor="#ccccff">
                                    <xsl:value-of select="Day"/>
                                    <xsl:value-of select="Title"/>
                                    <xsl:value-of select="Professor"/>
                                </td>
                            </xsl:when>
                            <xsl:otherwise>
                                <td bgcolor="#fcc678">
                                    <xsl:value-of select="Day"/>
                                    <xsl:value-of select="Title"/>
                                    <xsl:value-of select="Professor"/>
                                </td>
                            </xsl:otherwise>
                        </xsl:choose>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
  

我正在处理这些问题:首先,我想对一周中的日子进行排序,从星期一到星期五。我用完全不同的方式尝试了它,但似乎没有任何效果。 之后,我想简单地将元素做成表格形式,但我希望它们按天排序并按组着色。我不能为组着色(日、教授、标题、相同颜色),但只能为日列着色。 有什么建议吗?

【问题讨论】:

  • 请一次问一个问题。

标签: xml sorting xslt


【解决方案1】:

WRT 排序,你的代码:

<xsl:sort data-type="number" select="string-length(substringbefore('|Monday|Tuesday|Wednesday|Thursday|Friday|',@Day))"/> 

有两个问题:

  1. 没有substringbefore()函数;
  2. Day 是一个元素,而不是一个属性

试试吧:

<xsl:sort data-type="number" select="string-length(substring-before('|Monday|Tuesday|Wednesday|Thursday|Friday|', Day))"/>

(注意| 分隔符在这里是多余的。)


您应该单独询问颜色。不过,我要说的是:没有必要为一周中的每一天重复相同的代码。

【讨论】:

  • 我是 XML 新手,你真的帮助了我!谢谢你,谢谢你,谢谢你。你摇滚!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
  • 2021-01-26
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 2012-10-12
相关资源
最近更新 更多