Is there a way to extract the individual parts of a single XML tag using PHP or Xslt?

I am trying to create a php fantasy flash application that uses an existing rss feed to update a database for a university project. My problem is that the only free rss channel that I can find is not in a format that would allow me to simply use xslt to delete the information and display it correctly.

Here’s a short excerpt 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>
      <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 it possible to extract in some way the individual parts of the same tag using PHP or Xslt, so when it inserts into the database, it splits the header into the home team, guest team, evaluation?

Any help would be appreciated to begin project planning?

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

XML-, XML. XML , .

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

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

This conversion is :

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

gives very precisely the desired 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>
0
source

Source: https://habr.com/ru/post/1773242/


All Articles