"The Rubiayat of Omar Khayyam - Edward FitzGerald", "tyger"=>"The Tyger - William Blake", "leda"=>"Leda and the Swan - W.B. Yeats", "sorrow"=>"The Sorrow of Love - W.B. Yeats"); //Defines an associative array linking poem keys to appropriate file paths. $poemfiledict = array("rubiayat"=>"http://www.brianjaystanley.com/LIS/apps/rubiayat.xml", "tyger"=>"http://www.brianjaystanley.com/LIS/apps/tyger.xml", "leda"=>"http://www.brianjaystanley.com/LIS/apps/leda.xml", "sorrow"=>"http://www.brianjaystanley.com/LIS/apps/sorrow.xml"); //Defines an associative array linking poem keys to default versions. $poemversiondict = array("rubiayat"=>"4th edition", "tyger"=>"Published version", "leda"=>"Published version", "sorrow"=>"Final printed version"); //END DICTIONARIES--------------------------------------------------------------------------- //GET ARGUMENTS----------------------------------------------------------------------------- //if no poem variable is supplied (that is, on initial page load), set defaults; //otherwise use supplied parameter if (! isset($_GET["poem"])) { $poem = "rubiayat"; } else { $poem = $_GET["poem"]; } //if no version variable is supplied, set defaults; //otherwise use supplied parameter if (! isset($_GET["version"])) { $version = $poemversiondict[$poem]; //get the default version from the dictionary. } else { $version = $_GET["version"]; } $file = $poemfiledict[$poem]; //retrieve the appropriate filepath from the dictionary. //END GET ARGUMENTS-------------------------------------------------------------------------- //START FUNCTIONS----------------------------------------------------------------------------- //This function creates an array listing each version of the document except the current version. function listVersions($file,$version){ $reader = new XMLReader(); //initialize reader $reader->open($file); //open file $versions = array(); while ($reader->read()) { if ($reader->nodeType == XMLReader::ELEMENT && $reader->localName == "docEdition") { $reader->read(); if ($reader->value != $version) { $versions[] = $reader->value; } } } return $versions; } //this function displays the requested version function displayVersion($poemnamedict,$file,$poem,$version) { $reader = new XMLReader(); //initialize reader $reader->open($file); //open file //I expected the following line to convert XML entities, but it did not. Instead I //use PHP's utf8_decode function on each return value where entities might be encountered. // $reader->setParserProperty(XMLReader::SUBST_ENTITIES, TRUE); $versions = listVersions($file,$version); //move forward through the document; look for title and author element nodes, and begin //creating HTML while ($reader->read()) { if ($reader->nodeType == XMLReader::ELEMENT && $reader->localName == "title") { $reader->read(); $title = $reader->value;} if ($reader->nodeType == XMLReader::ELEMENT && $reader->localName == "author") { $reader->read(); $author = $reader->value; //output document head, navigation links, and banner. echo ' Poetry Viewer - ' . utf8_decode($title) . '
Books Banner
'; //output choosepoem form, and poem title and author echo '
Choose poem:

' . utf8_decode($title) . '

by ' . utf8_decode($author) . '

';} //look for docEdition element node, move forward to its text node, see //if this is the right version, and if so print version and date if ($reader->nodeType == XMLReader::ELEMENT && $reader->localName == "docEdition") { $reader->read(); if ($reader->value == $version) { echo "

" . $reader->value . "

"; //print edition $reader->next("docDate"); $reader->read(); echo '

Date: ' . $reader->value . '

'; //output forms for version choice and highlighting option. echo '
Switch edition:
     
Highlight changes
'; //start moving forward again; look for lg element node, grab its n //attribute value, and start building div boxes while ($reader->read()) { if ($reader->nodeType == XMLReader::ELEMENT && $reader->localName == "lg") { $stanzaID = $reader->getAttribute("n"); echo '
'; //print stanza header and lines; at lg end tag, create div box //for comparison stanzas, add javascript function call, and break //loop while ($reader->read()) { if ($reader->nodeType == XMLReader::ELEMENT && $reader->localName == "head") { $reader->read(); echo '
' . $reader->value . "
";} elseif ($reader->nodeType == XMLReader::ELEMENT && $reader->localName == "l") { $reader->read(); echo utf8_decode($reader->value) . "
"; } elseif ($reader->nodeType == XMLReader::END_ELEMENT && $reader->localName == "lg") { echo '
'; break; } } } //break loop at text end tag (signaling end of version) elseif ($reader->nodeType == XMLReader::END_ELEMENT && $reader->localName == "text") { break; } } } } } echo "
"; } //END FUNCTIONS-------------------------------------------------------------------------------- displayVersion($poemnamedict,$file,$poem,$version); ?>