this is a follow up to this post. Basically I need to get the position()
of items within a filtered and sorted collection. Any advice on how to proceed with this?
Thanks!
XML:
<?xml version="1.0" encoding="utf-8"?>
<news>
<newsItem id="1">
<title>Title 1</title>
</newsItem>
<newsItem id="2">
<title>Title 2</title>
</newsItem>
<newsItem id="3">
<title></title>
</newsItem>
<newsItem id="4">
<title></title>
</newsItem>
<newsItem id="5">
<title>Title 5</title>
</newsItem>
</news>
XSL:
<xsl:template match="/">
<ol>
<xsl:apply-templates select="/news/newsItem [string(title)][3 > position()]">
<xsl:sort order="descending" data-type="number" select="@id"/>
</xsl:apply-templates>
</ol>
</xsl:template>
<xsl:template match="newsItem">
<li开发者_开发技巧>
<xsl:value-of select="title"/>
</li>
</xsl:template>
<xsl:template match="*" />
Desired Result:
- Title 5
- Title 2
Actual Result:
- Title 2
- Title 1
You should really look at my answer about context node list.
This stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<ol>
<xsl:apply-templates select="/news/newsItem [string(title)]">
<xsl:sort order="descending" data-type="number" select="@id"/>
</xsl:apply-templates>
</ol>
</xsl:template>
<xsl:template match="newsItem">
<xsl:if test="3 > position()">
<li>
<xsl:value-of select="title"/>
</li>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:
<ol>
<li>Title 5</li>
<li>Title 2</li>
</ol>
精彩评论