Images

XML - PART II


XML
The eXtensible Markup Language (XML) is a general-purpose markup language. Its primary purpose is to facilitate the sharing of data across different information systems, particularly via the Internet. It is a simplified subset of the Standard Generalized Markup Language (SGML), and is designed to be relatively human-legible.Tally 8.1 logo
By adding semantic constraints, application languages can be implemented in XML. These include XHTMLRSSMathML,GraphMLScalable Vector Graphics(SVG), MusicXML, and thousands of others.
Moreover, XML is sometimes used as the specification language for such application languages.
What makes XML truly powerful is the acceptance and hard work done by all those who work with databases, programming, office application, etc.
It is because of this hard work that the tools exist to do these conversions from whatever platform into standardized XML data or convert XML into a format used by that platform.


XML Formatting
Formatting XML with CSS
Until there is a style sheet associated (whether CSS or XSLInternet Explorer andMozilla Firefox display tree structure of XML file as shown below:
XML tree structure
It is possible to format XML file to format with CSS as we format HTML files. We can write style definitions for XML in the same way we write stylesheet for HTML.
Below is an example of how to use a CSS style sheet to format an XML document:
<?xml version="1.0" encoding="ISO-8859-1"?>

<?xml-stylesheet type="text/css" href="xml_css.css"?>

<!-- DTD declration -->
<!DOCTYPE tutorial [
<!ELEMENT ebiz (tutorial+,info)>
<!ELEMENT tutorial (category,title,author,url,launch_date)>
<!ATTLIST tutorial category (cc|ll|ul) #REQUIRED>
<!ELEMENT category (description?,sub_category?)>
<!ELEMENT launch_date (date,month,year)>
<!ENTITY developer "xyz@ebizel.com">
<!ENTITY copyright "eBIZ.com, All right Reserved, 2007">
]>

<ebiz>
<tutorial category="cc">
  <category>
    <description>Computer Courses</description>
    <sub_category>Tutorial group: Scripting Language</sub_category>
  </category>
  <title>Tutorial Name :XML</title>
  <author></author>
  <url>URL :- http://education.ebizel.com/html/xml/</url>
  <launch_date>
    <date>25</date>
    <month>April</month>
    <year>2007</year>
  </launch_date> </tutorial>
<info>Author: &developer; <br />©right; </info>
</ebiz>
The second line, <?xml-stylesheet type="text/css" href="xml_css.css?>, links the XML file to the CSS file, that containts the following code:
/* CSS to format XML document*/
ebiz
{
    border: #000099 thin solid;
    width:500;
    height:400;
/*background-color:cyan;*/
    margin-left:0;
    margin-top:0;
    margin-right:0;
    margin-bottom:0;
}

tutorial
{
    display:block;
    margin-left:0 px;
    width:500;
    height:150;
    background-color:pink
}

category
{
    display:block;
    margin-left:12;
    font-family:"Courier New", Courier, monospace;
    color:#0000FF;
}

description
{
    display:block;
    margin-left:12;
    font-weight:400;
    color:#9933FF
}

title
{
    display:block;
    font-weight:bolder;
    margin-left:16;
}
url
{
    text-decoration:underline;
    color:blue;
    display:inline;
    margin-left:16;
}

sub_category
{
    display:inline;
    margin-left:20;
    font-weight:bolder;
    color:red;
    margin-left:16;
}

br
{
    display:block;
}
info
{
    display:block;
    margin-left:25;
    width:300;
    border:#996600 medium solid;
    font-style:italic;
    margin-top:200;
    padding-top:2;
}

launch_date
{
    display:block;
    margin-left:16;
}
Click here to view the XML file without formatting & Click here to view the XML file with CSS formatting applied. To view the CSS file click here .
XML file with CSS formatting.
Even if it looks right to use CSS this way, I strongly believe that formatting with XSL will be the (de-facto) standard way to format XML in future or as soon as the main browsers support it.

XML Formatting
Formatting XML with XSL
What is XSL?
The EXtensible Stylesheet Language (XSL) includes both a formatting language and a transformation language. Each of these, naturally enough, is an XML application. The transformation language provides elements that define rules for how one XML document is transformed into another XML document. The transformed XML document may use the markup and DTD of the original document or it may use a completely different set of elements. In particular, it may use the elements defined by the second part of XSL, the formatting objects. XSL like CSS can be used to format XML document and define how the XML document should look.
XSL is a combination of two languages used as a single language, it's not one language. The first language is a transformation language, the second a formatting language. Its ability to move data from one XML representation to another makes it an important component of XML-based electronic commerce, electronic data interchange (B2B exchange etc), metadata exchange, and any application that needs to convert between different XML representations of the same data. These uses are also united by their lack of concern with interpretation of data on a display for humans to read. They are purely about moving data from one computer system or program to another.
The transformation and formatting halves of XSL can function independently of each other. For instance, the transformation language can transform an XML document into a well-formed HTML file, and completely ignore XSL formatting objects.
A sample XSL file :
Source code of “xsl_tutorial.xsl “
<?xml version="1.0" encoding="iso-8859-1"?>

    <!DOCTYPE xsl:stylesheet [
    <!ENTITY nbsp " ">
    <!ENTITY copy "©">
    <!ENTITY reg "®">
    <!ENTITY trade "™">
    <!ENTITY mdash "—">
    <!ENTITY ldquo "“">
    <!ENTITY rdquo "”">
    <!ENTITY pound "£">
    <!ENTITY yen "¥">
    <!ENTITY euro "€">
]>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title></title>
</head>
<body style="background-color: #00CCFF; font:'Courier New', Courier, monospace; ">
    <xsl:for-each select="ebiz/tutorial">
        <div style=" margin-left:-4px; border:#00CCCC thin dotted; width:400; m
argin-top:4">
         <span style=" font-weight:bolder; color:#FFFFFF;padding:8px">
          <span style="font-style:italic"> Cateogory of tutorial :</span>
         <xsl:value-of select="category" />
    <br />

Sub-Category: <span style="padding-left:15; color:#990000"><xsl:value-of select="sub_category" />
<br />

<b>Name of Tutorial:
<span style="color:#006699;">
<xsl:value-of select="title" /></span>
<br />
</b>
</span>
<br />
Author :<span style="color:#006699;">
<xsl:value-of select="author" />
</span>
<br />
Date of Lanunch : <span style="color:#006699;">
<xsl:value-of select="launch_date" />
</span>
</span>
</div>
<xsl:for-each select="ebiz">
<xsl:value-of select="info" /> </xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Click here to view the file .
Source code of “xml_without_xsl.xml”
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE tutorial [
<!ELEMENT ebiz (tutorial?,info)>
<!ELEMENT tutorial (category,sub_category?,title,author,url,launch_date)>
        <!ATTLIST tutorial category (cc|ll|ul) #REQUIRED>
    <!ENTITY developer "xyz@ebizel.com">
    <!ENTITY developer1 "abc@ebizel.com">
    <!ENTITY developer2 "xxx@ebizel.com">
    <!ENTITY nbsp " ">
    <!ENTITY copyright "eBIZ.com, All right Reserved, 2007">
]>
<ebiz>
<tutorial category="cc">
    <category>Computer Courses</category>
    <sub_category> Scripting Language</sub_category>
    <title> XML</title>
    <author>&developer;</author>
    <url>http://education.ebizel.com/html/xml/</url>
    <launch_date> 25/April2007 </launch_date>

</tutorial>
<tutorial category="ll">
    <category>Language Learning</category>
    <title>French Tutorial </title>
    <author>&developer;</author>
    <url>http://education.ebizel.com/html/xml/</url>
    <launch_date> 29/April2007 </launch_date>

</tutorial>
<tutorial category="cc">
    <category>Computer Courses</category>
    <sub_category> General</sub_category>
    <title> Computer Hardware</title>
    <author>&developer2;</author>
    <url>http://education.ebizel.com/html/xml/</url>
    <launch_date> 30/April2007 </launch_date>
</tutorial>

</ebiz>
Click here to view the file .
Given below is the source of the XML file, which has been formatted using XSL. The second line,
<?xml-stylesheet type="text/xsl" href="xsl_tutorial.xsl" ?>
links the XML file to the XSL file.
Source code of xml_with_xsl.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="xsl_tutorial.xsl" ?>
<!DOCTYPE tutorial [
<!ELEMENT ebiz (tutorial?,info)>
<!ELEMENT tutorial (category,sub_category?,title,author,url,launch_date)>
     <!ATTLIST tutorial category (cc|ll|ul) #REQUIRED>

    <!ENTITY developer "xyz@ebizel.com">
    <!ENTITY developer1 "abc@ebizel.com">
    <!ENTITY developer2 "xxx@ebizel.com">
    <!ENTITY nbsp " ">
        <!ENTITY copyright "eBIZ.com, All right Reserved, 2007">
]>

<ebiz>

<tutorial category="cc">
    <category>Computer Courses</category>
    <sub_category> Scripting Language</sub_category>
    <title> XML</title>
    <author>&developer;</author>
    <url>http://education.ebizel.com/html/xml/</url>
    <launch_date> 25/April2007 </launch_date>
</tutorial>

<tutorial category="ll">
    <category>Language Learning</category>
    <title>French Tutorial </title>
    <author>&developer;</author>
    <url>http://education.ebizel.com/html/xml/</url>
    <launch_date> 29/April2007 </launch_date>
</tutorial>

<tutorial category="cc">
    <category>Computer Courses</category>
    <sub_category> General</sub_category>
    <title> Computer Hardware</title>
    <author>&developer2;</author>
    <url>http://education.ebizel.com/html/xml/</url>
    <launch_date> 30/April2007 </launch_date>
</tutorial>

</ebiz>
Click here to view the file .

XML Praser
Working with Microsoft XML Parser
What is an XML parser?
In simplest we can say that “XML Parser is the software that reads an XML document, identifies all the XML tags and passes the data to the application”. The work of XML parser is to do syntactic analysis of the given XML document.
Parsing XML Documents
To manipulate an XML document, you need an XML parser. The parser loads the document into your computer's memory. Once the document is loaded, its data can be manipulated using the DOM. The DOM treats the XML document as a tree. You can understand this tree as the XML document tree shown when you the XML document with any browser such as Firefox etc.
There are some differences between Microsoft's XML parser and the XML parser used in Mozilla and Opera browsers. In this tutorial we will show you how to create scripts that will work only in Internet Explorer browsers.
Working with Microsoft XML Parser
Microsoft's XML parser is a COM component that comes with Internet Explorer 5and higher. Once you have installed Internet Explorer, the parser is available to scripts. If you are using Internet Explorer version lower then IE 5, examples in this chapter may not work.
Microsoft's XML parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML.
Creating an instance of MS XML Parser :
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
Loading an XML document into parser:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="true"
xmlDoc.validateOnParse="true"
xmlDoc.load("simple_xml.xml")
The first line of the script above creates an instance of the XML parser. The second line turns off asynchronized loading, to make sure that the parser will not continue execution of the script before the document is fully loaded. The third line tells the parser to load an XML document called "simple_xml.xml".
XML Validation
Validating an XML file against specified DTD through MS XML Parser:
Source code of “xml_parser_validation.php”
<html>
<body>
<script language="JavaScript">
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
    xmlDoc.async="true"
    xmlDoc.validateOnParse="true"
    xmlDoc.load("simple_xml.xml")
    if(xmlDoc.parseError.errorCode<0)
    {
        document.write("<br>Error Code: ")
        document.write(xmlDoc.parseError.errorCode)
        document.write("<br>Error Reason: ")
        document.write(xmlDoc.parseError.reason)
        document.write("<br>Error Line: ")
    document.write(xmlDoc.parseError.line)
    }
    else
    {
        document.write("<B>Valid XML Document</b><br />")
    }
</script>
</body>
</html>
Source code of “simple_xml.xml”
<?xml version="1.0" encoding="UTF-7"?>
    <!DOCTYPE address SYSTEM "simple_xml_dtd.dtd">
    <address>
        <name>Dharmendra Das</name>
        <address_str>D 210, sector 11, Rohni</address_str>
        <city>New Delhi</city>
        <state>Delhi</state>
        <pin>110000</pin>
        <company_info>©©right;</company_info>
    </address>
Source code of DTD declaration:
<?xml version="1.0" encoding="UTF-7"?>
<!ELEMENT address (name,address_str+,city,state,pin,company_info?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address_str (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT pin (#PCDATA)>
<!ELEMENT company_info (#PCDATA)>

<!ENTITY copy "©">

<!ENTITY copyright "2007, eBIZ.com all rights reserved">
Click here to view the parser output. Have a look at simple_xml.xml and it’s DTD declaration.

XML Praser
Mozilla & Opera XML Parser
XML Parser in MozillaFirefoxOpera and other browser work little differently from Microsoft XML parser. If you try to open the example of the previous chapter with a browser other then the Microsoft Internet Explorer, you might not be able to see the parser output. Reason is the previous example is optimized for the Microsoft Internet Explorer.
Mozilla's XML (I've used Mozilla XML parser to refer to XML parser of Mozilla, Firefox and Opera browsers) parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML.
Creating an instance of the XML Parser in Mozilla:
var xmlDoc=document.implementation.createDocument("namespace","rootelement",null);
The first parameter, namespace, defines the namespace used for the XML document. The second parameter, rootelement , is the XML root element in the XML file. The third parameter, null, is always null because it is not implemented yet.
Loading an XML document into parser
The following code fragment loads an existing XML document ("simple_xml.xml") into Mozillas' XML parser:
var xmlDoc=document.implementation.createDocument("","address",null);

xmlDoc.load("simple_xml.xml");
The second line tells the parser to load an XML document called "simple_xml.xml".
<html>
<head>
<script type="text/javascript">
var xmlDoc;
function createParserInstance()
{
    xmlDoc=document.implementation.createDocument("","",null);
    xmlDoc.load("simple_xml.xml");
    xmlDoc.onload=loadMSG;

}
function loadMSG()
{
    document.getElementById("name").innerHTML= xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;

//document.write(xmlDoc.getElementsByTagName("name")
[0].childNodes[0].nodeValue);

    document.getElementById("address_str").innerHTML= xmlDoc.getElementsByTagName("address_str")[0].childNodes[0].nodeValue;

    document.getElementById("city").innerHTML= xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;

    document.getElementById("state").innerHTML= xmlDoc.getElementsByTagName("state")[0].childNodes[0].nodeValue;

    document.getElementById("pin").innerHTML= xmlDoc.getElementsByTagName("pin")[0].childNodes[0].nodeValue;
}
</script>
</head><body onload="javascript:createParserInstance();">
<h1>Address details of Associate</h1>
<p><b>Name </b> <span id="name"></span><br />
<b>Address </b> <span id="address_str"></span><br />
<b>City:</b> <span id="city"></span><br />
<b>State :</b><span id="state"></span><br />
<b>Pin : </b><span id="pin"></span>
</p>
</body>
</html>
Click here to view the parser output.
Note: You can see the out put only if you are using Mozilla and/or Opera web browsers.

XML Praser
XML Data island
What is Data Island?
By using the unofficial <xml> tag can embed xml data into HTML file (<xml> tag is non-standard tag and works only with Microsoft Internet Explorer) .As we know HTML is used for presentation of data and XML is used for storage of data and XML does not know anything about how to present the data.
With the help of XML data island we can load specified XML file and then display the content of XML file in HTML page.
Note: <xml> tag is unofficial and works only with Microsoft Internet Explorer and if you are using any other browser then you might not be able to see the output of following example.
Bind Data Island to HTML Elements
Example:
<html>
<body bgcolor="pink">

<xml id="address"  src="simple.xml"></xml>

<table border="1" datasrc="#address" bgcolor="pink">
<thead>
<tr bgcolor="cyan">
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Pin</th></tr>
</thead>
<tfoot>
<tr><th colspan="5">eBIZ Associate Address details</th></tr>
</tfoot>
<tbody>
<tr>
<td><span datafld="name"></span></td>
<td><i><span datafld="address_str"></span></td>
<td><span datafld="city"></span></td>
<td><span datafld="state"></span></td>
<td><span datafld="pin"></span></td>
</tr>
</tbody>
</table>
</body>
</html>
Click here to view the parser output.
Note: <xml> tag is unofficial and works only with MS IE and if you are using any other browser then you might not be able to see the output of following example.
Source code of simple.xml
<?xml version="1.0" encoding="UTF-7"?>
<ebiz>
  <address>
    <name>Dharmendra Das</name>
    <address_str>D 210</address_str>
    <city>New Delhi</city>
    <state>Delhi</state>
    <pin>110000</pin>
  </address>
  <address>
    <name>XYZ Kumar</name>
    <address_str>b 66</address_str>
    <city>Noida </city>
    <state>UP</state>
    <pin>201301</pin>
  </address>
   <address>
    <name>Sam </name>
    <address_str>Karmpura</address_str>
    <city>Noida </city>
    <state>UP</state>
    <pin>201301</pin>
  </address>
   <address>
    <name>K Kumar</name>
    <address_str>b 66</address_str>
    <city>Noida </city>
    <state>UP</state>
    <pin>201301</pin>
  </address>
  <address>
    <name>VJ</name>
    <address_str>e 66</address_str>
    <city>New Delhi </city>
    <state>Delhi</state>
    <pin>201301</pin>
  </address>
  <address>
    <name>Amu</name>
    <address_str>J 3</address_str>
    <city>Noida </city>
    <state>UP</state>
    <pin>201301</pin>
  </address>
</ebiz>
Click here to view the parser output.

XML Schema
Introduction to XML Schema
The definition of an XML document, which includes the XML tags and their interrelationships. Residing within the document itself, an XML schema may be used to verify the integrity of the content. XML schema can be treated as grammar that validates the XML document. Various recommendations for an XML schema were submitted to the W3C, and a standard was approved in May 2001 that included the ability to define data by type (date, integer, etc.).
XML Schema is a superset of DTD, which is the standard SGML schema. Unlike DTD,W3C XML Schema is written in XML syntax, which although more verbose than DTD, can be created with any XML tools. Since XML Schema is written in XML it is easy to learn, self explaning and flexible than DTD.
XML Schema Structure
Grammar based Schema language
A grammar-based schema specifies what elements may be used in an XML instance document, the order of the elements, the number of occurrences of each element, and the content and datatype of each element and attribute.
Assertion based Schema language
An assertion-based schema makes assertions about the relationships that must hold between the elements and attributes in an XML instance document.
What is XML Schemas?
XML Schema is an XML vocabulary for expressing your data's business rules
Purpose of XML Schemas
1. Specify: the structure of instance documents "this element contains these elements, which contains these other elements, etc"
2. Specify the datatype of each element/attribute "this element shall hold an integer with the range 0 to 12,000" (DTDs don't do too well with specifying datatypes like this)
3. Specify which elements are child elements  
4. Specify the order of child elements  
5. Specify the number of child elements  
6. Specify whether an element is empty or can include text  
7. WSpecify default and fixed values for elements and attributes 

XML Schema
Schema vs. DTD
Schemas make it possible to provide the full "legal" structure of an XML document and to specify type characteristics for data contained within the document. XML based schemas are very quickly emerging as one of the most important facets of the XMLrevolution. A schema is the set of rules that defines how a document is put together. Schema allows the user to check the validity of the XML document. XML Schema can be used to validate sequence, occurrence and other such constraint for an element.
Difference between Schema and DTD
A DTD is:
DTD's old and most widely used grammar for XML documents, but they were originally developed for SGML. The XML Document Type Declaration(DTD) contains or points to markup declarations that provide a grammar for a class of documents. This grammar is known as a document type definition or DTD.
The DTD can point to an external subset containing markup declarations, or can contain the markup declarations directly in an internal subset, or can even do both.  
A Schema is:
XML Schemas express shared vocabularies and allow machines to carry out rules made by people. They provide a means for defining the structure, content and semantics ofXML documents.
In summary, schemas are a richer and more powerful of describing information than what is possible with DTDs.  
DTD vs. Schema
Both Document Type Definitions (DTD's) and XML Schemas (XSD's, also known asWXS) are industry-standard ways to define XML-based data models, and you'll find many tools and utilities for working with both DTD and XML Schema.
There are many technical benefits of migrating older DTDs to XML Schema, including:
1. Support for primitive (built-in) data types (eg: xs:integerxs:stringxs:date, and so on), which facilitates using XML in conjunction with other typed-data, including relational data.
2. The ability to define custom data types, using object-oriented data modeling principles: encapsulation, inheritance, and substitution.  
3. Compatibility other XML technologies, for example, Web servicesXQueryXSLTand other technologies can optionally be schema-aware. 
Advantage of using XML Schema over DTD
XML Schemas Support Data Types. One of the greatest strength of XML Schemas is the support for data types.
XML Schemas are extensible to future additions
• XML Schemas are flexible and easy to learn in comparison to DTD
XML Schemas are extensible to future additions
XML Schemas are richer and more powerful than DTDs
XML Schemas are written in XML
XML Schemas support data types
XML Schemas support namespaces
XML Schemas are more powerful than DTD
XML Schemas are a tremendous advancement over DTDs:
• Enhanced datatypes
• 44+ versus 10
• Can create your own datatypes (this gives you flexibility to have your own datatype)
Example: "This is a new type based on the string type and elements of this type must follow this pattern: ddd-dddd, where 'd' represents a digit".
•Written in the same syntax as instance documents
• less syntax to remember
• Object-oriented'ish
• Can extend or restrict a type (derive new type definitions on the basis of old ones)
• Can express sets, i.e., can define the child elements to occur in any order
• Can specify element content as being unique (keys on content) and uniqueness within a region
• Can define multiple elements with the same name but different content
• Can define elements with nil content
• Can define substitutable elements - e.g., the "emp" element is substitutable for the "employee" element.
• XML Schemas are extensible, because they are written in XML.
• XML Schemas secures Data Communication
• Another great strength about XML Schemas is that they are written in XML.
Simple example
With DTD
Given below is an XML file that contains internal DTD declration:
<?xml version="1.0" ?>

<!DOCTYPE name
[
<!ELEMENT name (title?, fname, middle_name?, lname, suffix?) >
<!ELEMENT title (#PCDATA) >
<!ELEMENT fname (#PCDATA) >
<!ELEMENT middle_name (#PCDATA) >
<!ELEMENT lname (#PCDATA) >
<!ELEMENT suffix (#PCDATA) >
<!ENTITY uuml "ü" >
]
>
<name>
<fname>XYZ</fname>
<middle_name>Kümar</middle_name>
<lname></lname>
</name>
With Schema
Note, it seems a bit hairy when it comes to defining entities with Schema .... so I left out the &uuml; of Kümar here.
The Schema file:
<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="uuml" type="xsd:token" fixed="ü"/>

<xsd:complexType name="name">
   <xsd:sequence>
      <xsd:element name="title" type="xsd:string" minOccurs="0" maxOccurs="1" />
      <xsd:element name="fname" type="xsd:string" minOccurs="1" maxOccurs="1" />
     <xsd:element name="middle_name" type="xsd:string" minOccurs="0" maxOccurs="1" />
      <xsd:element name="lname" type="xsd:string" minOccurs="1" maxOccurs="1" />
      <xsd:element name="suffix" type="xsd:string" minOccurs="0" maxOccurs="1" />
  </xsd:sequence>
</xsd:complexType>
</xsd:schema>
The XML file:
<?xml version="1.0" encoding="ISO-8859-1" ?>

<name
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="person.xsd">
<fname>XYZ</fname>
<middle_name>Kumar</middle_name>
<lname></lname>
</name>

XML Schema
XML Schema Elements
A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes.
However, the "only text" restriction is quite misleading. The text can be of many different types. It can be one of the types included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself.
You can also add restrictions (facets) to a data type in order to limit its content, or you can require the data to match a specific pattern.
Note there are two types of XSD elements:
1. Simple
2. Complex
Here, we will be discussing simple Schema elements only.
Defining a Simple Element
The syntax for defining a simple element is:
<xs:element name="element_name" type="data_type"/>
in the above declration element_name is the name of the element you want to define and data_type is the type of data the element can contain.
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
XSL Data types
Example
<name>Dharmendra Das</name>
<address_str>D 210</address_str>
<city>New Delhi</city>
<state>Delhi</state>
<pin>110000</pin>
And here are the corresponding simple element definitions:
<xs:element name="name" type="xs:string"/>
<xs:element name="address_str" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="pin" type="xs:integer"/>

XML Schema
XML Schema Attributes
Simple elements cannot have attributes. But the attribute itself is always declared as a simple type. If an element has attributes, it is considered to be of a complex type.
Defining a simple XSD attribute
<xs:attribute name="attbr_name" type="data_type"/>
In the above declaration attbr_name is the name of the attribute and data_typespecifies the data type of the attribute.
Here is an XML element with an attribute:
<name lang=”EN”>Dharmendra Das</name>
And here is the corresponding attribute definition:
<xs:attribute name="lang" type="xs:string"/>
Restrictions on Content
When an XML element or attribute has a data type defined, it puts restrictions on the element's or attribute's content.
If an XML element is of type "xs:date" and contains a string like "Hello World", the element will not validate. 
Basic example:
<?xml version="1.0"?>
<ebiz xmlns="http://www.ebizel.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= "tutorial_attrib.xsd">
<book>
     <title>XML Tutorial</title>
     <author>R Tiwari</author>
     <date>29/04/2007</date>
     <ISBN>1-56592-235-2</ISBN>
     <publisher>eBIZ.com Publication</publisher>
</book>
<book>
     <title>JSP Tutorial</title>
     <author>A Kumar</author>
     <date>29/04/2007</date>
    <ISBN>0-440-34319-4</ISBN>
     <publisher>eBIZ.com Publication</publisher>
</book>
<book>
     <title>SQL Tutorial</title>
     <author>S Srivastava</author>
     <date> 29/04/2007</date>
     <ISBN>0-06-064831-7</ISBN>
     <publisher>eBIZ.com Publication</publisher>
</book>
</ebiz>
Click here to view the file.
XML Schema for above xml file: “tutorial_attrib.xsd”
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ebizel.com"
xmlns="http://www.ebizel.com"
elementFormDefault="qualified">
<xsd:element name="ebiz">
     <xsd:complexType>
     <xsd:sequence>
     <xsd:element ref="book" minOccurs="1" maxOccurs="unbounded"/>
     </xsd:sequence>
     </xsd:complexType>
     </xsd:element>
     <xsd:element name="Book">
<xsd:complexType>
     <xsd:sequence>
          <xsd:element ref="title" minOccurs="1" maxOccurs="1"/>
          <xsd:element ref="author" minOccurs="1" maxOccurs="1"/>
          <xsd:element ref="date" minOccurs="1" maxOccurs="1"/>
          <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/>
          <xsd:element ref="publisher" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
     <xsd:element name="title" type="xsd:string"/>
     <xsd:element name="author" type="xsd:string"/>
     <xsd:element name="date" type="xsd:data"/>
     <xsd:element name="ISBN" type="xsd:string"/>
     <xsd:element name="publisher" type="xsd:string"/>
<xsd:attributeGroup name="tutorialAttribute">
<xsd:attribute name="Category" use="required">
     <xsd:simpleType>
     <xsd:restriction base="xsd:string">
     <xsd:enumeration value="computer courses"/>
     <xsd:enumeration value="language learning"/>
     <xsd:enumeration value="utility"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
     <xsd:attribute name="isLaunched " type="xsd:boolean" default="false"/>
     <xsd:attribute name="reviewer" type="xsd:string" default=" "/>
</xsd:attributeGroup>

</xsd:schema>

XML Schema
XML Schema Facets
Restrictions(facets) are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
By using these restrictions, we can control the kind of value that can be supplied. For instance suppose eBIZ.com want to place an restriction on the age and the condition is“only those who are above 15 and below 76 can join eBIZ.com”. We can place this restriction using schema restriction.
Simple facets
<xsd:element name="age" type="xsd:integer">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="16" />
<xsd:maxInclusive value="75" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
Restrictions for Data types
ConstraintDescription
enumerationDefines a list of acceptable values
fractionDigitsSpecifies the maximum number of decimal places allowed. Must be equal to or greater than zero
lengthSpecifies the exact number of characters or list items allowed. Must be equal to or greater than zero
maxExclusiveSpecifies the upper bounds for numeric values (the value must be less than this value)
maxInclusiveSpecifies the upper bounds for numeric values (the value must be less than or equal to this value)
maxLengthSpecifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
minExclusiveSpecifies the lower bounds for numeric values (the value must be greater than this value)
minInclusiveSpecifies the lower bounds for numeric values (the value must be greater than or equal to this value)
minLengthSpecifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
patternDefines the exact sequence of characters that are acceptable
totalDigitsSpecifies the exact number of digits allowed. Must be greater than zero
whiteSpaceSpecifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
Exmple :
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ebizel.com"
xmlns="http://www.ebizel.com"
elementFormDefault="qualified">
<xsd:element name="ebiz">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="address" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="address">
<xsd:complexType>
<xsd:sequence>
     <xsd:element ref="name" minOccurs="1" maxOccurs="1"/>
     <xsd:element ref="age" minOccurs="1" maxOccurs="1" />
     <xsd:element ref="address_str" minOccurs="1" maxOccurs="1"/>
     <xsd:element ref="city" minOccurs="1" maxOccurs="1"/>
     <xsd:element ref="state" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="pin" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="xsd:integer">
<xsd:simpleType>
     <xsd:restriction base="xsd:integer">
          <xsd:minInclusive value="16" />
          <xsd:maxInclusive value="75" />
          </xsd:restriction>
</xsd:simpleType>
</xsd:element>
     <xsd:element name="address_str" type="xsd:string"/>
     <xsd:element name="city" type="xsd:string"/>
     <xsd:element name="state" type="xsd:string">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="2" />
<xsd:maxLength value="4" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="pin" type="xsd:integer">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:length value="6" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>


</xsd:schema>
Click here to view the file.

XML Schema
XML Schema Examples
Example 1: eBIZ address demo
Given below is the source code of XML file that is used by eBIZ.com to store the details of it’s associates :
<?xml version="1.0" encoding="UTF-7"?>

<ebiz xmlns="http://www.ebizel.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="simple_with_schema.xsd">
<address>
     <name>Dharmendra Das</name>
     <age>20</age>
     <address_str>D 210</address_str>
     <city>New Delhi</city>
     <state>CG</state>
     <pin>110000</pin>
</address>
<address>
     <name>XYZ Kumar</name>
     <age>20</age>
     <address_str>b 66</address_str>
     <city>Noida </city>
     <state>UP</state>
     <pin>201301</pin>
</address>
<address>
     <name>Sam </name>
     <age>20</age>
     <address_str>Karmpura</address_str>
     <city>Noida </city>
     <state>UP</state>
     <pin>201301</pin>
</address>
<address>
     <name>AK Kumar</name>
     <age>20</age>
     <address_str>b 66</address_str>
     <city>Noida </city>
     <state>UP</state>
     <pin>201301</pin>
</address>
<address>
     <name>VJ</name>
     <age>20</age>
     <address_str>e 66</address_str>
     <city>New Delhi </city>
     <state>DLI</state>
     <pin>201301</pin>
</address>
<address>
     <name>Amu</name>
     <age>30</age>
     <address_str>J 3</address_str>
     <city>Noida </city>
     <state>UP</state>
     <pin>201301</pin>
</address>

</ebiz>
Click here to view the file.
Schema declaration for above should look like this:
<?xml version="1.0" encoding="UTF-7"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ebizel.com"
xmlns="http://www.ebizel.com"
elementFormDefault="qualified">
     <xsd:element name="ebiz">
     <xsd:complexType>
     <xsd:sequence>
     <xsd:element ref="address" minOccurs="1" maxOccurs="unbounded"/>
     </xsd:sequence>
</xsd:complexType>
</xsd:element>
    <xsd:element name="address">
     <xsd:complexType>
     <xsd:sequence>
      <xsd:element ref="name" minOccurs="1" maxOccurs="1"/>
      <xsd:element ref="age" minOccurs="1" maxOccurs="1" />
      <xsd:element ref="address_str" minOccurs="1" maxOccurs="1"/>
       <xsd:element ref="city" minOccurs="1" maxOccurs="1"/>
      <xsd:element ref="state" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="pin" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="xsd:integer">
<xsd:simpleType>
      <xsd:restriction base="xsd:integer">
      <xsd:minInclusive value="16" />
      <xsd:maxInclusive value="75" />
      </xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="address_str" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string">
<xsd:simpleType>
      <xsd:restriction base="xsd:string">
      <xsd:minLength value="2" />
      <xsd:maxLength value="4" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="pin" type="xsd:integer">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:length value="6" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>


</xsd:schema>
Click here to view the file.

0 comments: