CSE2045Y Web Application Development: XML, XSD, XSLT With PHP
CSE2045Y Web Application Development: XML, XSD, XSLT With PHP
Lecture 8
XML, XSD, XSLT with PHP
1
Agenda
• XML, XSD, XSLT and PHP
• Validate XML against Schema in PHP
• Accessing XML nodes from PHP
– Accessing nodes using SimpleXML
• Transform XML in PHP
– Stylesheet
– DOM Document
• XML DOM and JavaScript
– Sample scenarios
2
Recap
• XML is used to transfer data.
– e.g. From one business partner to another.
• Possible Scenario:
1. Data from Organisation1 sent to Organisation2.
2. Organisation2 needs to process the data either by
displaying it in appropriate format or adding it to its own
database.
4
Possible Scenario
1. XML Data from Organisation1 sent to Organisation2.
<?xml version=“1.0”?>
<modules>
<module>
<modulecode>CSE2045y</modulecode>
<moduledesc>Web Application Development</moduledesc>
</module>
<module>
<modulecode>CSE1240</modulecode>
<moduledesc>Database Systems</moduledesc>
</module>
</modules>
5
Possible Scenario
2. Suppose Organisation2 wants to insert the
data into its own database.
– However, Organisation2 needs to validate the data
against a Schema before the database operation.
6
Schema (modules.xsd)
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="modules">
<xs:complexType>
<xs:sequence>
<xs:element name="module" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="modulecode" type="xs:string"/>
<xs:element name="moduledesc" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema> 7
Validate XML against Schema in PHP
• We need to validate the XML document against the
Schema in PHP:
$xml_Doc = new DOMDocument();
$xml_Doc->loadXML($modules);
if(!$xml_Doc->schemaValidate('modules.xsd'))
echo "Your xml is NOT valid";
if (!mysql_query($sql_insert,$con)){
die('Error: ' . mysql_error());}
}//end if
}//end if
}//end foreach
13
Transform XML in PHP
Option 1
• Apply a stylesheet (XSLT) to the resulting XML
string and output the XML to the browser.
//Build an XML to display the modules.
header('Content-Type: text/xml');
$xml_output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
$xml_output=$xml_output."<?xml-stylesheet type=\"text/xsl\"
href=\"modules.xsl\"?>";
$xml_output = $xml_output.$modules;
echo $xml_output;
echo $proc->transformToXML($xml_Doc);
<cd>
<title>Electric Ladyland</title>
<artist>Jimi Hendrix</artist>
<year>1997</year>
</cd>
</collection>
16
Activity 1
• The corresponding xslt file, collection.xsl, is as
follows:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="owner" select="'Nicolas Eliaszewicz'"/>
<xsl:template match="collection">
Hey! Welcome to <xsl:value-of select="$owner"/>'s sweet CD
collection!
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="cd">
<h1><xsl:value-of select="title/text()"/></h1>
<h2>by <xsl:value-of select="artist/text()"/> - <xsl:value-of
select="year/text()"/></h2>
<hr />
</xsl:template>
</xsl:stylesheet> 17
Activity 1
• The xslt file will display the data in the
browser:
18
Activity 1
a) Write the xsd file, collection.xsd.
19
XML DOM and JavaScript
• Scenario 1:
– The XML is being built at a browser level using DOM.
– Step 1: An XML string with only the root is created.
– Step 2: This XML string is converted into an XML document
object using the function loadXMLString(txt).
– Step 3: Data is added to the XML by calling addTableRow( )
which in turn calls insertModule( ).
– The display is recomputed by traversing the XML
document and displaying the different nodes in a table.
– Step 4: When the user decides to submit the data, the
XML document is serialized into a string by the function
encodeItems() and the string is placed in a hidden field
and sent to the server for processing.
20
XML DOM and JavaScript
• Important DOM functions used in the example
are:
– createElement
– appendChild
– createTextNode
– childNodes[ ]
– firstChild
– nodeValue
21
Scenario 1 Explained (HTML file)
<head><script> … JavaScript goes here … </script></head>
<body>
<form action=addmodulexml.php method=post onsubmit="return encodeItems()">
<table id=table_modules>
<tr>
<th width=200>Module Code</th>
<th width=200>Module Name</th>
<th>
</tr>
<tr>
<td><input type=text name=txt_modulecode></td>
<td><input type=text name=txt_moduledesc></td>
<td><button onclick="return addTableRow()">Add Module</button></td>
</tr>
</table>
<br><br>
<div id="div_modules"></div>
<input type=submit value=submit>
<input type=hidden name=txt_xml_modules id=txt_xml_modules>
</form>
</body>
</html> 22
Scenario 1 Explained
(Create XML Document)
• Step 1: An XML string with only the root is
created.
var xml_modules= "<modules></modules>";
23
Scenario 1 Explained
(loadXMLString function)
function loadXMLString(txt)
{
var xmlDoc;
insertModule(modulecode, moduledesc);
<module>
<module> childNodes[1]
<modulecode> childNodes[0]
CSE1041 firstChild.nodevalue OR childNodes[0].nodevalue
</modulecode>
<moduledesc> childNodes[1]
Web Tech 1 firstChild.value OR childNodes[0].nodevalue
</moduledesc>
</module>
</modules> 28
Scenario 1 Explained
(encodeItems & getBrowser functions)
function getBrowser()
function encodeItems()
{
{
var sbrowser=navigator.userAgent;
var browser=getBrowser();
if (sbrowser.toLowerCase().indexOf('msie')>-1)
var xmlstring;
return "ie";
if (browser=="ie")
if (sbrowser.toLowerCase().indexOf('firefox')>-
{
1)
xmlstring=xmlDoc_modules.xml;
return "ff";
alert(xmlstring);
return "";
document.forms[0].txt_xml_modules.value= xmlstring;
}//end function getBrowser()
return true;
}
if (browser=="ff")
{
xmlstring = (new
Refer to addmodule_xml.html
XMLSerializer()).serializeToString(xmlDoc_modules); for the complete set of
document.forms[0].txt_xml_modules.value= xmlstring; JavaScript codes.
alert(xmlstring);
return true;
}
return false;
}//end function encode_Items()
29
XML DOM and JavaScript
• Scenario 2:
– Allow the user to delete nodes from the XML.
document, thus re-computing the display.
– The delete button is added for each row and the
deleteNode parameter corresponds to the
appropriate module node in the XML document.
<button id="+i+" onclick=deleteNode(this)>Delete</button>
– On clicking on the delete Button, the deleteNode()
function is invoked.
• A reference is created to the appropriate node and the latter
deleted using removeChild().
• Refer to addmodule_xmldelete.html for the complete set of
codes. 30
XML DOM and JavaScript
• Scenario 3:
– Allowing the user to choose an entry and update it.
– Use a global variable global_update_mode to keep track of
whether we are in insert or update mode.
– Codes that add a button next to each row:
<button id="+i+" onclick='return
retrieveNode(this)'>Update</button>
– retrieveNode() sets the update mode, gets the appropriate
values and places them in the text fields.
– updateModule() changes the appropriate node values to
the new values and changes the update mode to false.
• Refer to addmodule_xmldeleteupdate.html for the
complete set of codes. 31
References
• Adapted from XML, XSD and XSLT in Action
with PHP and JavaScript Part 1 & 2, by Mr
Anwar Chutoo.
32