【问题标题】:Lowercase RSS feeds tag names and attributes with XSLT -- keeping namespaces小写 RSS 使用 XSLT 提供标签名称和属性——保留名称空间
【发布时间】:2016-12-05 20:34:27
【问题描述】:

我的任务是处理几个 RSS 提要,这些提要被第三方管理得很差。

我的问题是它们的大小写非常不可靠 - 例如,一些提要使用正确的元素标签<rss><item><enclosure url="example.mp3"> 等,但其他时候他们使用不正确的@ 987654326@、<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”命名空间)

非常感谢,非常感谢,

---- 编辑:附加注释,根据 cmets 中 michael.hor257k 的要求 ----

输入(注意大写的&lt;RSS&gt;&lt;/RSS&gt;

<?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 上面得到的输出(注意 &lt;rss&gt; 中缺少的属性——原子命名空间和 iTunes 命名空间消失了,顶部的 link 标记已从 &lt;atom:link href="http://feeds.soundcloud.com/users/soundcloud:users:245142600/sounds.rss?before=291065752" rel="next" type="application/rss+xml"/&gt; 更改为 @987654339 @

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

所需的输出(与输入完全相同,只是用&lt;rss&gt; 而不是&lt;RSS&gt;&lt;channel&gt; 而不是&lt;CHANNEL&gt; 等)

<?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>

这是使用下面的 PHP 脚本运行的

$xmlLoaded = new DOMDocument;
$xmlLoaded->preserveWhiteSpace = FALSE;
$xmlLoaded->loadXML($stringContainingOriginalXML);
//Load the style sheet inside an object
$xslStylesheetObject= new DOMDocument();
//Create a processor
$xslStylesheetProcessor = new XSLTProcessor();
$xslStylesheetProcessor->registerPHPFunctions();
//Import stylesheet into processor
$xslStylesheetProcessor->importStylesheet($xslStylesheetObject);
//Reload new XML after processing
$xmlLoaded->loadXML($xslStylesheetProcessor->transformToXML($xmlLoaded));

【问题讨论】:

  • 为什么需要那些命名空间声明?如果在输出中使用它们,它们将被自动包含在内;否则它们是多余的,没有人会错过它们(或者至少没有人应该)。
  • 您好 michael.hor257k - 感谢您的回复!
  • 需要这些名称空间来保持 XML 有效(对于 iTunes、RSS 等)——您的原始 XSLT 会从文档顶部删除所有名称空间,因此它无法通过 iTunes 验证... . 有没有办法像我上面描述的那样将所有命名空间保留在 RSS 元素内的顶部?
  • 您的原始脚本也将“00:00:01”更改为“itunes.com/dtds/podcast-1.0.dtd">00:00:01</…>” -----如果您的小写 XSLT 根本不干扰这些名称空间,那就太好了
  • 请编辑您的问题并包括输入的代表性示例以及预期输出。您使用的是哪种 XSLT 处理器?我不知道有哪一种会产生您声称的结果。

标签: xml xslt xslt-1.0


【解决方案1】:

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

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

【讨论】:

  • 感谢您对这个主题的快速教育——很高兴知道所有这些元素实际上在语义上是相同的。谢谢你:)
  • 我无法让你最新的模板代码工作(我不确定在哪里添加它?它似乎对我不起作用......)但我仍在标记我确信这是正确的,您对这三种类型如何相同的解释非常有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 2012-01-21
  • 2018-06-26
相关资源
最近更新 更多