xml - 从youtube api XSLT中获取观看次数

标签 xml api xslt youtube youtube-api

我正在尝试从youtube api中获取 View 数量,它对标题,URL和缩略图效果很好,尽管对 View 数量却不起作用。

这是我试图从中获取的XML代码(请看最后一行):

<feed xmlns:gml="http://www.opengis.net/gml" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:yt="http://gdata.youtube.com/schemas/2007" xmlns:georss="http://www.georss.org/georss" xmlns:app="http://purl.org/atom/app#" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns="http://www.w3.org/2005/Atom">
<id>http://gdata.youtube.com/feeds/api/standardfeeds/on_the_web</id>
<updated>2012-03-21T03:32:37.000-07:00</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video"/>
<title type="text">On the Web</title>
<logo>http://www.youtube.com/img/pic_youtubelogo_123x63.gif</logo>
<link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/standardfeeds/on_the_web"/>
<link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/standardfeeds/on_the_web/batch"/>
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/standardfeeds/on_the_web?start-index=1&amp;max-results=25"/>
<link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/standardfeeds/on_the_web?start-index=26&amp;max-results=25"/>
<author>
    <name>YouTube</name>
    <uri>http://www.youtube.com/</uri>
</author>
<generator version="2.1" uri="http://gdata.youtube.com">YouTube data API</generator>
<openSearch:totalResults>192</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry>
    <id>http://gdata.youtube.com/feeds/api/videos/LSNxrEa3Usw</id>
    <published>2012-03-07T01:09:29.000Z</published>
    <updated>2012-03-21T10:32:34.000Z</updated>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video"/>
    <category scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="Entertainment" label="Entertainment"/>
    <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="makena"/>
    <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="adele"/>
    <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="singing"/>
    <title type="text">2yr old Makena sings Adele...so cute</title>
    <content type="text">My 2yr old daughter loves Adele and sings along!</content>
    <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=LSNxrEa3Usw&amp;feature=youtube_gdata"/>
    <link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/LSNxrEa3Usw/responses"/>
    <link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/LSNxrEa3Usw/related"/>
    <link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=LSNxrEa3Usw"/>
    <link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/standardfeeds/on_the_web/v/LSNxrEa3Usw"/>
    <author>
        <name>heatherfedorick</name>
        <uri>http://gdata.youtube.com/feeds/api/users/heatherfedorick</uri>
    </author>
    <gd:comments>
        <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#comments" href="http://gdata.youtube.com/feeds/api/videos/LSNxrEa3Usw/comments" countHint="2433"/>
    </gd:comments>
    <yt:hd/>
    <media:group>
        <media:category label="Entertainment" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Entertainment</media:category>
        <media:content url="http://www.youtube.com/v/LSNxrEa3Usw?version=3&amp;f=standard&amp;app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="105" yt:format="5"/>
        <media:content url="rtsp://v5.cache6.c.youtube.com/CiQLENy73wIaGwnMUrdGrHEjLRMYDSANFEgGUghzdGFuZGFyZAw=/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="105" yt:format="1"/>
        <media:content url="rtsp://v5.cache6.c.youtube.com/CiQLENy73wIaGwnMUrdGrHEjLRMYESARFEgGUghzdGFuZGFyZAw=/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="105" yt:format="6"/>
        <media:description type="plain">My 2yr old daughter loves Adele and sings along!</media:description>
        <media:keywords>makena, adele, singing</media:keywords>
        <media:player url="http://www.youtube.com/watch?v=LSNxrEa3Usw&amp;feature=youtube_gdata_player"/>
        <media:restriction type="country" relationship="deny">DE</media:restriction>
        <media:thumbnail url="http://i.ytimg.com/vi/LSNxrEa3Usw/0.jpg" height="360" width="480" time="00:00:52.500"/>
        <media:thumbnail url="http://i.ytimg.com/vi/LSNxrEa3Usw/1.jpg" height="90" width="120" time="00:00:26.250"/>
        <media:thumbnail url="http://i.ytimg.com/vi/LSNxrEa3Usw/2.jpg" height="90" width="120" time="00:00:52.500"/>
        <media:thumbnail url="http://i.ytimg.com/vi/LSNxrEa3Usw/3.jpg" height="90" width="120" time="00:01:18.750"/>
        <media:title type="plain">2yr old Makena sings Adele...so cute</media:title>
        <yt:duration seconds="105"/>
    </media:group>
    <gd:rating average="4.9061623" max="5" min="1" numRaters="18159" rel="http://schemas.google.com/g/2005#overall"/>
    <yt:statistics favoriteCount="3144" viewCount="3134579"/>
</entry>

这是获取数据并将其放入自己定义的xml形式的代码:
<?xml version="1.0" ?>

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.w3.org/2005/Atom"
xmlns:media='http://search.yahoo.com/mrss/'>
xmlns:yt='http://gdata.youtube.com/schemas/2007'

<xsl:template match="/">

<xsl:for-each select="/a:feed/a:entry">
<item>
    <xsl:attribute name="type">youtube</xsl:attribute>

    <xsl:attribute name="name">
        <xsl:value-of select="a:title" />
    </xsl:attribute>

    <xsl:attribute name="author">
        <xsl:value-of select="a:author/a:name" />
    </xsl:attribute>

    <xsl:attribute name="url">
        <xsl:value-of select="a:link/@href" />
    </xsl:attribute>

    <xsl:attribute name="image">
        <xsl:value-of select="media:group/media:thumbnail[@width='120']/@url" />
    </xsl:attribute>

    <xsl:attribute name="score">
        <xsl:value-of select="yt:statistics/@viewCount" />
    </xsl:attribute>
</item>
</xsl:for-each>

</xsl:template>

尽管行:
<xsl:attribute name="score">
    <xsl:value-of select="yt:statistics/@viewCount" />
</xsl:attribute>

根本不会返回我的分数:(

知道我在做什么错吗?

最佳答案

您作为样式表发布的内容格式不正确,尤其是其中带有前缀yt的 namespace 声明

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.w3.org/2005/Atom"
xmlns:media='http://search.yahoo.com/mrss/'>
xmlns:yt='http://gdata.youtube.com/schemas/2007'

被放错地方了
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.w3.org/2005/Atom"
xmlns:media='http://search.yahoo.com/mrss/'
xmlns:yt='http://gdata.youtube.com/schemas/2007'>

据我所知,这就是全部错误。但是,我希望任何XSLT处理器都希望报告该错误,因此也许在您的实际代码中还有其他或进一步的问题。如果仍然有问题,请发布最少但完整的示例,以便我们重现问题,包括您收到的任何错误消息以及XSLT处理器的名称和版本。

关于xml - 从youtube api XSLT中获取观看次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803012/

相关文章:

android - 防止 AlertDialog 填满屏幕

java - 生成的 XML 中存在意外属性

java - JAXB 能否将字符串解码为日期属性

javascript - 将 unicode 转换回文本 - Wordpress JSON api

xslt - xsl :key not working when looping through nodeset obtained with xalan:nodeset

php - 如何使用 PHP 解析 XML?

javascript - 如何通过 AJAX 发送 YouTube 视频元数据

php - Node ,带有 PHP api 服务器的 React Web 服务器

xml - xsl : how to split strings?

java - XSLT 处理递归深度