2

I just started playing with xpath and need some help.

I'm using tvdb's api and have the following xml (shortened for the question)

<Data>
   <Episode>
      <id>4762752</id>
      <EpisodeName>I.</EpisodeName>
      <EpisodeNumber>1</EpisodeNumber>
      <SeasonNumber>1</SeasonNumber>
   </Episode>
   <Episode>
      <id>4762756</id>
      <EpisodeName>II.</EpisodeName>
      <EpisodeNumber>2</EpisodeNumber>
      <SeasonNumber>1</SeasonNumber>
   </Episode>
</Data>

I want to get all the Episode details where episode number = something and season number is equal to something.

I think I have the xpath query correct but I cannot (or do not know how to) access the data after.

My code so far:

$tvdbSeriesData = simplexml_load_file($tvdbSeriesFeed);

$tvdbEpisodes = $tvdbSeriesData->xpath("//Data/Episode[SeasonNumber/text() = '$seasonNumber'][EpisodeNumber/text() = '$episodeNumber']");

echo $tvdbEpisodes->id;

This is my first try with xpath, so apologies if this is trivial, but could someone throw me in the right direction or explain to me what I've done wrong here.

Thanks

1
  • It's look like working fine if you try print_r($tvdbEpisodes); Commented Aug 1, 2016 at 21:14

1 Answer 1

1

The xpath function returns Array with the results of your xpath query:

$tvdbSeriesFeed = '<Data>
   <Episode>
      <id>4762752</id>
      <EpisodeName>I.</EpisodeName>
      <EpisodeNumber>1</EpisodeNumber>
      <SeasonNumber>1</SeasonNumber>
   </Episode>
   <Episode>
      <id>4762756</id>
      <EpisodeName>II.</EpisodeName>
      <EpisodeNumber>2</EpisodeNumber>
      <SeasonNumber>1</SeasonNumber>
   </Episode>
</Data>';

$tvdbSeriesData = simplexml_load_string($tvdbSeriesFeed);
$seasonNumber = 1;
$episodeNumber = 1;
$tvdbEpisodes = $tvdbSeriesData->xpath("//Data/Episode[SeasonNumber/text() = '$seasonNumber'][EpisodeNumber/text() = '$episodeNumber']");

echo $tvdbEpisodes[0]->id; // 4762752

I used the function simplexml_load_string only for the example to work.

You can also var_dump the results:

var_dump($tvdbEpisodes);

Will output:

array(1) {
  [0] =>
  class SimpleXMLElement#3 (4) {
    public $id =>
    string(7) "4762752"
    public $EpisodeName =>
    string(2) "I."
    public $EpisodeNumber =>
    string(1) "1"
    public $SeasonNumber =>
    string(1) "1"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.