RSS Feed验证报错:media:content未定义元素问题求助
问题
将RSS Feed通过W3C Feed验证工具检测时,出现以下报错:
抱歉,该Feed验证不通过。
line 31, column 0: Undefined media:content element: media:hasSyndicationRights (30 occurrences) [help] <media:hasSyndicationRights>1</media:hasSyndicationRights> line 32, column 0: Undefined media:content element: media:licenseId (30 occurrences) [help] <media:licenseId/> line 33, column 0: Undefined media:content element: media:licensorName (30 occurrences) [help] <media:licensorName/>
已正确声明media命名空间xmlns:media="http://search.yahoo.com/mrss/",以下是RSS Feed的核心代码片段:
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" version="2.0"> <channel> <title>Cricket365</title> <atom:link href="http://cricket365.test" rel="self" type="application/rss+xml"/> <link>http://cricket365.test</link> <description>Love the game as much as we do</description> <language>en-US</language> <item> <!-- 省略部分item内容 --> <media:content url="http://cricket365.test/content/uploads/2022/08/2.67371035.jpg" medium="image" type="image/jpeg" expression="full" lang="en"> <media:title>2.67371035.jpg</media:title> <media:text> <![CDATA[ ]]> </media:text> <media:credit>PA Images</media:credit> <media:hasSyndicationRights>1</media:hasSyndicationRights> <media:licenseId/> <media:licensorName/> </media:content> <dcterms:modified>2022-09-20T12:10:00+00:00</dcterms:modified> </item> </channel> </rss>
解决方案
核心原因
media:hasSyndicationRights、media:licenseId、media:licensorName这三个元素不属于标准的MRSS(Media RSS)规范,W3C验证工具仅认可该规范内定义的元素,因此抛出未定义错误。
处理方式
有两种可行的解决路径:
移除非标准元素
直接删除这三个不符合规范的元素,修改后的media:content片段如下:<media:content url="http://cricket365.test/content/uploads/2022/08/2.67371035.jpg" medium="image" type="image/jpeg" expression="full" lang="en"> <media:title>2.67371035.jpg</media:title> <media:text> <![CDATA[ ]]> </media:text> <media:credit>PA Images</media:credit> </media:content>这是最直接的方式,能快速通过验证。
自定义命名空间保留数据
如果需要保留这些自定义元数据,可声明一个独立的自定义命名空间,比如xmlns:custom="http://your-domain.com/custom-rss/",然后将元素替换为自定义前缀:<!-- 在rss根元素添加自定义命名空间 --> <rss xmlns:content="..." xmlns:custom="http://your-domain.com/custom-rss/" ... xmlns:media="http://search.yahoo.com/mrss/" version="2.0"> <!-- 修改media:content内的元素 --> <media:content ...> <!-- 原有标准元素 --> <custom:hasSyndicationRights>1</custom:hasSyndicationRights> <custom:licenseId/> <custom:licensorName/> </media:content>这种方式既保留了数据,又符合XML命名空间规范,不会触发验证错误。
内容的提问来源于stack exchange,提问作者Syed Zohaib Haider
相关产品推荐
相关产品推荐

