Simple Xpath extraction 2 with PHP 5

This example grabs an XML file, extracts certain elements (i.e. exercice elements) and then will again apply an Xpath expression to the collected results that will extract "title" elements. It will not work with php 4.x or PHP 5 without XML support installed. So this makes use of: Here is the kind of input this example can deal with:
 <student>
  <exercises>
     <exercise id="" creation-day="28" creation-month="11" creation-year="02" mod-day="" mod-month="" mod-year="" mod-by="" version="" status="draft">

       <staf no="14" ex-number=" 1a "/>
       <title>Home page personnelle</title>
       <url>http://tecfa.unige.ch/perso/staf/lattion/welcome.html</url>
       <description>créer sa homepage en xhtml</description>
       <report report-url="http://tecfa.unige.ch/staf/staf-i/lattion/staf14/ex1/comment.html"/>
     </exercise>
 .......
 <exercises>
<student>
Below is the result generated by php for the following Xpath meaning get all exercise elements (nodes) whose staf child elements have the "no='14'" attribute:
//student/exercises/exercise/staf[@no='14']/..
alternative notation (not used here): "//student/exercises/exercise/staf[@no='14']/parent::*"
To the resulting DOM node collection we apply the following Xpath
 "title"
Of course we also could have used an other DOM function to get at the contents of a simple element...

The source (simple-xpath2.phps or simple-xpath2.text is in this same directory. It is freeware :)

See simple-xpath3.php for still nicer looking output that will use XSLT to render the found fragments or go go back.



D.K.S.
Last modified: Fri Jan 21 12:30:15 MET 2005