I've been struggling over this all day and in reality it's probably really simple... but I'm a complete beginner to the world of PHP and XML so could really do with some help.
I'm using SimpleXML to parse my data and have two second-level groups - (yearlist) and (eplist). I have (year) nested inside (yearlist) which has an attribute "yid", set as ID in my DTD. It also has (yearname) nested inside (year) which contains a more detailed description to be displayed as output. I have (ep) nested inside (eplist), with the attribute "yearid" (which correlates directly to "yid"), set as IDREF in my DTD.
Basically, when I'm parsing the data for (eplist), I want to use (yearname) as a group header - using yearid=yid>yearname as the path.
I've created an example of my data which may help explain my problem better.
Here is my DTD:
<?xml encoding="UTF-8"?>
<!ELEMENT besteplist (yearlist,eplist)>
<!ELEMENT yearlist (year)+>
<!ELEMENT year (yearname)>
<!ATTLIST year
yid ID #REQUIRED>
<!ELEMENT yearname (#PCDATA)>
<!ELEMENT eplist (ep)+>
<!ELEMENT ep (eptitle,eptnumber)>
<!ATTLIST ep
eid ID #REQUIRED
yearid IDREF #IMPLIED>
<!ELEMENT eptitle (#PCDATA)>
<!ELEMENT eptnumber (#PCDATA)>
Here is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE besteplist SYSTEM "example.dtd">
<besteplist>
<yearlist>
<year yid="y1">
<yearname>1995, Season 1</yearname>
</year>
<year yid="y2">
<yearname>1996, Season 2</yearname>
</year>
<year yid="y3">
<yearname>1997, Season 3</yearname>
</year>
</yearlist>
<eplist>
<ep yearid="y1" eid="e1">
<eptitle>The First Episode</eptitle>
<eptnumber>1</eptnumber>
</ep>
<ep yearid="y2" eid="e2">
<eptitle>Bla bla bla</eptitle>
<eptnumber>21</eptnumber>
</ep>
<ep yearid="y2" eid="e3">
<eptitle>Rar rar rar</eptitle>
<eptnumber>39</eptnumber>
</ep>
<ep yearid="y2" eid="e4">
<eptitle>Tra la la</eptitle>
<eptnumber>45</eptnumber>
</ep>
<ep yearid="y3" eid="e5">
<eptitle>Donkey</eptitle>
<eptnumber>126</eptnumber>
</ep>
</eplist>
</besteplist>
Here is an example of how I'd like the output to look:
SEASON: 1995, Season 1
EPISODE TITLE: The First Episode
EPISODE NUMBER: 1
SEASON: 1996, Season 2
EPISODE TITLE: Bla bla bla
EPISODE NUMBER: 21
EPISODE TITLE: Rar rar rar
EPISODE NUMBER: 39
EPISODE TITLE: Tra la la
EPISODE NUMBER: 45
SEASON: 1997, Season 3
EPISODE TITLE: Donkey
EPISODE NUMBER: 126
I don't think it'll be much use posting the code I've attempted already as it's probably fairly useless... what I have managed to do is the very basics. Once I've got this down I can move on to the next stage... formatting...
I'm not attached to SimpleXML in any way so if somebody can suggest a more efficient way of doing things, I'm all ears.
Thank you so much in advance to anybody who takes the time to help me out. :)
Sam
In response to @michi, I've been sat trying to work out xpath and reading all sorts of syntax/tutorials online and can't seem to get my head around it. This is what I have so far... but I've commented out the xpath as it's obviously wrong.
<?php
$xml=simplexml_load_file("example.xml") or die("Error: Cannot create object");
foreach($xml->yearlist->children() as $years) {
$xyid=$years[yid];
echo "_____________________________________________<br>";
echo "(yid= " . $xyid . " )<br>";
echo "SEASON: " . $years->yearname . "<br>";
echo "_____________________________________________<br>";
foreach($xml->eplist->children() as $episodes) {
echo "EPISODE TITLE: " . $episodes->eptitle . "<br>";
echo "EPISODE NUMBER: " . $episodes->eptnumber . "<br>";
$xyearid=$episodes[yearid];
echo "(yearid= " . $xyearid . " )<br>";
// echo $xml->xpath('//year[@yid="$episodes[yearid]"]/yearname');
echo "</p>";
}
}
?>
I hope you can guide me in the right direction!
Thanks Sam
Thanks for the help michi - that's definitely a step in the right direction!
I'm trying to think of ways to only display the season name once... came across iterations and arrays but they all look too complicated for me. Is it possible to include xpath within a foreach command? I thought perhaps if I nested foreach episodes within foreach seasons and used xpath to match the ID it could work, but I can't seem to get it to show the elements. Am I on the right track?
<?php
$xml=simplexml_load_file("example.xml") or die("Error: Cannot create object");
foreach ($xml->yearlist->year as $season) {
echo "SEASON: " . $season->yearname . PHP_EOL;
foreach ($xml->xpath("//ep[@yearid='$season[yid]']")[0] as $episode) {
echo "EPISODE TITLE: " . $episode->eptitle . PHP_EOL;
echo "EPISODE NUMBER: " . $episode->eptnumber . PHP_EOL;
echo PHP_EOL;
}
}
?>
Thanks again!