我有一個由音樂應用程式制作的歌曲串列,我想在網站中投影反向串列。例如我有以下清單:
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>
如何反轉串列,以便我可以將最后播放的歌曲放在串列頂部,然后播放較早的歌曲?
我是這個社區的新手,盡管我在網站上進行了研究,但我沒有找到解決以下問題的方法。
uj5u.com熱心網友回復:
看看這是否適合你:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Event">
<html>
<body>
<ul>
<xsl:for-each select="Song">
<xsl:sort select="position()" data-type="number" order="descending"/>
<li>
<xsl:value-of select="Artist/@name"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="@title"/>
<xsl:text> (</xsl:text>
<xsl:value-of select="Info/@StartTime"/>
<xsl:text>)</xsl:text>
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
uj5u.com熱心網友回復:
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不支持。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359608.html
