2011-02-09 77 views
0

我有下面的XML循环在XSLT

<?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 /信道/项目可以是任何计数(在当前情况下,它计数为2)。我需要的其他后显示的标题为选取框一个审查如下

ISRO频谱交易:中心,从天王响应,....,....

我怎样才能在实现这一目标XSLT?好心指点

感谢

回答

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

酷....这部作品由于一吨的帮助。顺便说一句,你能为XSL推荐任何好书吗? – padLing 2011-02-09 09:00:58

+0

@padLing,那么你应该接受答案(只需点击我答案左上角的复选标记)。 – Flack 2011-02-09 09:48:47

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>