有什么办法可以使用PHP或Xslt提取单个XML标记的各个部分吗?

时间:2010-11-05 14:17:33

标签: php xml xslt rss

我正在尝试创建一个php fantasy football应用程序,它使用现有的rss feed来更新大学项目的数据库。我的问题是,我能找到的唯一免费RSS源不是一种格式,只允许我使用xslt删除信息并正确显示它。

以下是Feed的简短摘录:

<?xml version="1.0" encoding="UTF-8"?> 
<rss version="2.0">
  <channel>
    <title>Soccer Livescore RSS Feed - ScoresPro.com</title>
    <ttl>2</ttl>
    <link>http://www.scorespro.com</link>
    <description>Latest scores from ScoresPro.com</description>
    <pubDate></pubDate>
    <item>
      <title>Pst  AL Wahda FC Abu Dhabi  - AL Ahli Dubai     0 - 0 (UAE - Premier League) </title>
      <link>http://www.scorespro.com/</link></item>
    <item>
      <title>Pst  Dubai Csc  - AL Wasl Dubai     0 - 0 (UAE - Premier League) </title>
      <link>http://www.scorespro.com/</link>
    </item>
    <item>
      <title>Pst  Ittihad  - Wathbah     0 - 0 (SYRIA - Division 1) </title>
      <link>http://www.scorespro.com/</link>
    </item>
    <item>
      <title>Pst  Saba  - Sepahan     0 - 0 (IRAN - Premier League) </title>
      <link>http://www.scorespro.com/</link>
    </item>
    <item>
      <title>HT  Teshrin  - Foutoua     1 - 0 (SYRIA - Division 1) </title>
      <link>http://www.scorespro.com/</link>
    </item>
  </channel>
</rss>

有没有办法可以使用PHP或Xslt提取单个标签的各个部分,所以当它插入数据库时​​,它会将标题分成主队,客队,得分?

任何帮助都会受到赞赏,所以我可以开始规划项目了吗?

3 个答案:

答案 0 :(得分:0)

int xml_parse ( resource $parser , string $data [, bool $is_final = false ] )

此扩展允许您创建XML解析器,然后为不同的XML事件定义处理程序。每个XML解析器还有一些您可以调整的参数。

http://uk.php.net/manual/en/book.xml.php

答案 1 :(得分:0)

XSLT的字符串函数是基本的,但它们应该足以解析布局中常见的字符串。以下内容适用于您提供的数据。当然,如果它没有遇到它用作分隔符的文字标记,它将会失败。

<xsl:template match="title">
  <scores>
    <xsl:variable name="teams"
                  select="substring-before(., '     ')"/>
    <xsl:variable name="remainder"
                  select="substring-after(., '     ')"/>
    <xsl:variable name="score"
                  select="substring-before($remainder, ' (')"/>
    <home_team>
      <xsl:value-of select="normalize-space(substring-before($teams, ' - '))"/>
    </home_team>
    <away_team>
      <xsl:value-of select="normalize-space(substring-after($teams, ' - '))"/>
    </away_team>
    <score>
      <xsl:value-of select="normalize-space($score)"/>
    </score>
  </scores>
</xsl:template>

答案 2 :(得分:0)

此转化

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

 <xsl:variable name="vDigits" select="'0123456789'"/>

 <xsl:template match="item/title">
  <xsl:variable name="vHTeam" select="substring-before(.,'-')"/>
  <xsl:variable name="vPart2" select=
      "substring-before(substring-after(.,'-'), '-')"/>
  <xsl:variable name="vATeam" select=
   "translate($vPart2,$vDigits,'')"/>

   <xsl:variable name="vScored" select=
   "translate($vPart2, translate($vPart2, $vDigits, ''),'')
   "/>

   <xsl:variable name="vLost" select=
    "substring-before(substring-after(substring-after(.,'-'), '-'), '(')
    "/>

    <xsl:variable name="vScore" select=
     "normalize-space(concat($vScored, ' - ', $vLost))
     "/>

   <xsl:variable name="vLeague" select=
   "substring-before(substring-after(.,'('), ')')
   "/>

   <item>
    <home-team><xsl:value-of select="normalize-space($vHTeam)"/></home-team>
    <away-team><xsl:value-of select="normalize-space($vATeam)"/></away-team>
    <score><xsl:value-of select="$vScore"/></score>
    <league><xsl:value-of select="normalize-space($vLeague)"/></league>
   </item>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的XML文档

<rss version="2.0">
  <channel>
    <title>Soccer Livescore RSS Feed - ScoresPro.com</title>
    <ttl>2</ttl>
    <link>http://www.scorespro.com</link>
    <description>Latest scores from ScoresPro.com</description>
    <pubDate></pubDate>
    <item>
      <title>Pst  AL Wahda FC Abu Dhabi  - AL Ahli Dubai     0 - 0 (UAE - Premier League) </title>
      <link>http://www.scorespro.com/</link></item>
    <item>
      <title>Pst  Dubai Csc  - AL Wasl Dubai     0 - 0 (UAE - Premier League) </title>
      <link>http://www.scorespro.com/</link>
    </item>
    <item>
      <title>Pst  Ittihad  - Wathbah     0 - 0 (SYRIA - Division 1) </title>
      <link>http://www.scorespro.com/</link>
    </item>
    <item>
      <title>Pst  Saba  - Sepahan     0 - 0 (IRAN - Premier League) </title>
      <link>http://www.scorespro.com/</link>
    </item>
    <item>
      <title>HT  Teshrin  - Foutoua     1 - 0 (SYRIA - Division 1) </title>
      <link>http://www.scorespro.com/</link>
    </item>
  </channel>
</rss>

非常精确地生成想要的结果:

<item>
    <home-team>Pst AL Wahda FC Abu Dhabi</home-team>
    <away-team>AL Ahli Dubai</away-team>
    <score>0 - 0</score>
    <league>UAE - Premier League</league>
</item>
<item>
    <home-team>Pst Dubai Csc</home-team>
    <away-team>AL Wasl Dubai</away-team>
    <score>0 - 0</score>
    <league>UAE - Premier League</league>
</item>
<item>
    <home-team>Pst Ittihad</home-team>
    <away-team>Wathbah</away-team>
    <score>0 - 0</score>
    <league>SYRIA - Division 1</league>
</item>
<item>
    <home-team>Pst Saba</home-team>
    <away-team>Sepahan</away-team>
    <score>0 - 0</score>
    <league>IRAN - Premier League</league>
</item>
<item>
    <home-team>HT Teshrin</home-team>
    <away-team>Foutoua</away-team>
    <score>1 - 0</score>
    <league>SYRIA - Division 1</league>
</item>