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>

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

您正在执行:

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

这试图对每个diaryentrys进行排序,但每个diary只有一个entry。此外,您已经处于entry的上下文中,并且表达式entry/@date从该上下文中不选择任何内容,因为entry不是其自身的子级。

尝试:

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

您使用一个xsl:for-each太多了
只需将XSLT-1.0代码缩减为…

<?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:sort select="entry/@date"/>
<xsl:sort select="entry/location"/>
<tr>
<td>
<xsl:value-of select="entry/@date"/>
</td>
<td>
<xsl:value-of select="entry/location"/>
</td>
<td>
<xsl:value-of select="entry/description"/>
</td>
<td>
<img border="1" width="100px" height="100px">
<xsl:attribute name="src">
<xsl:value-of select="entry/img"/>
</xsl:attribute>
</img>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

并且您的输出将按CCD_ 9和CCD_。

@Cahirsu-您可以尝试这种方式进行多重排序:

<xsl:template match="/">
<xsl:variable name="pr">
**<xsl:perform-sort select="entry">
<xsl:sort select="@date" data-type="number" order="descending"/>
<xsl:sort select="location" order="ascending"/>                
</xsl:perform-sort>**
</xsl:variable>
</xsl:template>

并应用变量$pr

最新更新