带有RSS源的XSLT

时间:2015-03-11 22:15:08

标签: html xml xslt rss

我有一个定期更改的RSS源(相同的格式,不同的数据),我需要对其应用转换,以使用标记从中获取项目列表。问题是,我永远不知道带有正确标签的项目的位置。示例:如果我从这里生成一个与“理论”相关的页面的旋转木马,我需要从中提取“项目” RSS中具有相关标签的项目列表,然后将类“active”附加到具有“newsitem row item”类的元素。

我意识到我可以使用jQuery轻松完成这项工作 - >

$(".newsitem.row.item").first().attr("class","newsitem row item active");

但是,如果可能的话,我想保存一个步骤,并使用活动类生成HTML。

<rss xmlns:atom=" ... " version="2.0">
  <channel>
    <title>Latest News</title>
      <link>http://...</link>
      <description>Latest News</description>
    <atom:link href="..." rel="self"/>
    <language>en</language>
    <copyright>Copyright (c) 2015</copyright>
    <lastBuildDate>Sat, 25 Apr 2015 23:39:40 -0500</lastBuildDate>
    <item>
        <title>TITLE FOR FIRST ITEM</title>
        <link>LINK FOR FIRST ITEM</link>
        <description>DESCRIPTION</description>
        <pubDate>Sat, 25 Apr 2015 23:39:40 -0500</pubDate>
        <guid>GUID</guid>
        <category>gallery</category>
        <category>events</category> 
    <item>
        <title>TITLE FOR NEXT ITEM</title>
        <link>LINK FOR NEXT ITEM</link>
        <description>DESCRIPTION ...</description>
        <pubDate>Tue, 10 Mar 2015 20:59:12 -0500</pubDate>
        <guid>GUID ...</guid>
        <category>architecture</category>
        <category>class acts</category>
        <category>competitions</category>
        <category>feature</category>
        <category>global college</category>
        <category>graduate work</category>
        <category>health systems &amp; design</category>
        <category>honors</category>
        <category>rss</category>
        <category>wellness</category>
    </item>
    ...
    <item>
        <title>TITLE FOR THE NTH ITEM</title>
        <link>LINK FOR THE NTH ITEM</link>
        <description>DESCRIPTION ...</description>
        <pubDate>Tue, 10 Mar 2015 20:52:45 -0500</pubDate>
        <guid>GUID ...</guid>
        <category>applied creativity</category>
        <category>gallery</category>
        <category>coa gallery</category>
        <category>graduate work</category>
        <category>research</category>
        <category>rss</category>
        <category>technology</category>
        <category>theory-philosophy</category>
        <category>video</category>
        <category>visualization</category>
    </item>
 </channel>
</rss>

我的代码是:

 <xsl:stylesheet xmlns:xsl="..." version="1.0">

<xsl:template match="/">
<div class="row">
    <div class="col-md-12 banner_image vanish">
        <div class="carousel newsitems slide" data-carousel-delay="8000" id="banner-latest-85695">
            <div class="carousel-inner">
                <xsl:apply-templates select="//item"/>
            </div>
            <a class="carousel-control left" data-slide="next" href="#banner-latest-85695" title="Previous News Item"></a>
            <a class="carousel-control right" data-slide="next" href="#banner-latest-85695" title="Next News Item"></a>
        </div>
    </div>
</div>
</xsl:template>

<xsl:template match="item|text()">
        <xsl:if test="category[contains(text(),'theory')]">
        <div class="newsitem row item">
            <xsl:if test="position() = ?"> <!-- what position test do I use? -->
                <xsl:attribute name="class">newsitem row item active</xsl:attribute>
            </xsl:if>
            <div class="image span">
                <img>
                    <xsl:attribute name="src">
                        <xsl:value-of select="carousel"/>
                    </xsl:attribute>
                    <xsl:attribute name="alt">
                        <xsl:value-of select="title"/>
                    </xsl:attribute>
                </img>
            </div>
            <div class="details span">
                <h2>
                    <a>
                        <xsl:attribute name="href">
                            <xsl:value-of select="link"/>
                        </xsl:attribute>
                        <xsl:value-of select="title"/>  
                    </a>
                </h2>
                <div class="publication-date">
                    <span>posted</span>
                    <xsl:value-of select="pubDate"/>
                </div>
                <div class="description">
                    <xsl:value-of select="description"/>
                </div>
            </div>
        </div>
    </xsl:if>
</xsl:template>

如果rss Feed中的第一项包含我要查找的标记,则此方法有效。我在这里俯瞰什么?

**编辑澄清 链接替换为“......”

1 个答案:

答案 0 :(得分:0)

你的问题并不完全清楚。如果您想通过&#34;标签&#34;选择项目(或项目)。它包含,你应该做的事情:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <div>
        <xsl:apply-templates select="rss/channel/item[tag2]"/>
     </div>
</xsl:template>

<xsl:template match="item">
    <div>
        <span>
            <xsl:value-of select="value"/>
        </span>
     </div>
</xsl:template>

</xsl:stylesheet>

在这里,我们正在寻找带有名为tag2的子元素的item / s。将其应用于以下测试输入

<rss>
   <channel>
      <item>
         <tag1/>
         <value>100</value>
      </item>
      <item>
         <tag2/>
         <value>200</value>
      </item>
      <item>
         <tag2/>
         <value>201</value>
      </item>
      <item>
         <tag3/>
         <value>300</value>
      </item>
   </channel>
</rss>

生成结果

<?xml version="1.0" encoding="UTF-8"?>
<div>
   <div>
      <span>200</span>
   </div>
   <div>
      <span>201</span>
   </div>
</div>