2017-08-16 82 views
1

如何获取html表格中的条目/媒体:缩略图/ url值。带命名空间的Youtube频道Feed的XSLT转换

饲料XML:

<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom"> 
    <link rel="self" href="http://www.youtube.com/feeds/videos.xml?channel_id=MyChannel"/> 
    <id>yt:channel:MyChannel</id> 
    <yt:channelId>MyChannel</yt:channelId> 
    <title>Jon Smith</title> 
    <link rel="alternate" href="https://www.youtube.com/channel/MyChannel"/> 
    <author> 
    <name>Jon Smith</name> 
    <uri> 
     https://www.youtube.com/channel/MyChannel 
    </uri> 
    </author> 
    <published>2007-02-13T04:11:08+00:00</published> 
    <entry> 
    <id>yt:video:BQrqsddkSuI_Uo</id> 
    <yt:videoId>BQrqsskSuI_Uo</yt:videoId> 
    <yt:channelId>MyChannel</yt:channelId> 
    <title>Title 1</title> 
    <link rel="alternate" href="https://www.youtube.com/watch?v=BQrqsddkSusdfdI_Uo"/> 
    <author> 
     <name>Jon Smith</name> 
     <uri> 
     https://www.youtube.com/channel/MyChannel 
     </uri> 
    </author> 
    <published>2017-07-26T17:41:31+00:00</published> 
    <updated>2017-08-07T11:17:33+00:00</updated> 
    <media:group> 
     <media:title>Title 1</media:title> 
     <media:content url="https://www.youtube.com/v/BQrqsddskSuI_Uo?version=3" type="application/x-shockwave-flash" width="640" height="390"/> 
     <media:thumbnail url="https://i3.ytimg.com/vi/BQrqksdsdSuI_Uo/hqdefault.jpg" width="480" height="360"/> 
     <media:description>Description 1.</media:description> 
     <media:community> 
     <media:starRating count="1" average="5.00" min="1" max="5"/> 
     <media:statistics views="13"/> 
     </media:community> 
    </media:group> 
    </entry> 
    <entry> 
    <id>yt:video:uhAXp6sdfsddRnSA</id> 
    <yt:videoId>uhAXp6sdfsdRnSA</yt:videoId> 
    <yt:channelId>MyChannel</yt:channelId> 
    <title> 
     Title 2 
    </title> 
    <link rel="alternate" href="https://www.youtube.com/watch?v=uhAXsdsdp6dRnSA"/> 
    <author> 
     <name>Jon Smith</name> 
     <uri> 
     https://www.youtube.com/channel/MyChannel 
     </uri> 
    </author> 
    <published>2014-10-28T21:39:27+00:00</published> 
    <updated>2017-07-26T17:42:27+00:00</updated> 
    <media:group> 
     <media:title> 
     Title 2 
     </media:title> 
     <media:content url="https://www.youtube.com/v/uhAXp6dRnsddSA?version=3" type="application/x-shockwave-flash" width="640" height="390"/> 
     <media:thumbnail url="https://i2.ytimg.com/vi/uhAXp6dsdsdRnSA/hqdefault.jpg" width="480" height="360"/> 
     <media:description>Description 2</media:description> 
     <media:community> 
     <media:starRating count="0" average="0.00" min="1" max="5"/> 
     <media:statistics views="25"/> 
     </media:community> 
    </media:group> 
    </entry> 
</feed> 

XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="feed"> 
<html> 
<body> 
    <h2>My Videos</h2> 
    <table border="1"> 
    <tr bgcolor="#9acd32"> 
     <th style="text-align:left">Title</th> 
     <th style="text-align:left">Image URL</th> 
    </tr> 
    <xsl:for-each select="entry"> 
    <tr> 
     <td><xsl:value-of select="title"/></td> 
     <td><xsl:value-of select="media:thumbnail/@url"/></td> 
    </tr> 
    </xsl:for-each> 
    </table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

回答

1

想通了。您需要重命名XSLT文件中源XML文件的名称空间(默认或其他名称空间)。

所以在源文件中,你的命名空间:xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom"

所以这些添加到您的XSLT文件,以及...但是....你必须给默认xmlns明确的前缀。所以我给它一个d,如默认。所以在XSLT,该命名空间,你必须xmlns:d="http://www.w3.org/2005/Atom"

最终XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:media="http://search.yahoo.com/mrss/" xmlns:d="http://www.w3.org/2005/Atom" > 
<xsl:template match="d:feed"> 
<html> 
<body> 
    <h2>My Videos</h2> 
    <table border="1"> 
    <tr bgcolor="#9acd32"> 
     <th style="text-align:left">Title<xsl:value-of select="d:id"/></th> 
     <th style="text-align:left">Image URL</th> 
    </tr> 
    <xsl:for-each select="d:entry"> 
    <tr> 
     <td><xsl:value-of select="d:title"/></td> 
     <td><img> 
      <xsl:attribute name="src"> 
       <xsl:value-of select='media:group/media:thumbnail/@url'/> 
      </xsl:attribute> 
      </img> 
     </td> 
    </tr> 
    </xsl:for-each> 
    </table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 
+0

正要具有完全相同的回答,但与非'的'! – Parfait