xml - XSL 列表反向
问题描述
我有一个音乐应用程序制作的歌曲列表,我想将反向列表投影到网站中。例如,我有清单:
Deep Zone Vs Balthazar - Dj Take Me Away (In The Mix) (12:24:45)
Tom Boxer Feat Antonia - Morena (12:27:43)
Alexandra Stan - Lemonade (12:30:16)
Flo Rida feat. Timbaland - Elevator (12:33:43)
创建列表的 XML 文件是:
<?xml version="1.0" encoding="utf-8"?>
<Event status="happened">
<Song title="Dj Take Me Away (In The Mix)">
<Artist name="Deep Zone Vs Balthazar" ID="335712"></Artist>
<Info StartTime="12:24:45" JazlerID="12619" PlayListerID="" />
</Song>
<Song title="Morena">
<Artist name="Tom Boxer Feat Antonia" ID="335910"></Artist>
<Info StartTime="12:27:43" JazlerID="13079" PlayListerID="" />
</Song>
<Song title="Lemonade">
<Artist name="Alexandra Stan" ID="335773"></Artist>
<Info StartTime="12:30:16" JazlerID="12693" PlayListerID="" />
</Song>
<Song title="Elevator">
<Artist name="Flo Rida feat. Timbaland" ID="335818"></Artist>
<Info StartTime="12:33:43" JazlerID="12837" PlayListerID="" />
</Song>
</Event>
XSL 文件是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Event">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Artist">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Song">
<html>
<body>
<ul>
<li style="margin-bottom: -10px; margin-left: -30px; list-style: circle;">
<xsl:for-each select="Artist">
<xsl:value-of select="@name"/>
</xsl:for-each>
-
<xsl:value-of select="@title"/>
<span>
(<xsl:for-each select="Info">
<xsl:value-of select="@StartTime"/>
</xsl:for-each>)
</span><br />
</li>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如何反转列表,以便我可以将最后播放的歌曲放在列表顶部,然后播放较早的歌曲?
我是这个社区的新手,尽管我在网站上进行了研究,但我没有找到以下问题的解决方案。
解决方案
XSLT/XPath 3(甚至是 2,不记得了)有一个reverse
功能,所以select="reverse(Artist)"
在那个版本中就足够了。
否则使用例如
<xsl:for-each select="Artist">
<xsl:sort select="position()" order="descending"/>
...
</xsl:for-each>
根据您和进一步的评论,您使用的原始代码for-each select="Artist"
似乎根本没有处理和输出艺术家的“列表”,所以当然,如果您处理单个Artist
元素,也reverse
不会以相反的position()
顺序排序会改变任何东西。
我想你在更高层处理Song
这样使用的元素,<xsl:for-each select="reverse(Song)">
或者<xsl:apply-templates select="reverse(Song)"/>
在 XSLT 3中,<xsl:for-each select="Song"><xsl:sort select="position()" order="descending"/>...</xsl:for-each>
或者在不支持<xsl:apply-templates select="Song"><xsl:sort select="position()" order="descending"/></xsl:apply-templates>
的版本中。reverse
推荐阅读
- rust - 移动关闭的特征?
- python - 如何使用 FastAPI 返回 HTMLResponse
- php - 我可以从 PHP if{} 内跳转到 else{}
- python - 在 numpy 数组中查找一些缺失点
- sql - Oracle SQL - 从实体开始日期和结束日期开始每天计算实体
- scala - 如何在 IntelliJ IDEA 的 sbt shell 中扩展控制台中的代码?
- css - CSS:在考虑间隙空间时如何让网格尊重边距?
- swiftui - 为什么我在用户登录时看不到我的动画?
- javascript - 如何索引不和谐频道?
- azure-aks - 在 docker 容器内调用时,Azure AKS 获取凭据不起作用