XSLT文件
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template match="/">
<xsl:call-template name="content">
</xsl:call-template>
</xsl:template>
<xsl:template name="content">
<movies>
<xsl:for-each select="/movies/movie">
<xsl:for-each select="movie[starts-with(name,'Ka')]">
<xsl:apply-templates select="name" />
<xsl:apply-templates select="vdate" />
</xsl:for-each>
<xsl:for-each select="/movies/movie">
<xsl:sort select="date" data-type="number" order="descending"/>
<xsl:if test="position() <= 50">
<xsl:value-of select="vdate"/>
</xsl:if>
</xsl:for-each>
<xsl:sort select="date" data-type="number" order="'.($this->_boolAscending?'ascending':'descending').'"/>
<movie>
<vdate><xsl:value-of select="vdate"/></vdate>
<no><xsl:value-of select="no"/></no>
<name><xsl:value-of select="name"/></name>
<link1><xsl:value-of select="link1"/></link1>
<link2><xsl:value-of select="link2"/></link2>
<link3><xsl:value-of select="link3"/></link3>
<year><xsl:value-of select="year"/></year>
</movie>
</xsl:for-each>
</movies>
</xsl:template>
</xsl:stylesheet>
XML文件
<?xml version="1.0"?>
<movies>
<movie><no>2013001</no>
<date>20131213</date>
<vdate>13-12-2013</vdate>
<name>Kalyana Kanavugal</name>
<mlink1>http://www.moviebunk.com/?t7f4vm15zza5xv8</mlink1>
<mlink2>http://www.moviebunk.com/?mvz1tjks2z571fj</mlink2>
<mlink3>http://www.moviebunk.com/?k64p6yjfl5b24z4</mlink3>
<link1><![CDATA[<a href="index2.php?q=K176AZ0DN8LW930D0ZYJ8T74P">320KBPS</a>]]></link1>
<link2><![CDATA[<a href="index3.php?q=5245C918Z99SOHBRU6298G13M">256KBPS</a>]]></link2>
<link3><![CDATA[<a href="index4.php?q=3FZ0MJ8S65749XBSI0670ARH2">128KBPS</a>]]></link3>
<year>2011</year>
</movie>
<movie><no>2013002</no>
<date>20131110</date>
<vdate>10-11-2013</vdate>
<name>Kandaudan</name>
<mlink1>http://www.moviebunk.com/?d2411v82s08x1b2</mlink1>
<mlink2>http://www.moviebunk.com/?gd0ume8bdo6lazg</mlink2>
<mlink3>http://www.moviebunk.com/?gyry7qb81kvnind</mlink3>
<link1><![CDATA[<a href="index2.php?q=6A29SM5TI180BJD50E97VTMQM">320KBPS</a>]]></link1>
<link2><![CDATA[<a href="index3.php?q=1MGEFRH6H675H6599G0APR0KZ">256KBPS</a>]]></link2>
<link3><![CDATA[<a href="index4.php?q=83GDW3645XHF296HY3XUEO96A">128KBPS</a>]]></link3>
<year>2011</year>
</movie>
<movie><no>2013003</no>
<date>20121019</date>
<vdate>19-10-2012</vdate>
<name>Koyambedu Perundhu Nilayam</name>
<mlink1>http://www.moviebunk.com/?bdb6a76twx4kfnb</mlink1>
<mlink2>http://www.moviebunk.com/?n56o5xuqc7euqq6</mlink2>
<mlink3>http://www.moviebunk.com/?wksvaa965pp6a1e</mlink3>
<link1><![CDATA[<a href="index2.php?q=5R9B6916IN5H5R6G4O37G3XB3">320KBPS</a>]]></link1>
<link2><![CDATA[<a href="index3.php?q=TM7ZFY8619W371P972P92R51F">256KBPS</a>]]></link2>
<link3><![CDATA[<a href="index4.php?q=HQ6YHH751Y7M0H8ZK60V773U3">128KBPS</a>]]></link3>
<year>2011</year>
</movie>
</movies>
我是XSLT编程的新手。请建议我对XML文件进行排序的方法。 排序有问题如下:名称以'K'字母开头,然后按降序排序。仅显示前50个值。我尝试在没有“Ka”开头的情况下进行排序并限制结果 - 这很好。
像这样的HTML输出
Movie Information
=================
Kalyana Kanavugal
2011
Download-320KBPS
Download-256KBPS
Download-128KBPS
Kandaudan
2011
Download-320KBPS
Download-256KBPS
Download-128KBPS
Koyambedu Perundhu Nilayam
2011
Download-320KBPS
Download-256KBPS
Download-128KBPS
答案 0 :(得分:1)
名称以“K”字母开头,然后按降序排序[按日期?] 订购。仅显示前50个值。
如果我理解正确,那么这就足够了:
<?xml version="1.0" encoding="UTF-8"?>
<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"
cdata-section-elements="link1 link2 link3"/>
<xsl:template match="/movies">
<xsl:copy>
<xsl:apply-templates select="movie[starts-with(name, 'K')]">
<xsl:sort select="date" data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="movie">
<xsl:if test="position() <= 50">
<xsl:copy>
<xsl:copy-of select="vdate"/>
<xsl:copy-of select="no"/>
<xsl:copy-of select="name"/>
<xsl:copy-of select="link1"/>
<xsl:copy-of select="link2"/>
<xsl:copy-of select="link3"/>
<xsl:copy-of select="year"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<强>增加:强>
对于HTML输出,请尝试类似:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/movies">
<html>
<body>
<h1>Movie Information</h1>
<xsl:apply-templates select="movie[starts-with(name, 'K')]">
<xsl:sort select="date" data-type="number" order="descending"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="movie">
<xsl:if test="position() <= 50">
<h4>
<xsl:value-of select="name"/><br/>
<xsl:value-of select="year"/>
</h4>
<p>
Download - <xsl:value-of select="link1" disable-output-escaping="yes"/><br/>
Download - <xsl:value-of select="link2" disable-output-escaping="yes"/><br/>
Download - <xsl:value-of select="link3" disable-output-escaping="yes"/>
</p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>