what is it? w3c standard for navigating through an xml document filtering finding information...

12
CM0665 WEB APPLICATIONS INTEGRATION USING XPATH TO NAVIGATE & FILTER XML (VIA SIMPLEXML)

Upload: sharleen-bell

Post on 18-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

What are they? (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 [ ]

TRANSCRIPT

Page 1: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

CM0665 WEB APPLICATIONS INTEGRATIONUSING XPATH TO NAVIGATE & FILTER XML (VIA SIMPLEXML)

Page 2: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

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)• Also used in XSLT for xml transformation

Page 3: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

XPATH EXPRESSIONS• What are they?

• (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 [ ]

Page 4: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

EXAMPLE XML DOCUMENT

<?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>

student.xml

Root node

Child of root node

Children of student

Page 5: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

EXAMPLES: XPATH EXPRESSIONS• What would be the path expression to

navigate to the startYear node?

• What would be the path expression to select the startYear node where the startYear = 2002?

‘/students/student/startYear’

‘/students/student/startYear[. = “2002”]’

--- OR ---

‘/students/student[startYear = “2002”]/startYear’

www.w3schools.com/xpath/xpath_intro.asphas more details on & examples of path expressions

Page 6: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

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.asphas more details on & examples of path expressions

Page 7: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

XPATH & SIMPLEXML EXAMPLEThe 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.

Fiona MurrayM828827

……

Ann DevonM928493

NameStudent Code

Page 8: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

XPATH & SIMPLEXML – PHP CODE

Steps involved in this example:1. Create an instance of a simpleXML class by loading the

XML file

2. Construct the query expression

3. Call the XPath method to execute the query

4. Loop through the result set

Page 9: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

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);

Page 10: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

STEP 4: LOOP THROUGH QUERY RESULT SET

// iterate through the students returned by the xpath queryforeach ($students as $student) {

}

echo " {$student->studentCode} \n"; echo " {$student->forename} "; echo "{$student->surname} \n";

echo "<tr>\n"; echo "<td>{$student->studentCode}</td>\n"; echo "<td>{$student->forename} "; echo "{$student->surname}</td>\n"; echo "</tr>\n";

echo "<table border=\"1\">\n";echo "<tr><td>Student Code</td><td>Name</td></tr>\n";

echo "</table>\n";

Page 11: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

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

Page 12: What is it? w3c standard for Navigating through an XML document Filtering  finding information Includes Expressions for navigating to parts of an

RECOMMENDED READINGhttp://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*