小写RSS使用XSLT提供标记名称和属性 - 保留名称空间

时间:2016-12-05 20:34:28

标签: xml xslt xslt-1.0

我的任务是处理由第三方管理不善的处理多个RSS Feed

我的问题是他们的大小写非常不可靠 - 例如,有些Feed会使用正确的元素代码<rss><item><enclosure url="example.mp3">等,但是其他时候他们会使用不正确的<RSS><Item><Enclosure URL="example.mp3">个案。

毋庸置疑,这使得阅读XML(使用PHP5 DOMDocument)非常棘手。

我在这里找到了一个相当不错的XSLT样式表(显然非常有才华的michael.hor257k)可以成功解决我的大写问题:“How to convert tags in all tags in xml to lowercase without changing case of atribute values?

然而,XSLT样式表,虽然它可以小写所有元素和属性....它会删除所有RSS根级别名称空间属性!

例如,下面的RSS XML:

<?xml version="1.0" encoding="UTF-8"?>
<RSS xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"     EXAMPLEATTRIBUTE="example">
<channel>
...

通过此XSLT运行时

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />

<xsl:template match="*">
    <xsl:element name="{translate(local-name(), $uppercase, $lowercase)}" namespace="{namespace-uri()}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@*">
    <xsl:attribute name="{translate(local-name(), $uppercase, $lowercase)}" namespace="{namespace-uri()}">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

<xsl:template match="comment() | text() | processing-instruction()">
    <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

将给出以下结果

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" exampleattribute="example">
<channel>
...

当我真正需要的是

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"     exampleattribute ="example">
<channel>
...

我很欣赏这是一个非常小众的问题,但它现在已经困扰了我好几个小时,事实上,几乎是我整天。

TL / DR:有谁请知道如何......

a)使用XSLT 1.0小写RSS XML中的所有属性和元素标记

b)执行此操作时保留所有RSS根级别命名空间(因此'atom'和'iTunes'命名空间仍然存在)

我会非常感激,非常感谢你,

  

----编辑:额外的笔记,根据michael.hor257k的评论----

