开发者

xslt: matching position of filtered and sorted result set

开发者 https://www.devze.com 2023-02-13 00:16 出处:网络
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?

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:

  1. Title 5
  2. Title 2

Actual Result:

  1. Title 2
  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>
0

精彩评论

暂无评论...
验证码 换一张
取 消