gpt4 book ai didi

xml - 处理XSLT命名空间不匹配

转载 作者:行者123 更新时间:2023-12-03 06:02:54 25 4
gpt4 key购买 nike

因此,希望这将是我对该项目的最终帮助请求,因为你们已经提供了很多帮助。

基本上,我正在写一些代码,将YouTube播放列表转换为RSS feed。我已经按照我想要的方式工作了,除了一个小细节:我不确定为什么,但是我需要从YouTube XML中删除一个 namespace ,然后才能进行编译。

YouTube的 namespace 声明如下所示:

<feed xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:gml='http://www.opengis.net/gml' xmlns:yt='http://gdata.youtube.com/schemas/2007' xmlns:georss='http://www.georss.org/georss' gd:etag='W/&quot;A04NRn47eCp7I2A9WhJTF0g.&quot;'>

正常情况下,我会使用完全相同的参数,除非出于某种原因(如果包括 xmlns='http://www.w3.org/2005/Atom'段),整个过程都返回null。

到目前为止,我的技术一直是从YouTube XML中剔除这一行,但这使我感到有点不雅。如果有人比我更了解这一点,可以指出我当前代码的问题,我将不胜感激。

亚当

编辑:供引用,这是我的XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007' gd:etag='W/&quot;Ak8EQX47eCp7I2A9WhdSEkQ.&quot;'>

<xsl:template match="/">
<xsl:for-each select="entry">
<xsl:if test="yt:position &lt; 3">
<item>

<title><xsl:value-of select="title" /></title>
<xsl:variable name="videoid" select="substring-before(substring-after(media:group/media:content/@url, 'v/'), '?')" /> <!--Extracting the YouTube video ID-->
<description>
<embed src="http://www.youtube.com/v/{$videoid}" type="application/x-shockwave-flash" allowscriptaccess="never" allowfullscreen="true" width="500" height="300"></embed> <!--Embed code for the video-->
<br />
<br />
<xsl:call-template name="ParseLink"> <!--Parses the video description such that URLs become links-->
<xsl:with-param name="text" select="media:group/media:description" />
</xsl:call-template>
<img src="http://i.ytimg.com/vi/{$videoid}/hqdefault.jpg" style="display:none"/> <!--Includes hidden thumbnail-->
</description>

<link>http://www.youtube.com/watch?v=<xsl:value-of select="$videoid" /></link> <!--Link to video on YouTube-->
<guid isPermaLink="false">http://www.youtube.com/watch?v=<xsl:value-of select="$videoid" /></guid>


</item>
</xsl:if>
</xsl:for-each>
</xsl:template>

<xsl:template name="ParseLink"> <!--YouTube automatically turns URLs into links, this performs the same function for RSS-->
<xsl:param name="text"/>
<xsl:choose>

<xsl:when test="contains($text,'http')">
<a href="http{substring-before(substring-after($text, 'http'), '&#xA;')}">Watch the complete video on our website</a>
<br/>
<xsl:call-template name="ParseText"> <!--Once the link is found, parses the rest of the text-->
<xsl:with-param name="text">
<xsl:value-of select="substring-after($text,'&#xA;')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>

<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="ParseText"> <!--Replaces line breaks in the description to <br> tags-->
<xsl:param name="text"/>
<xsl:choose>

<xsl:when test="contains($text,'&#xA;')">
<xsl:value-of select="substring-before($text,'&#xA;')"/>
<br />
<xsl:call-template name="ParseText">
<xsl:with-param name="text">
<xsl:value-of select="substring-after($text,'&#xA;')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>

<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>

</xsl:choose>
</xsl:template>

最佳答案

您的样式表不起作用的原因是因为您没有定义Atom命名空间... YouTube的默认命名空间是http://www.w3.org/2005/Atom。如果要在“提要”节点上进行匹配,则将具有如下样式表:

<xsl:stylesheet xmlns:atom="http://www.w3.org/2005/Atom" ... more stuff ... >
<xsl:template match="/atom:feed">
etc. etc.
</xsl:template>
</xsl:stylesheet>

编辑:
这是一个有效的示例样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007" gd:etag="W/&quot;Ak8EQX47eCp7I2A9WhdSEkQ.&quot;">
<xsl:template match="/atom:feed">
<xsl:apply-templates select="atom:entry"/>
</xsl:template>
<xsl:template match="atom:entry">
<p>Entry:<xsl:value-of select="atom:title"/>
</p>
</xsl:template>
</xsl:stylesheet>

编辑:
好的,这是您更新的样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007" gd:etag="W/&quot;Ak8EQX47eCp7I2A9WhdSEkQ.&quot;">
<xsl:template match="/atom:feed">
<xsl:for-each select="atom:entry">
<item>
<title>
<xsl:value-of select="atom:title"/>
</title>
<xsl:variable name="videoid" select="substring-before(substring-after(media:group/media:content/@url, 'v/'), '?')"/>
<!--Extracting the YouTube video ID-->
<description>
<embed src="http://www.youtube.com/v/{$videoid}" type="application/x-shockwave-flash" allowscriptaccess="never" allowfullscreen="true" width="500" height="300"/>
<!--Embed code for the video-->
<br/>
<br/>
<xsl:call-template name="ParseLink">
<!--Parses the video description such that URLs become links-->
<xsl:with-param name="text" select="media:group/media:description"/>
</xsl:call-template>
<img src="http://i.ytimg.com/vi/{$videoid}/hqdefault.jpg" style="display:none"/>
<!--Includes hidden thumbnail-->
</description>
<link>http://www.youtube.com/watch?v=<xsl:value-of select="$videoid"/>
</link>
<!--Link to video on YouTube-->
<guid isPermaLink="false">http://www.youtube.com/watch?v=<xsl:value-of select="$videoid"/>
</guid>
</item>
</xsl:for-each>
</xsl:template>
<xsl:template name="ParseLink">
<!--YouTube automatically turns URLs into links, this performs the same function for RSS-->
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text,'http')">
<a href="http{substring-before(substring-after($text, 'http'), '&#xA;')}">Watch the complete video on our website</a>
<br/>
<xsl:call-template name="ParseText">
<!--Once the link is found, parses the rest of the text-->
<xsl:with-param name="text">
<xsl:value-of select="substring-after($text,'&#xA;')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ParseText">
<!--Replaces line breaks in the description to <br> tags-->
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text,'&#xA;')">
<xsl:value-of select="substring-before($text,'&#xA;')"/>
<br/>
<xsl:call-template name="ParseText">
<xsl:with-param name="text">
<xsl:value-of select="substring-after($text,'&#xA;')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

关于xml - 处理XSLT命名空间不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11234537/

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