html - 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"/>
这试图对entry
每个的 s进行排序diary
- 但每个diary
只有一个entry
。此外,您已经在 的上下文中entry
,并且从这个上下文中,表达式entry/@date
什么也不选择,因为entry
它不是它自己的孩子。
请尝试:
<xsl:for-each select="diary/entry">
<xsl:sort select="@date"/>
推荐阅读
- java - Application.yml 值自动转换为小写
- javascript - react-export-excel 出口太慢
- java - 如何使用Java实现华为分块文件上传
- c# - 使用 HttpClient.GetAsync 在浏览器中打开链接
- java - 带有可选列的 Bean 到 csv
- image - tf.pad 函数中有四个元组是什么意思
- angular - 服务不打印 console.log
- speech-recognition - Google Speech To Text 是否符合 RFC 6787
- python - 查找数据列表的平均值,不包括 Python 中的某些条目
- node.js - 如何在 pg-promise 的多行插入中插入带有 nextval 函数的整数