0% found this document useful (0 votes)
22 views

XML Update

The document discusses XML and its components like elements, attributes, entities and DTD. It also explains XML schema which is an alternative to DTD and provides examples of converting a simple XML document to DTD and schema definitions.

Uploaded by

Akshay Ar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

XML Update

The document discusses XML and its components like elements, attributes, entities and DTD. It also explains XML schema which is an alternative to DTD and provides examples of converting a simple XML document to DTD and schema definitions.

Uploaded by

Akshay Ar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

 Define XML

Answer: It is a meta mark up language that specifies the rule


for creating a mark-up language. It is used to define user
defined tags
Syntax : <?xml version =”1.0”>
<plane>
<make> abc </make>
<model> m1 </model>
</plane>
The XML language is a way to structure data for sharing
across websites.
Several web technologies like RSS Feeds and Podcasts are
written in XML.
XML is easy to create.
To read and update, create and manipulate an XML
document, you will need an XML parser.

<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 SimpleXML - Get Node Values of Specific Elements

<?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 SimpleXML - Get Node Values - Loop

<?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>";
}
?>

PHP SimpleXML - Get Attribute Values


<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
echo $xml->book[0]['category'] . "<br>";
echo $xml->book[1]->title['lang'];
Define DTD. List and example 2 types of DTD with an
example
Answer : It stands for Document Type Definitions. It is a set
of structural rules called declarations that specifies a set of
elements in the document

Syntax:<!ELEMENT element_name(list of child elements)>


<! ELEMENT plane( make. Model, year)>
<! ELEMENT make (#PCDATA)>
<! ELEMENT model (#PCDATA)>

2 Types of DTD: Internal DTD and External DTD


Internal DTD: DTD embedded in XML document
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Plane [
<!ELEMENT Plane (make, model)>
<!ELEMENT make (#PCDATA)>
<!ELEMENT model (#PCDATA)>
]>
<plane>
<make> abc </make>
<model> m1 </model>
</plane>
External DTD: DTD stored as a separate file and called in
the
XML document

<?xml version="1.0" encoding="UTF-8" ?>


<!DOCTYPE address SYSTEM "Plane.dtd">
<plane>
<make> abc </make>
<model> m1 </model>
</plane>

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>

 Explain building blocks of XML document /DTD


Answer : Elements. Attributes. Entities
<books>
  <subject category="Management">
    <title>SCM</title>
    <author>Rajesh</author>
    <year>2005</year>
    <price>500</price>
  </subject>
  <sub category="programming">
    <title>PERL</title>
    <author>Larry wall </author>
    <year>2003</year>
    <price>700</price>
  </subject>
</books>
Elements: books and subject
Syntax; :<!ELEMENT element_name(list of child elements)>
<!ELEMENT books(subject, title, author, year, price)

Attributes : category="Management/Programming”
<!ATTLIST element_name attribute_name attribute
_type[default-value]>

<!ATTLIST books category CDATA #FIXED=”management”>

Entities
<!ENTITY entity-name "entity-value">
<!ENTITY SCM "Supply chain Management.">

XML example:

<author>&writer;&copyright;</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>

XML Schema has a lot of built-in data types. The most


common types are:
 xs:string
 xs:decimal
 xs:integer
 xs:boolean
 xs:date
 xs:time
Example

 Write short notes on XML processor and web services


Answer
XML Processor
 It is used for XML document transformation and
presentations
 It has 3 standards XSLT, XPath, XSL
 XSLT-to Transform xml documents to different forms
 XPath : for querying the expressions and identifying
specific parts in the xml document
 XSL- output of the application
 Xml PROCESSOR takes the input from xml document
this input will be given as a data to the XSLT and them
merged to produce XSL document

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>

 XML programs WITH CSS AND XSLT


XSLT
<?xml Version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="xml_3.xsl" ?>
<student>
<class>
<name>sid</name>
<usn>1</usn>
<branch>mca</branch>
<college>bmsit</college>
<sem>sem-1</sem>
</class>
<class>
<name>swat</name>
<usn>2</usn>
<branch>cse</branch>
<college>pesit</college>
<sem>sem-2</sem>
</class>
<class>
<name>subh</name>
<usn>3</usn>
<branch>be</branch>
<college>rnsit</college>
<sem>sem-3</sem>
</class>
</student>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>STUDENT DETAILS</h2><br><br>
<table border="2" bordercolor="yellow">
<tr bgcolor="red">
<th>NAME</th>
<th>USN</th><th>BRANCH</th>
<th>COLLEGE</th><th>SEM</th>
</tr>
<xsl:for-each select="student/class">
<tr>
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="usn" /></td>
<td><xsl:value-of select="branch" /></td>
<td><xsl:value-of select="college" /></td>
<td><xsl:value-of select="sem" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

You might also like