XML Update
XML Update
<note>
<to>XYZ</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>INFORMATION</body>
</note>
<!DOCTYPE html>
<html>
<body>
<?php
$xml=simplexml_load_file("note.xml") or die("Error: Cannot create object");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
</body>
</html>
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en-us">XQuery Kick Start</title>
<author>James McGovern</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en-us">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
echo $xml->book[0]->title . "<br>";
echo $xml->book[1]->title;
?>
<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
foreach($xml->children() as $books) {
echo $books->title . ", ";
echo $books->author . ", ";
echo $books->year . ", ";
echo $books->price . "<br>";
}
?>
Plane.dtd
<!ELEMENT Plane (make, model)>
<!ELEMENT make (#PCDATA)>
<!ELEMENT model (#PCDATA)>
Drawbacks of dtd
1: Since dtd syntax if unrelated to xml, analyzing with xml
processor is difficult
2: Lot of restriction on the data content
3:Since data types are limited, representation of numeric
values is difficult
Solution is xml schema
An XML Schema describes the structure of an XML
document.
An xml schema is an xml document which is parsed with xml
parser and it supports more
data types than DTD’S
Xml schema allows nay DTD to be automatically converted to
equivalent xml schema
Purpose
1) specifies structure of its instance xml documents
2) specifies data type of every element and attribute
Defining schema
Schemas are written by using collection of names
Typical name :http://www.w3.org/2001/XMLSchema
Names can be : element ,schema, sequence , string
Xmlns: xsd = “ http://www.w3.org/2001/XMLSchema”
Define namespace. Write an example
Answer:It is the collection of elements and attribute names
used in the XML document. The name of the namespace will
be in URI
Syntax: <element_name xmlns[:PREFIX]=URI>
Default namespace:
<t xmlns:html=http://www.w3.org/TR/html4/>
<root>
<h:table xmlns:h="URI/">
<h:tr>
<h:td>FRUIT -1</h:td>
<h:td>FRUIT -2</h:td>
</h:tr> </h:table>
<f:table xmlns:f="uri/">
<h:tr>
<h:td>VEG -1</h:td>
<h:td>VEG -2</h:td>
</h:tr></f:table>
</root>
Attributes : category="Management/Programming”
<!ATTLIST element_name attribute_name attribute
_type[default-value]>
Entities
<!ENTITY entity-name "entity-value">
<!ENTITY SCM "Supply chain Management.">
XML example:
<author>&writer;©right;</author>
Define XML Schema. Write an example
Answer : It defines the structure and contents of the xml
document
It is an alternative to DTD
BCOZ: Cannot be analysed with XML processor
Very rigid in structure
Only few data types are supported
Example
<xsd :simpleType name=”plane”>
<xsd :sequence >
<xsd : element name=”make” type =”xsd: string”/>
<xsd : element name=”model” type =”xsd: string”/>
<xsd : element name=”make” type =”xsd: string”/>
<xsd : element name=”location” type =”xsd: string”/>
<xsd : element name=”year” type =”xsd: decimal”/>
<xsd :sequence >
</xsd :simpleType>
XSLT
document
XSLT XSL
PROCESSOR document
Conversion to xml , dtd and xml schema
Simple XML
<? Xml version =”1.0” encoding =“utf-8”>
<plane>
<make> abc </make>
<model> m1 </model>
<location>
<city> sa </city> <area> vp </area.
</location>
<year> 1998 </year>
</plane>
DTD
<! ELEMENT plane( make. Model, year)>
<! ELEMENT make (#PCDATA)>
<! ELEMENT model (#PCDATA)>
<! ELEMENT location (city, area)>
<! ELEMENT year (#PCDATA)>
Schema
<xsd :simpleType name=”plane”>
<xsd :sequence >
<xsd : element name=”make” type =”xsd: string”/>
<xsd : element name=”model” type =”xsd: string”/>
<xsd : element name=”make” type =”xsd: string”/>
<xsd : element name=”location” type =”xsd: string”/>
<xsd : element name=”year” type =”xsd: decimal”/>
<xsd :sequence > </xsd :simpleType>
DTD
<! ATTLIST airplane places CDATA “4”>
<! ATTLIST airplane places ENGINE_TYPE CDATA =” JET“
#REQUIRED>
XML
<airplane places =”4” ENGINE_TYPE =”JET”> </airplane>