【问题标题】:Restructure XML File using XSLT or PHP script使用 XSLT 或 PHP 脚本重构 XML 文件
【发布时间】:2012-06-30 12:02:23
【问题描述】:

我有一个 iphone 应用程序,它解析标准 rss 提要并将其显示在桌子上。 但是,我从客户端获得了一个提要,但应用程序中的解析器无法获取节点并解析数据,因为它不是标准的 rss 提要。

他们给我的布局如下:

    <rss version="0.92">
        <channel>
        <title>Feed</title>
        <link>http://google.com</link>
        <description>
        Description here
        </description>
        <lastBuildDate>30 June +0100</lastBuildDate>
        <language>en</language>
            <event>
                <eventID>123</eventID>
                <name>Name here</name>
                <date>2012-06-29</date>
                <time>21:00</time>
                <category>Arts</category>
                <info>
                 Info here
                </info>
            </event>
            <event>
                <eventID>223</eventID>
                <name>Name here</name>
                <date>2012-06-30</date>
                <time>22:00</time>
                <category>Dance</category>
                <info>
                 Info here
                </info>
            </event>
    </channel>
</rss>

有没有办法使用 XSLT 或 PHP 脚本将此 xml 文件重组为标准的 rss 提要布局? 标准的 RSS 提要布局如下:

<rss>
     <channel>
     <item>
        <title>
            <![CDATA[ Title here ]]>
        </title>
        <link>
            http://www.theatre.com/
        </link>
        <guid>
        http://www.theatre.com
        </guid>
        <description> 
         <p> Description </p>

        </description>
        <dc:subject>
        <![CDATA[ ]]>
        </dc:subject>
        <dc:date>2013-02-01T18:00:04+00:00</dc:date>
      </item>
     </channel>
</rss>

【问题讨论】:

  • 两者均未验证为 RSS:validator.w3.org/appc/#validate_by_input
  • 输出的guid元素内容从何而来?输入中的事件对应于输出中的什么? cd:date 的内容来自哪里?
  • dc 输出的 url 边界是什么?
  • 告诉客户端为您提供有效的 RSS v2.0 提要。
  • 您的评论与提供的样本无关。能否请您提供: (1) 样本输入文件; (2) 给定输入的预期输出; (三)转化规则; (4) 您使用的是哪个 XSLT 版本:1.0 还是 2.0?

标签: php xml xslt rss atom-feed


【解决方案1】:

不清楚你想要什么,所以这个样式表只是说明你可能需要什么。

此 XSLT 1.0 样式表 ....

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
 <rss>
  <channel>
    <xsl:apply-templates select="rss/channel/event" /> 
  </channel>
 </rss>
</xsl:template>

<xsl:template match="event" >
 <item>
  <title><xsl:value-of select="../title" /></title>
  <link><xsl:value-of select="../link" /></link>
  <guid>http://www.theatre.com</guid>
  <description><xsl:value-of select="../description" /></description>
  <subject><xsl:value-of select="category" /></subject>
  <date><xsl:value-of select="date" />T<xsl:value-of select="time" />+00:00</date>
 </item>
</xsl:template>      

</xsl:stylesheet>

...当应用于您提供的示例输入时,将生成此文档...*

<?xml version="1.0" encoding="utf-8"?>
<rss>
  <channel>
    <item>
      <title>Feed</title>
      <link>http://google.com</link>
      <guid>http://www.theatre.com</guid>
      <description>
        Description here
        </description>
      <subject>Arts</subject>
      <date>2012-06-29T21:00+00:00</date>
    </item>
    <item>
      <title>Feed</title>
      <link>http://google.com</link>
      <guid>http://www.theatre.com</guid>
      <description>
        Description here
        </description>
      <subject>Dance</subject>
      <date>2012-06-30T22:00+00:00</date>
    </item>
  </channel>
</rss>    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 2021-04-19
    • 2018-05-23
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多