I am attempting to create a php fantasy football application that uses an existing rss feed to update the database for a university project. My problem is that the only free rss feed I can find is not in a format that would allow me to simply use xslt to remove the information and display it correctly.
Here is a short extract from the 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&g开发者_如何学Ct;
<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>
Is there any way I can extract individual parts of a single tag using PHP or Xslt so when It inserts into the database it seperates the title into home team, away team, score?
Any help would be appreciated so I can begin planning the project?
int xml_parse ( resource $parser , string $data [, bool $is_final = false ] )
This extension lets you create XML parsers and then define handlers for different XML events. Each XML parser also has a few parameters you can adjust.
http://uk.php.net/manual/en/book.xml.php
XSLT's string functions are rudimentary, but they should be good enough for parsing strings that are as regular in layout as those appear to be. The below works on the data you've provided. It will, of course, fail if it doesn't encounter the literal tokens that it's using as delimiters.
<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>
This transformation:
<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>
when applied on the provided XML document:
<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>
produces very precisely the wanted result:
<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>
精彩评论