0

I have the following 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>

The rss/channel/item can be of any count(in the current case it's count is 2). I need to display the Titles as a Marquee one after the other as follows

ISRO spectrum deal under review: Centre, Response from Devas,....,....

How can I accomplish this in XSLT? kindly advice

Thanks

2 Answers 2

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>

Result against your sample:

<div id="marquee">ISRO spectrum deal under review: Centre, Response from Devas</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Cool....this works thanks a ton for your help. BTW, can you suggest any good books for XSL.
@padLing, then you should accept the answer (just click on the checkmark to the top left of my answer).
@padLing, for books check this question: stackoverflow.com/questions/339930/…
1

In addition to @Flack correct answer, in XSLT 2.0 xsl:value-of instruction preserves sequence. So, this stylesheet:

<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>

Also outputs:

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

1 Comment

+1 - I didn't know about separator despite having implemented it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.