xsl场景,当每个循环中的节点不存在时

时间:2014-08-14 15:03:47

标签: xml xslt

我有一个由http://feeds.contenthub.aol.com/syndication/2.0/feed/53dfbe8362619表示的xml。我也发布了一个xml的补丁

<item>
<title>
<![CDATA[ Jon Lester Uses Full-Page Ad To Thank Red Sox Fans ]]>
</title>
<description type="html">
<![CDATA[
<p>Jon Lester made a classy move by taking a full-age ad in the Sunday edition of the Boston Globe...</p>
]]>
</description>
<link>
http://www.huffingtonpost.com/2014/08/03/jon-lester-full-page-ad-red-sox-fans_n_5645882.html
</link>
<source_id>qkjDte</source_id>
<guid isPermalink="false">0d85d061e4436ffaeeeafa9a86c3a253</guid>
<dc:creator>Michael Klopman</dc:creator>
<dc:publisher>Huff. Post: United States - Sports</dc:publisher>
<dc:rightsHolder>Huff. Post: United States - Sports</dc:rightsHolder>
<pubDate>Sun, 03 Aug 2014 19:58:04 +0000</pubDate>
<ingested>1407096147696297537</ingested>
<modified>2014-08-05 14:58:17</modified>
<category>
<![CDATA[ Jon Lester full page ad ]]>
</category>
<category>
<![CDATA[ brew-pub ]]>
</category>
<category>
<![CDATA[ Red Sox Jon Lester ]]>
</category>
<category>
<![CDATA[ mlb ]]>
</category>
<category>
<![CDATA[ major league baseball ]]>
</category>
<category>
<![CDATA[ Jon LEster full-page ad ]]>
</category>
<category>
<![CDATA[ Jon Lester Boston Globe ]]>
</category>
<category>
<![CDATA[ Oakland A's ]]>
</category>
<category>
<![CDATA[ Red Sox ]]>
</category>
<category>
<![CDATA[ oakland Athletics ]]>
</category>
<category>
<![CDATA[ Boston Red Sox ]]>
</category>
<media:content url="http://i.huffpost.com/gen/1945400/original.jpg" width="" height="" medium="image">
<media:keywords>headline</media:keywords>
<media:media_html>
<![CDATA[ <HH--PHOTO--JON-LESTER--1945400--HH> ]]>
</media:media_html>
<dc:identifier>
<![CDATA[ ]]>
</dc:identifier>
<media:credit>
<![CDATA[ ASSOCIATED PRESS ]]>
</media:credit>
<media:description>
<![CDATA[
Boston Red Sox starting pitcher Jon Lester heads to the bull pen before a baseball game against the Kansas City Royals in Boston, Sunday, July 20, 2014. (AP Photo/Michael Dwyer)
]]>
</media:description>
<media:title>
<![CDATA[ ]]>
</media:title>
</media:content>
</item>
<item>
<title>
<![CDATA[ 10 Home Runs of the Century ]]>
</title>
<description type="html">
<![CDATA[ %Slideshow-209585% ]]>
</description>
<link>
http://www.mandatory.com/2014/07/24/10-home-runs-of-the-century/
</link>
<source_id>blEPZi</source_id>
<guid isPermalink="false">20936381</guid>
<dc:creator>Joshua Crouthamel</dc:creator>
<dc:publisher>Mandatory</dc:publisher>
<dc:rightsHolder>Mandatory</dc:rightsHolder>
<pubDate>Thu, 24 Jul 2014 15:52:00 +0000</pubDate>
<ingested>1406220498372620179</ingested>
<modified>2014-07-24 16:45:40</modified>
<category>
<![CDATA[ co-gallery ]]>
</category>
<category>
<![CDATA[ Sports ]]>
</category>
<ss:slideshow>
<ss:media_html>
<![CDATA[ %slideshow-209585% ]]>
</ss:media_html>
<media:title>
<![CDATA[ 10 Home Runs of the Century ]]>
</media:title>
<dc:identifier>
<![CDATA[ 209585 ]]>
</dc:identifier>
<dc:created>2014-07-24 15:52:00</dc:created>
<media:content url="http://www.blogcdn.com/slideshows/images/slides/279/483/8/S2794838/slug/l/1-2.jpg" width="" height="" medium="image">
<dc:identifier>
<![CDATA[ 2794838 ]]>
</dc:identifier>
<media:credit>
<![CDATA[ New York Giants ]]>
</media:credit>
<media:description>
<![CDATA[
<p>The home run is the best part of baseball. But not all home runs are created equal. Here are the 10 most amazing home runs of the century. (Prepare to argue in the comments.)</p> <p><strong>Bobby Thomson&rsquo;s &ldquo;Shot Heard &lsquo;Round the World&rdquo;</strong><br /> <strong>October 3, 1951:</strong> Things weren&rsquo;t looking good for the New York Giants coming into the ninth inning of the National League pennant game &mdash; they were down 4-1 to the Brooklyn Dodgers, and the game&rsquo;s winner would take home the title. Enter Bobby Thomson, who hit a game-ending three-run homer &hellip; and the New York Polo Grounds went wild.&nbsp;</p>
]]>
</media:description>
<media:title>
<![CDATA[ Bobby Thomson’s “Shot Heard ‘Round the World” ]]>
</media:title>
</media:content>
<media:content url="http://www.blogcdn.com/slideshows/images/slides/279/484/0/S2794840/slug/l/2-2.jpg" width="" height="" medium="image">
<dc:identifier>
<![CDATA[ 2794840 ]]>
</dc:identifier>
<media:credit>
<![CDATA[ Los Angeles Dodgers ]]>
</media:credit>
<media:description>
<![CDATA[
<p><strong>Kirk Gibson&rsquo;s World Series Home Run</strong><br /> <strong>October 15, 1988:</strong> Talk about an incredible underdog story. Gibson was nursing two knee injuries as well as a virus when he was called in to pinch hit for the Los Angeles Dodgers at the bottom of the ninth, Dodgers down by one with two outs on the board. His two-run, walk-off homer, followed by subsequent fist-pumping round of the bases, was one of the greatest moments in LA sporting history (and won the game).</p>
]]>

所以我的xsl遍历每个项目节点以获得<title>, <description>, <media:content/@url> and <link>节点。但是我想在xslt中解决2个极端情况。如果您查看我所关注的节点中的Feed http://feeds.contenthub.aol.com/syndication/2.0/feed/53dfbe8362619:当 节点不存在时,每个节目都会出现,因此我无法访问它 @url < / strong>属性。因此,当 / @ url 属性不存在时, 节点对于每个节点都不存在,但它们适用于 节点不存在的节点当下。我想写一个执行此操作的xsl

if(!media:content node) { 获取ss:slideshow / media:content [1] / @ url //第一个媒体:内容节点及其url属性 }

第二个角落是某个节点有%幻灯片。 如果普通节点包含“%slideshow ”作为内容,我想获得 ss:slideshow / media:description 。我为此编写了xsl,但它没有给我媒体:ss:slideshow中的描述。 所以我想要的是这样的 if(描述节点包含'%幻灯片放映) { 使用ss;幻灯片/媒体:说明 }  当我尝试访问ss:slideshow节点时,它打破了失败的XSL转换。

我的XSL如下

 <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
        xmlns:media="http://search.yahoo.com/mrss/">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="/">
            <rss xmlns:georss="http://www.georss.org/georss" xmlns:twitter="http://api.twitter.com" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
                <channel>
                    <author>
                        <xsl:text>Content-Feed-Pics</xsl:text>
                    </author>
                    <xsl:for-each select="rss/channel/item[position() &lt; 30]">
                        <item>
                            <title><xsl:value-of select="title"/></title>
                            <xsl:call-template name="deleteEllipses">
                                <xsl:with-param name="nodeData" select="description"></xsl:with-param>
                            </xsl:call-template>
                            <!--author><xsl:value-of select="dc:rss/dc:channel/dc:item/dc:creator"/></author-->
                           <!--icon>
                           <xsl:value-of select="concat(entities/media//media_url,':thumb')"/>                    
                           </icon-->


                            <link><xsl:value-of select="link"/></link>
                                           <!--description><xsl:value-of select="media:content[1]/media:description"/></description-->

                   <!--xsl:variable name="descriptionVar"><xsl:value-of select="description"/></xsl:variable-->

                            <!--description><xsl:value-of select="description"/></description-->
                            <enclosure>
                     <xsl:attribute name="url">
                    <!-- <xsl:value-of select="."/>
                    <xsl:value-of select="concat('myText:', /*/properties/property[@name='report']/@value)" /> -->
                       <xsl:value-of select="media:content/@url"/>                         
                   </xsl:attribute>
                             <xsl:attribute name="type">
                              <xsl:text>image/jpg</xsl:text>
                              </xsl:attribute>
                          </enclosure>
                            <!--pubDate><xsl:value-of select="pubDate"/></pubDate-->
                        </item>
                    </xsl:for-each>
                </channel>
            </rss>
        </xsl:template>
         <xsl:template name="deleteEllipses">
            <xsl:param name="nodeData"/>
            <xsl:choose>
                <xsl:when test="contains($nodeData, '%Slideshow')">
                    <!--xsl:variable name="prestring" select="substring-before($nodeData, 'Read more')"/-->
                    <!--xsl:variable name="poststring" select="substring-after($nodeData, '...')"/>
                    <xsl:variable name="poststringspace" select="concat(' ', $poststring)"/-->
                    <description><xsl:value-of select="//media:content[1]/media:description"/></description>
                </xsl:when>
                <xsl:otherwise>
                    <description><xsl:value-of select="$nodeData"/></description>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>

    </xsl:stylesheet>

请帮助我。

此致 斯瓦拉杰

1 个答案:

答案 0 :(得分:0)

首先,您需要做的是在XSLT中声明相关的命名空间,以便轻松访问属于该命名空间的XML中的节点

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:media="http://search.yahoo.com/mrss/"
    xmlns:ss="http://contenthub.aol.com/slideshow/">

然后,一种解决方案是使用 xsl:choose 来确定是否存在media:content元素。

<xsl:attribute name="url">
  <xsl:choose>
   <xsl:when test="media:content">
       <xsl:value-of select="media:content/@url"/>                         
   </xsl:when>
   <xsl:otherwise>
       <xsl:value-of select="ss:slideshow/media:content[1]/@url"/>                         
   </xsl:otherwise>
  </xsl:choose>
</xsl:attribute>

事实上,因为看起来你仍然在寻找一个media:content元素,但只是处于不同的层次,你可以完全没有xsl:choose,只是这样做。 ..

<xsl:attribute name="url">
   <xsl:value-of select=".//media:content/@url"/>  
</xsl:attribute>

更好的是,使用“属性值模板”直接在enclosure项目上创建属性

<enclosure url="{.//media:content/@url}" type="image/jpg" />