1 / 12

CG0119 Web Database Systems Using XPath to Navigate & Filter XML (via SimpleXML) ‏

CG0119 Web Database Systems Using XPath to Navigate & Filter XML (via SimpleXML) ‏. XPath. What is it? w3c standard for… Navigating through an XML document Filtering & finding information Includes… Expressions for navigating to parts of an XML document

garan
Download Presentation

CG0119 Web Database Systems Using XPath to Navigate & Filter XML (via SimpleXML) ‏

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CG0119 Web Database SystemsUsing XPath to Navigate & Filter XML (via SimpleXML)‏

  2. XPath • What is it? • w3c standard for… • Navigating through an XML document • Filtering & finding information • Includes… • Expressions for navigating to parts of an XML document • The standard includes functions (e.g. for string & date manipulation)‏ • Used in XSLT – we used some XPath path expressions last week…!!!

  3. XPath Expressions • What are they? • Path expressions are a means of selecting nodes (or sets of nodes) from an XML document • They consist of… • A path to the nodes/node sets • A predicate (optional)‏ • Syntax • Without a predicate: • Nodenames, separated by a forward slash (/)‏ • Predicates: • Predicates are placed inside [ ]

  4. Root node Child of root node Example xml document student.xml <?xml version="1.0"?> <students> <student> <studentCode>P283746</studentCode> <forename>Anabela</forename> <surname>Domingues</surname> <studyTypeID>P</studyTypeID> <startYear>2004</startYear> </student> <student> <studentCode>M928493</studentCode> <forename>Ann</forename> <surname>Devon</surname> <studyTypeID>U</studyTypeID> <startYear>2002</startYear> </student> ... </students> Children of student

  5. Examples: XPath Expressions • What would be the path expression to navigate to the startYear node? ‘/students/student/startYear’ • What would be the path expression to select the startYear node where the startYear = 2002? ‘/students/student/startYear[. = “2002”]’ --- OR --- ‘/students/student[startYear = “2002”]/startYear’ www.w3schools.com/xpath/xpath_intro.asp has more details on & examples of path expressions

  6. Example: Multiple Predicates • What would be the path expression to select student nodes where the startYear = 2002 and the studyTypeIdD= U? '/students/student[startYear=2003][studyTypeID = "U"]' www.w3schools.com/xpath/xpath_intro.asp has more details on & examples of path expressions

  7. Name Student Code Ann Devon M928493 … … Fiona Murray M828827 XPath & simpleXML Example The aim of the example used in the following slides is to list data in a XHTML table for all students who started at University in 2002 e.g.

  8. XPath & simpleXML – PHP Code Steps involved in this example: • Create an instance of a simpleXML class by loading the XML file • Construct the query expression • Call the XPath method to execute the query • Loop through the result set

  9. Steps 1 to 3: simpleXML & XPath Objects // Create a new simplexml instance loading xml file $studentsXML = simplexml_load_file('student.xml'); // Construct an XPath expression, // including a predicate in this instance. $qry = '/students/student[startYear = "2002"] '; // call the simplexml xpath method $students = $studentsXML->xpath($qry);

  10. echo "<table border=\"1\">\n"; echo "<tr><td>Student Code</td><td>Name</td></tr>\n"; echo "<tr>\n"; echo "<td>{$student->studentCode}</td>\n"; echo "<td>{$student->forename} "; echo "{$student->surname}</td>\n"; echo "</tr>\n"; echo "</table>\n"; Step 4: Loop Through Query Result Set // iterate through the students returned by the xpath query foreach ($students as $student) { } echo " {$student->studentCode} \n"; echo " {$student->forename} "; echo "{$student->surname} \n";

  11. Summary • XPath is a w3c standard for • Navigating through an XML document • Filtering & finding information • Path expressions navigate to a particular node or node-set and they may include predicates

  12. Recommended Reading http://www.zvon.org/xxl/XPathTutorial/General/examples.html - Xpath tutorial http://www.rpbourret.com/xml/XPathIn5.htm - XPath in 5 paragraphs! http://www.w3schools.com/xpath/ - w3schools XPath tutorial & reference *

More Related