首页 > 解决方案 > XML XSL xsl:sort 不对文件进行排序

问题描述

我正在尝试按日期或位置对这个简单的表格进行排序,但似乎没有任何效果。似乎根本没有应用排序。我已经在 w3c 和论坛等网站上搜索了一些提示,但我无法使其工作。

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet href="travelDiaries.xsl" type="text/xsl"?>
<diaries>
    <diary name='Wojciech'>
          <entry date='2020/06/12' title='Poland'>
            <location>Poland</location>
            <description>Trip to see the, family and friends in a home town</description>
            <img></img>
         </entry>
    </diary>

    <diary name='Karolina'>
        <entry date='2018/04/12' title='Poland'>
            <location>Poland</location>
            <description>Trip for site visiting, visiting a Capital city of Poland - Warsaw </description>
            <img></img>
        </entry>
    </diary>

     <diary name='Kuba'>
          <entry date='2019/03/02' title='Czech republic'>
            <location>Czech republic</location>
            <description>Visiting the Old Praque with friends, seeing most popular sites</description>
            <img></img>
         </entry>
    </diary>

     <diary name='Kevin'>
          <entry date='2020/11/08' title='Usa'>
            <location>Usa</location>
            <description>Traveling around different states, meeting people and learning about the culture</description>
            <img></img>
         </entry>
    </diary>
</diaries>

这是 XSL 代码:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/diaries">
        <html>
            <body>
                <table border="5">
                    <tr bgcolor="lawngreen">
                        <th>Date</th>
                        <th>Location</th>
                        <th>Description</th>
                        <th>Image</th>
                    </tr>
                <xsl:for-each select="diary">
                    <xsl:for-each select="entry">
                          <xsl:sort select="entry/@date"/>
                        <tr>
                            <td>
                                <xsl:value-of select="@date"/>
                            </td>
                            <td>
                                <xsl:value-of select="location"/>
                            </td>
                            <td>
                                <xsl:value-of select="description"/>
                            </td>
                            <td>
                                <img border="1" width="100px" height="100px">
                                    <xsl:attribute name="src">
                                <xsl:value-of select="img"/>
                                    </xsl:attribute>
                                </img>
                            </td>
                        </tr>
                    </xsl:for-each>
                </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

我真的觉得被这个困住了。

标签: htmlxmlxslt

解决方案


你正在做:

<xsl:for-each select="diary">
    <xsl:for-each select="entry">
        <xsl:sort select="entry/@date"/>

这试图对entry每个的 s进行排序diary- 但每个diary只有一个entry。此外,您已经在 的上下文中entry,并且从这个上下文中,表达式entry/@date什么也不选择,因为entry它不是它自己的孩子。

请尝试:

<xsl:for-each select="diary/entry">
    <xsl:sort select="@date"/>

推荐阅读