在XSLT中循环

时间:2011-02-09 07:51:27

标签: xslt

我有以下XML

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="sample.xsl" type="text/xsl"?>
<rss version="2.0"
 xmlns:atom="http://www.w3.org/2005/Atom"
 xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005"
 xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel
     xmlns:cfi="http://www.microsoft.com/schemas/rss/core/2005/internal">
        <title cf:type="text">The Hindu - Front Page</title>
        <link>http://www.hindu.com/</link>
        <description cf:type="text">The Internet edition of The Hindu,
            India's national newspaper</description>
        <image>
            <url>http://www.hindu.com/hindu/hindux.gif</url>
            <title>hindu.com</title>
            <link>http://www.hindu.com/</link>
        </image>
        <item>
            <title cf:type="text"
             xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005"
             >ISRO spectrum deal under review: Centre</title>
        </item>
        <item>
            <title cf:type="text"
             xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005"
             >Response from Devas</title>
        </item>
    </channel>
</rss>

rss / channel / item 可以是任何计数(在当前情况下,它的计数是2)。我需要将标题一个接一个地显示为Marquee,如下所示

正在审查ISRO频谱协议:中心,Devas回应,......,....

如何在XSLT中完成此操作?善意的建议

由于

2 个答案:

答案 0 :(得分:2)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:cfi="http://www.microsoft.com/schemas/rss/core/2005/internal"
    xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    exclude-result-prefixes="cfi cf dc">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/*">
        <div id="marquee">
            <xsl:apply-templates select="channel/item/title"/>
        </div>
    </xsl:template>

    <xsl:template match="title">
        <xsl:value-of select="."/>
        <xsl:if test="not(position() = last())">, </xsl:if>
    </xsl:template>

</xsl:stylesheet>

针对您的样本的结果:

<div id="marquee">ISRO spectrum deal under review: Centre, Response from Devas</div>

答案 1 :(得分:1)

除了@Flack正确答案,在XSLT 2.0 xsl:value-of指令中保留序列。那么,这个样式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <div id="marquee">
            <xsl:value-of select="rss/channel/item/title"
                          separator=", "/>
        </div>
    </xsl:template>
</xsl:stylesheet>

同时输出:

<div id="marquee"
 >ISRO spectrum deal under review: Centre, Response from Devas</div>