gpt4 book ai didi

xml - 具有 namespace 的YouTube channel 供稿的XSLT转换

转载 作者:行者123 更新时间:2023-12-03 05:31:17 24 4
gpt4 key购买 nike

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

提要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>

最佳答案

弄清楚了。您需要从XSLT文件中的源XML文件重命名 namespace (默认或其他方式)。

因此,在源文件中,您具有以下 namespace :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中,对于该 namespace ,您将拥有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>

关于xml - 具有 namespace 的YouTube channel 供稿的XSLT转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45724392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com