xslt matching first x items of filtered result set

一个人想着一个人 提交于 2019-12-06 01:02:13

This one's actually simpler than you might think. Do:

 <xsl:template match="newsItem[string(title)][position() &lt; 4]">

And drop the [string(title)] predicate from your <xsl:apply-templates select.

Like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <ol>
            <xsl:apply-templates select="/news/newsItem" />
        </ol>
    </xsl:template>

    <xsl:template match="newsItem[string(title)][position() &lt; 4]">
        <li><xsl:value-of select="position()" />
            <xsl:value-of select="title"/>
        </li>
    </xsl:template>

    <xsl:template match="*" />
</xsl:stylesheet>

What you're effectively doing here is applying a second filter ([position() &lt; 4]) after your [string(title)] filter, which results in the position() being applied to the filtered list.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!