输入(请注意大写的<RSS></RSS>

<?xml version="1.0" encoding="UTF-8"?><RSS xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  <CHANNEL>
    <atom:link href="http://feeds.soundcloud.com/users/soundcloud:users:245142600/sounds.rss" rel="self" type="application/rss+xml"/>
    <atom:link href="http://feeds.soundcloud.com/users/soundcloud:users:245142600/sounds.rss?before=291065752" rel="next" type="application/rss+xml"/>
    <title>FASNASTIC: The Everything FASNASTIC Feed</title>
    <link>http://fasnastic.com</link>
    <pubDate>Thu, 03 Nov 2016 17:21:11 +0000</pubDate>
    <lastBuildDate>Thu, 03 Nov 2016 17:21:11 +0000</lastBuildDate>
    <ttl>60</ttl>
    <language>en</language>
    <copyright>All rights reserved</copyright>
    <webMaster>feeds@soundcloud.com (SoundCloud Feeds)</webMaster>
    <description>FASNASTIC LTD. is a UK things brand who make games and podcasts and things.

Schedule....

THURSDAY: Game Fart Podcast - The FASNASTIC Farts &amp; Video Games Podcast.
Farting out video game news for your pleasure. Please do not listen to this podcast if you treat video games even remotely seriously...

FRIDAY: The Creepy Midnight Podcast - Do you like creepy shit? We do. This FASNASTIC LTD. podcast is designed to be listened to alone, at midnight. Conspiracy theories, science, diseases, technology and aliens.

Download our apps and games on Android &amp; iOS.
Find out more at http://fasnastic.com/</description>
    <itunes:subtitle>FASNASTIC LTD. is a UK things brand who make game…</itunes:subtitle>
    <itunes:owner>
      <itunes:name>FASNASTIC</itunes:name>
      <itunes:email>fasnastic@gmail.com</itunes:email>
    </itunes:owner>
    <itunes:author>FASNASTIC LTD.</itunes:author>
    <itunes:explicit>yes</itunes:explicit>
    <itunes:image href="http://i1.sndcdn.com/avatars-000274631751-z80y47-original.jpg"/>
    <IMAGE>
      <url>http://i1.sndcdn.com/avatars-000274631751-z80y47-original.jpg</url>
      <title>FASNASTIC</title>
      <link>http://fasnastic.com</link>
    </IMAGE>
    <itunes:category text="Comedy"/>
    <ITEM>
      <guid isPermaLink="false">tag:soundcloud,2010:tracks/291065752</guid>
      <title>FAS - NAS - TIC</title>
      <pubDate>Wed, 02 Nov 2016 03:00:52 +0000</pubDate>
      <link>https://soundcloud.com/fasnastic/fas-nas-tic</link>
      <itunes:duration>00:00:01</itunes:duration>
      <itunes:author>FASNASTIC LTD.</itunes:author>
      <itunes:explicit>yes</itunes:explicit>
      <itunes:summary>The FASNASTIC LTD. Sonic Logo (created by Esa Juhani Ruoho)</itunes:summary>
      <itunes:subtitle>The FASNASTIC LTD. Sonic Logo (created by Esa Juh…</itunes:subtitle>
      <description>The FASNASTIC LTD. Sonic Logo (created by Esa Juhani Ruoho)</description>
      <enclosure type="audio/mpeg" url="http://www.podtrac.com/pts/redirect.mp3/feeds.soundcloud.com/stream/291065752-fasnastic-fas-nas-tic.mp3" length="31805"/>
      <itunes:image href="http://i1.sndcdn.com/artworks-000191668956-448wgw-original.jpg"/>
    </ITEM>
  </CHANNEL>
</RSS>

输出我目前从XSLT上面获取(注意<rss>中缺少的属性 - 原子空命名空间和iTunes命名空间已经消失,顶部link标记已从{{更改1}}到<atom:link href="http://feeds.soundcloud.com/users/soundcloud:users:245142600/sounds.rss?before=291065752" rel="next" type="application/rss+xml"/>

<link xmlns="http://www.w3.org/2005/Atom" href="http://feeds.soundcloud.com/users/soundcloud:users:245142600/sounds.rss?before=291065752" rel="next" type="application/rss+xml"/>

所需的输出(与输入完全相同,仅使用<rss version="2.0"> <channel> <link xmlns="http://www.w3.org/2005/Atom" href="http://feeds.soundcloud.com/users/soundcloud:users:245142600/sounds.rss" rel="self" type="application/rss+xml"/> <link xmlns="http://www.w3.org/2005/Atom" href="http://feeds.soundcloud.com/users/soundcloud:users:245142600/sounds.rss?before=291065752" rel="next" type="application/rss+xml"/> <title>FASNASTIC: The Everything FASNASTIC Feed</title> <link>http://fasnastic.com</link> <pubdate>Thu, 03 Nov 2016 17:21:11 +0000</pubdate> <lastbuilddate>Thu, 03 Nov 2016 17:21:11 +0000</lastbuilddate> <ttl>60</ttl> <language>en</language> <copyright>All rights reserved</copyright> <webmaster>feeds@soundcloud.com (SoundCloud Feeds)</webmaster> <description>FASNASTIC LTD. is a UK things brand who make games and podcasts and things. Schedule.... THURSDAY: Game Fart Podcast - The FASNASTIC Farts &amp; Video Games Podcast. Farting out video game news for your pleasure. Please do not listen to this podcast if you treat video games even remotely seriously... FRIDAY: The Creepy Midnight Podcast - Do you like creepy shit? We do. This FASNASTIC LTD. podcast is designed to be listened to alone, at midnight. Conspiracy theories, science, diseases, technology and aliens. Download our apps and games on Android &amp; iOS. Find out more at http://fasnastic.com/</description> <subtitle xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">FASNASTIC LTD. is a UK things brand who make game…</subtitle> <owner xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd"> <name>FASNASTIC</name> <email>fasnastic@gmail.com</email> </owner> <author xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">FASNASTIC LTD.</author> <explicit xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">yes</explicit> <image xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd" href="http://i1.sndcdn.com/avatars-000274631751-z80y47-original.jpg"/> <image> <url>http://i1.sndcdn.com/avatars-000274631751-z80y47-original.jpg</url> <title>FASNASTIC</title> <link>http://fasnastic.com</link> </image> <category xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd" text="Comedy"/> <link/> <item xmlns:default="http://www.itunes.com/dtds/podcast-1.0.dtd"> <guid ispermalink="false">tag:soundcloud,2010:tracks/291065752</guid> <title>FAS - NAS - TIC</title> <pubdate>Wed, 02 Nov 2016 03:00:52 +0000</pubdate> <link>https://soundcloud.com/fasnastic/fas-nas-tic</link> <default:duration xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">00:00:01</default:duration> <default:author xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">FASNASTIC LTD.</default:author> <default:explicit xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">yes</default:explicit> <default:summary xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">The FASNASTIC LTD. Sonic Logo (created by Esa Juhani Ruoho)</default:summary> <default:subtitle xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">The FASNASTIC LTD. Sonic Logo (created by Esa Juh…</default:subtitle> <description>The FASNASTIC LTD. Sonic Logo (created by Esa Juhani Ruoho)</description> <enclosure type="audio/mpeg" url="http://www.podtrac.com/pts/redirect.mp3/feeds.soundcloud.com/stream/291065752-fasnastic-fas-nas-tic.mp3" length="31805"/> <default:image xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd" href="http://i1.sndcdn.com/artworks-000191668956-448wgw-original.jpg"/> </item> </channel> </rss> 代替<rss><RSS>代替<channel>等等)

<CHANNEL>

这是使用以下PHP脚本运行

<?xml version="1.0" encoding="UTF-8"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  <channel>
    <atom:link href="http://feeds.soundcloud.com/users/soundcloud:users:245142600/sounds.rss" rel="self" type="application/rss+xml"/>
    <atom:link href="http://feeds.soundcloud.com/users/soundcloud:users:245142600/sounds.rss?before=291065752" rel="next" type="application/rss+xml"/>
    <title>FASNASTIC: The Everything FASNASTIC Feed</title>
    <link>http://fasnastic.com</link>
    <pubDate>Thu, 03 Nov 2016 17:21:11 +0000</pubDate>
    <lastBuildDate>Thu, 03 Nov 2016 17:21:11 +0000</lastBuildDate>
    <ttl>60</ttl>
    <language>en</language>
    <copyright>All rights reserved</copyright>
    <webMaster>feeds@soundcloud.com (SoundCloud Feeds)</webMaster>
    <description>FASNASTIC LTD. is a UK things brand who make games and podcasts and things.

Schedule....

THURSDAY: Game Fart Podcast - The FASNASTIC Farts &amp; Video Games Podcast.
Farting out video game news for your pleasure. Please do not listen to this podcast if you treat video games even remotely seriously...

FRIDAY: The Creepy Midnight Podcast - Do you like creepy shit? We do. This FASNASTIC LTD. podcast is designed to be listened to alone, at midnight. Conspiracy theories, science, diseases, technology and aliens.

Download our apps and games on Android &amp; iOS.
Find out more at http://fasnastic.com/</description>
    <itunes:subtitle>FASNASTIC LTD. is a UK things brand who make game…</itunes:subtitle>
    <itunes:owner>
      <itunes:name>FASNASTIC</itunes:name>
      <itunes:email>fasnastic@gmail.com</itunes:email>
    </itunes:owner>
    <itunes:author>FASNASTIC LTD.</itunes:author>
    <itunes:explicit>yes</itunes:explicit>
    <itunes:image href="http://i1.sndcdn.com/avatars-000274631751-z80y47-original.jpg"/>
    <image>
      <url>http://i1.sndcdn.com/avatars-000274631751-z80y47-original.jpg</url>
      <title>FASNASTIC</title>
      <link>http://fasnastic.com</link>
    </image>
    <itunes:category text="Comedy"/>
    <item>
      <guid isPermaLink="false">tag:soundcloud,2010:tracks/291065752</guid>
      <title>FAS - NAS - TIC</title>
      <pubDate>Wed, 02 Nov 2016 03:00:52 +0000</pubDate>
      <link>https://soundcloud.com/fasnastic/fas-nas-tic</link>
      <itunes:duration>00:00:01</itunes:duration>
      <itunes:author>FASNASTIC LTD.</itunes:author>
      <itunes:explicit>yes</itunes:explicit>
      <itunes:summary>The FASNASTIC LTD. Sonic Logo (created by Esa Juhani Ruoho)</itunes:summary>
      <itunes:subtitle>The FASNASTIC LTD. Sonic Logo (created by Esa Juh…</itunes:subtitle>
      <description>The FASNASTIC LTD. Sonic Logo (created by Esa Juhani Ruoho)</description>
      <enclosure type="audio/mpeg" url="http://www.podtrac.com/pts/redirect.mp3/feeds.soundcloud.com/stream/291065752-fasnastic-fas-nas-tic.mp3" length="31805"/>
      <itunes:image href="http://i1.sndcdn.com/artworks-000191668956-448wgw-original.jpg"/>
    </item>
  </channel>
</rss>

1 个答案:

答案 0 :(得分:0)

这不是一项微不足道的任务,而且有充分的理由。原因是在语义上这三者之间没有区别:

<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
    <itunes:owner>

或:

<rss>
    <owner xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">

或:

<rss>
    <default:owner xmlns:default="http://www.itunes.com/dtds/podcast-1.0.dtd">

XSLT语言(特别是XSLT 1.0版本)没有提供控制输出语法形式的工具;这留待处理器随意决定。

出于同样的原因,您尝试强制执行所需语法的任何措施都可以使用一个处理器,而不是另一个处理器。

尽管如此,由于您可能正在使用libxslt处理器,我相信您需要做的就是添加以下模板:

<xsl:template match="RSS">
    <rss>
        <xsl:copy-of select="namespace::*"/>
        <xsl:apply-templates select="@*|node()"/>
    </rss>
</xsl:template>