具有命名空间的Youtube Channel feed的XSLT转换

时间:2017-08-16 23:24:02

标签: xml xslt youtube

如何在html表格中获取entry / media:thumbnail / url值。

Feed 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 个答案:

答案 0 :(得分: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>
相关问题