[go: up one dir, main page]

0% found this document useful (0 votes)
90 views106 pages

Unit Ii

The document discusses XML, its components like elements, attributes, and entity references. It also discusses XML syntax and structure, how to define elements and attributes using DTD, and different types of DTDs like internal and external DTD.

Uploaded by

Mr.V. Prabhakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views106 pages

Unit Ii

The document discusses XML, its components like elements, attributes, and entity references. It also discusses XML syntax and structure, how to define elements and attributes using DTD, and different types of DTDs like internal and external DTD.

Uploaded by

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

UNIT-II

XML
Contents
1. XML
2. Document type definition,
3. XML Schemas
4. presenting XML using XSLT
5. Document Object model
6. Reading the XML document using parsers
7. DOM parser and SAX parser.
Two problems with HTML:
1. Fixed set of tags and attributes:
User cannot define new tags or attributes
2. There are no restrictions on arrangement or order of tag
appearance in a document
HTML is used to how Why
the data
dois displayed
we need onXML
web.
<p> <b> Ms. Usha </b> <br>
Asst.Professor <br>
SNIST </p>

Tags in this document tell a browser how to display this


information, tags don’t tell the browser what the information is
XML
❑ XML is based on SGML- standard generalized mark up language.
❑ XML stands for extensible Markup Language, is a markup language that you can use to
create your own tags.
❑ XML is a markup language much like HTML

❑ XML was designed to carry data, not to display data


❑ XML tags are not predefined. You must define your own tags
❑ XML is designed to be self-descriptive

❑ XML is a W3C Recommendation


<address>
<name>
<title>Mrs.</title>
<first-name> Mary </first-name>
<last-name> McGoon </last-name>
</name>
<street> 1401 Main Street </street>
<city>Anytown</city>
<state>NC</state>
<postal-code> 34829 </postal-code>
⮚XML is a software- hardware independent tool for carrying
</address>
information.
⮚ XML is a most common tool for data transmission between all sorts
of applications.
How Can XML be Used:

1. XML Separates Data from HTML


2. XML Simplifies Data Transport
3. XML Simplifies Platform Changes
4. XML Makes Your Data More Available.
5. Takes data from program like mysql(microsoft sql) and convert into xml and share that xml
to all the sort of applications and platforms.

Applications of xml:
1. cell phones:
xml data is sent to some cell phones. That data is formatted by specifications of the cell
phone software designer to display text, image or even to play sounds.
2. File converters:
Many applications have been written to convert existing documents into the XML
standard. An example is a PDF to XML converter.
3. Voice XML :
Converts XML documents into an audio format so that you can listen to an XML document.
4. Ms office also uses its file format in xml.
Difference between HTML and XML:
Xml syntax:
Syntax is used to create well formed xml document.

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


<class_list>
<student>
<name> Rohit</name>
<grade> A+ </grade>
</student>
<student>
<name>Sai</name>
<grade>A-</grade>
<?xml version="1.0" encoding=“UTF-8"?>
❑ This is called xml declaration which states that what type xml version you are
using and type of encoding you using.
❑ encoding- character set used in this document is all character set used by
western european languages or it may be UTF- 8.
⮚ All XML Elements Must Have a Closing Tag <root>
⮚ XML Tags are Case Sensitive   <child>
   
⮚ XML Elements Must be Properly Nested
<subchild>.....</subchild>
In HTML :<b><i>This text is bold and italic</b></i>
  </child>
In xml:<b><i>This text is bold and italic</i></b> </root>
⮚ XML Documents Must Have a Root Element
An XML document contains :

• Elements
• Attribute
• Entity references
1. XML Element:
An XML element is everything from (including) the element's start tag to
(including) the element's end tag including text data.
Example:
<employee>
<empno>16</empno>
Elements
<name>Goutham</name>
<salary>45000</salary>
</employee>
An element can contain:
2. Child elements
3. attributes
4. Text-data
5. or a mix of all of the above...
2. XML attributes:
⮚ Attributes provide additional information about an element.
⮚ XML Attributes Must be Quoted Attribute

<employee empno=“1319” name=“Sam”>


<employee>
Rules to define Attributes:
• Attribute values must be enclosed with either single or
double quotes.
• Attribute names are Unique for one element.
XML elements vs attributes

<person gender="female"> <person>


  <firstname>Anna</firstname>   <gender>female</gender>
  <lastname>Smith</lastname>   <firstname>Anna</firstname>
</person>   <lastname>Smith</lastname>
</person>
gender is attribute gender is element
3. Entity reference:
⮚ Some characters have a special meaning in XML.
<person>
<name>Ramesh</name>
<age>age is <18</age>
</person>
⮚ In above example “less than symbol” has special meaning
⮚ “<“ is used for opening tag
⮚ In entity reference we will use “&lt;” for less than
<person>
<name>Ramesh</name>
<age>age is &lt;18</age>
</person>
User defined Entity ref:
Syntax:
&entity reference name;
XML tree:

⮚ XML documents form a tree structure that starts at "the root" and
branches to "the leaves".
⮚ XML document contains a single element.
⮚ That single element is called root element

<?xml version="1.0" encoding="ISO-8859-1"?>


<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
⮚ XML contains elements, attributes and entity references.
⮚ To define some functionality for elements, attributes and entity
references.
⮚ To define the structure of a xml document we required DTD or
XSD
⮚ XML document structures are defining inside the DTD or XSD
⮚ XML document is called well formed Document
Well Formed XML Documents:
A "Well Formed" XML document has correct XML syntax.
⮚ XML document must begin with PROLOG
⮚ XML documents must have a unique root element
⮚ XML elements must have a closing tag for all opening tags
⮚ XML tags are case sensitive
⮚ XML elements must be properly nested
⮚ XML attribute values must be quoted
⮚ In place of some special characters we can use entity references
Document type definition(DTD):

• DTD is an XML technique to define the structure of a XML Document.


• XML elements, attributes, Entity references functionality are defining
inside the DTD.
• DTD is a Text based Document with .dtd extension.
DTD contains:
⮚ Elements declaration
⮚ Attribute declaration
⮚ Entity references declaration
Why Use a DTD?
1. Each of our XML files can carry a description of its own
format.

2. Independent groups of people can agree to use a standard


DTD for interchanging data.

3. Application can use a standard DTD to verify that the data


we receive from the outside world is valid.

4. We can also use a DTD to verify our own data.


DTD declaration syntax:
To declare elements, attributes, entity references , to declare any
component we use following
Syntax: <!>
<!DOCTYPE > declaration
Example:
<!DOCTYPE employee[
<!ELEMENT employee(empno , empname, sal)>
<!ELEMENT empno(#PCDATA)>
<!ELEMENT name(#PCDATA)>
Element Name
<!ELEMENT element-name(content-model)>
Content Model: Five Types of Element Declaration are Possible.

1. Text Only Element


2. Child Only Element
3. Empty Element
4. Any Element
5. Mixed Element
Text Only Element Example
Empty Content:
⮚ This is a special case of element declaration. This element declaration does
not contain any content.(Not allows Text data and Child data but attributes
are allow).
⮚ These are declared with the keyword EMPTY.
Syntax:
<!ELEMENT element-name EMPTY>
Example: empty.xml
<!Doctype employees[
<!ELEMENT employees(employee *)>
<!ELEMENT employee EMPTY>]
< employees>
< employee>< employee>
Child Elements:
⮚ Child elements are separated by , or “|”
⮚ ‘|’(vertical line) :either or condition applied it means only one child.
<!ELEMENT book (book-name|title)>
<!ELEMENT book-name (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<book>
<title>WebTechnologies</title>
</book>
<book>
<book-name>WT</book-name>
</book>
Cardinality Operators:

⮚In XML document one element can occurs zero or n number of times.
⮚To specify how many number of times an element can occur in an XML document .
⮚We can use cardinality operator.
Cardinality operators:
Number of times element can occur in a XML document we can use Cardinality Operator.
Declaring Only One Occurrence of an Element 

<!ELEMENT element-name (child-name)>

Example:<!ELEMENT note (message)>

The example above declares that the child element "message" must occur once, and only once
inside the "note" element.

Declaring Minimum One Occurrence of an Element

<!ELEMENT element-name (child-name+)>

Example:<!ELEMENT note (message+)>

The + sign in the example above declares that the child element "message" must occur one or
more times inside the "note" element.
 
<!ELEMENT element-name (child-name*)>

Example:<!ELEMENT note (message*)>

The * sign in the example above declares that the child element
"message" can occur zero or more times inside the "note" element.

Declaring Zero or One Occurrences of an Element


 
<!ELEMENT element-name (child-name?)>

Example:<!ELEMENT note (message?)>

The ? sign in the example above declares that the child element
Example
Elements with any Contents
Elements declared with the category keyword ANY, can contain any

combination of parsable data:

<!ELEMENT element-name ANY>


Example:
TYPES OF DTD’S:
1.Internal DTD
2.External DTD
Internal DTD:
• If the DTD is declared inside the XML file, it should be wrapped in a
DOCTYPE definition with the following syntax:
<!DOCTYPE root-element [element-declarations]>

• Internal DTD’s are specific to XML document.


• Internal DTD’s are not reusable.
Example: emp.xml
<!DOCTYPE employee [
<!ELEMENT employee (empno,empname,sal)>
<!ELEMENT empno (#PCDATA)>
<!ELEMENT empname (#PCDATA)>
<!ELEMENT sal (#PCDATA)>
]>
<employee>
<empno>1216</empno>
<empname>ram</empname>
<sal>34000</sal>
</employee>
External DTD Declaration:
• If the DTD is declared in an external file, it should be
wrapped in a DOCTYPE definition with the following
syntax: root-element SYSTEM/PUBLIC “url of file dtd">
<!DOCTYPE

External DTD’s are two types:


1. Private DTD’s
2. Public DTD’s
Private DTD’s:
<!DOCTYPE root-element SYSTEM “DTDFilename.dtd">

Example: emp.dtd

<!ELEMENT employee (empno,empname,sal)>


<!ELEMENT empno (#PCDATA)>
<!ELEMENT empname (#PCDATA)>
<!ELEMENT sal (#PCDATA)>

Above file save with .dtd extension


Mapping to Private DTD to XML Example:

<!DOCTYPE employee SYSTEM "E:\SNIST\WT\EXAMPLES\employee.dtd" >


<employee>
<empno>1216</empno>
<empname>ram</empname>
<sal>34000</sal>
</employee>

Above file save with .XML extension


Public DTD’s:
• If any DTD not specific to any particular project – “Public DTD”.
• If any DTD is specific to a particular XML document are called “Internal
DTD”.
• If any DTD is specific to a particular project – “Private DTD”.
<!DOCTYPE rootElement PUBLIC “-//vendoename//version//EN ”DTDfile”>
Syntax:

PUBLIC : DTDs defined by International Organizations like W3C (standards)


The Building Blocks of XML Documents

Seen from a DTD point of view, all XML documents (and


HTML documents) are made up by the following building
blocks:

• Elements
• Attributes
• Entities
• PCDATA

Types of Entity
1. Internal Entity: declared within DTD:
syntax
<!ENTITY entity-name "entity-value">

Ex: <!ENTITY c “SNIST”>


Instruction: <college> my college name is &c;</college>
Note: An entity has three parts:
an ampersand (&),
an entity name, 41
2. External Entity:
Included in the different file and referred in the xml file.
Syntax
<!ENTITY entity-name SYSTEM "URI/URL">

Example
<!ENTITY writer SYSTEM "http://www.w3schools.com/entities.dtd">
or <!ENTITY writer SYSTEM “d:\test.txt”
<!ENTITY copyright SYSTEM "http://www.w3schools.com/entities.dtd">

XML example:

<author>&writer; &copyright;</author>
DTD Attributes:
• Attribute gives more information about an element or more precisely it defines a
property of an element.
• An XML attribute is always in the form of a name-value pair.
• An element can have any number of unique attributes.
• Attribute declaration is very much similar to element declarations in many ways
except one; instead of declaring allowable content for elements, you declare a list of
allowable attributes for each element.
• These lists are
<!ATTLIST called ATTLISTattribute-name
element-name declaration. attribute-type attribute-value>
Syntax:

▪ The DTD attributes start with <!ATTLIST keyword if the element contains the
attribute.
▪ element-name specifies the name of the element to which the attribute applies.
▪ attribute-name specifies the name of the attribute which is included with the
Attribute Value Declaration
• Within each attribute declaration, you must specify how the value will appear in
the document. You can specify if an attribute.
⮚ can have a default value
⮚ can have a fixed value
⮚ is required value
⮚ is implied value
Default Values:
It contains the default value. The values can be enclosed in single quotes(') or
double quotes(").
Syntax:
<!ATTLIST element-name attribute-name attribute-type "default-value">
• where default-value is the attribute value defined.
A Default Attribute Value Example:
DTD:
<!ELEMENT square>
<!ATTLIST square width CDATA "0">

Valid XML:<square width="100" />

In the example above, the "square" element is defined


with a "width" attribute of  type CDATA. If no width is
specified, it has a default value of 0.
#REQUIRED

Syntax

<!ATTLIST element-name attribute-name attribute-type #REQUIRED>


Example

DTD: <!ATTLIST person number CDATA #REQUIRED>


Valid XML: <person number="5677" />
Invalid XML: <person />

Use the #REQUIRED keyword if you don't have an option for a default
value, but still want to force the attribute to be present.
#FIXED

Syntax
<!ATTLIST element-name attribute-name attribute-type #FIXED "value">
xample

DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML: <sender company="Microsoft" />
Invalid XML:<sender company="W3Schools" />

Use the #FIXED keyword when you want an attribute to have a fixed
value without allowing the author to change it. If an author includes
another value, the XML parser will return an error.
Enumerated Attribute Values

Syntax

<!ATTLIST element-name attribute-name (en1|en2|..) default-value>


Example

DTD: <!ATTLIST payment type (check|cash) "cash">


XML example: <payment type="check" /> or
<payment type="cash" />

Use enumerated attribute values when you want the attribute value to be one of a fixed set of legal
values.
Example:
<?xml version = "1.0"?>
<!DOCTYPE address [
<!ELEMENT address (name )>
<!ELEMENT name ( #PCDATA )>
<!ATTLIST name id CDATA #REQUIRED> ]>
<address>
<name id = "1216">Ramesh</name>
</address>

Rules of Attribute Declaration:

• All attributes used in an XML document must be declared in the Document Type Definition
(DTD) using an Attribute-List Declaration
• Attributes may only appear in start or empty tags.
• The keyword ATTLIST must be in upper case
• No duplicate attribute names will be allowed within the attribute list for a given element.
Attribute Types:

⮚ When declaring attributes, you can specify how the processor


should handle the data that appears in the value.
We can categorize attribute types in three main categories :

1. String type
2. Tokenized types
3. Enumerated types
Attribute type categories
String Types Tokenized Types Enumerated Types
CDATA ID Enumeration
IDREF
IDREFS

55
CDATA ( Character DATA )
– It is a string type attribute
– It can take any string as a value
– It should not contain escape characters such as <,>,&,’, and “ .

ID
– Attribute of type ID contains unique value. This means that the value of an ID
attribute must not appear more than once throughout an XML document.
– ID resembles primary key concept used in databases.
– For example, attribute no ( question number ) of the element question should
always have a unique value so that it can be used to identify a question
uniquely.

– Examples <!ATTLIST question no ID #REQUIRED >


<!ATTLIST employee id ID #REQUIRED > 56
IDREF
– It is similar to that of foreign key concept in databases
– The attribute of type IDREF must refer to an ID value declared elsewhere in the document
– Example <!ATTLIST question no ID #REQUIRED >
<!ATTLIST answer qno IDREF #REQUIRED >

– Here, the qno attribute of answer refers to a question for which it is the answer. So, the
following XML document is valid:

< question no=‘q1’ >


What is the full form of DTD ?
</question>
< question no=‘q2’ >
What is the full form of XML ?
</question>
< answer qno=‘q1’ >
Document Type Definition 57
IDREFS
– It allows a list of ID values separated by white spaces
– Example

<!ATTLIST student roll ID #REQUIRED >


<!ATTLIST subject sid ID #REQUIRED >
<!ATTLIST marks ref IDREFS #REQUIRED >

– Following is an example of how to use it:

<student roll=‘501’> Samir Roy </student>


<subject sid=‘s1’ > Web Technology </subject>
<marks ref=‘r01 s1’> 82 </marks>

58
Enumerated value list

– Enumerated attribute values are used when we want the attribute


value to be one of a fixed set of values.
Ex:-

<!ATTLIST schedule
day ( mon | tue | wed | thu | fri | sat | sun ) “sun” >

59
XML namespace
⮚ XML was designed to be a very robust markup language that could be used
in many different applications
⮚ When you are creating new elements, there is the chance that the element's
name already exists.
<?xml version="1.0" encoding="ISO-8859-15"?>
<html>
<body>
<p>Welcome to my Health Resource</p>
</body>
<body>
<height>6ft</height>
<weight>155 lbs</weight>
</body>
</html>
Name Conflicts
• In XML, element names are defined by the developer. This often results
in a conflict when trying to mix XML documents from different XML
applications.

1. This XML carries HTML table information:


<table>
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>

2. This XML carries information about a table (a piece of furniture):


<table>
  <name>African Coffee Table</name>
  <width>80</width> 61
Solving the Name Conflict Using a Prefix
• Name conflicts in XML can easily be avoided using a name prefix.

1. This XML carries information about an HTML table, and a piece of furniture:
<h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
In the example above, there will be no conflict because the two <table> elements have different names.

62
XML Namespaces - The xmlns Attribute:

⮚ When using prefixes in XML, a namespace for the prefix must be


defined.
⮚ The namespace can be defined by an xmlns attribute in the start tag of
an element.
⮚ The namespace declaration has the following syntax.
xmlns:prefix="URI".

<name: element_name xmlns :name = “URI” >

⮚ Namespace: Way of keeping the names used by applications


separate from each other.
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table xmlns:f="http://www.w3schools.com/furniture">
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
</root>

⮚ 64
Namespaces can be declared in the elements where they are used
or in the XML root element:
<root>

<h:table xmlns:h="http://www.snist.com">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table xmlns:f="https://www.gmail.com">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

</root>
65
Disadvantages of DTD

1. DTD does not follow the XML syntax it requires new syntax.
2. Namespace does not supported
3. No data types.
4. No modularity and no reuse of elements.
5. No inheritance for elements or attributes
6. DTD is old technique.
XSD- XML Schema Definition:

1. XML Schema is an XML-based alternative to DTD.


2. An XML schema describes the structure of an XML document.
3. The XML Schema language is also referred to as XML Schema
Definition (XSD).
4. XSDs can be extensible for future additions.
5. XSD is richer and more powerful than DTD.

What is an XML Schema?


The purpose of an XML Schema is to define the legal building blocks
An XML Schema:

1. defines elements that can appear in a document


2. defines attributes that can appear in a document
3. defines which elements are child elements
4. defines the order of child elements
5. defines the number of child elements
6. defines whether an element is empty or can include text
7. defines data types for elements and attributes
8. defines default and fixed values for elements and attributes
Writing Simple XML Schema

Step 1: Write a Simple schema file to define the structure of XML file
and save it as .XSD extension .
Step 2: Write an XML Document for the Defined Schema .
Step 3: Execute the XML in Browser or XML Editor .
XSD - The <schema> Element:
The <schema> element is the root element of every XML
Schema:
<xs:schema>
...
...
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>

xmlns:xs= "http://www.w3.org/2001/XMLSchema"
this elements indicates that the elements and data types used in this XML
schema are come from the "http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.w3schools.com"
elements in this schema comes from "http://www.w3schools.com" namespace.

xmlns="http://www.w3schools.com" – default namespace


elementFormDefault="qualified"- any element used in this schema
Data Types in XSD:
XSD Elements: In XSD two ways to create Elements.
• Simple Element
• Complex Element
Simple Element:
• A simple element is an XML element that can contain only text data.
• It cannot contain any other elements or attributes.
Syntax:
<xs:schema>
<xs:element name=“element name” type=“xs:data type”>
</xs:element>
</xs:schema>
Example:

<xs:schema>
<xs:element name=“EmpNo” type=“xs:int”>
<xs:element name=“EmpName” type=“xs:string”>
</xs:element>
</xs:schema>
• Primitive types-19: • Built-in- derived Data Types:
• String,
• boolean, • normalizedString •intger,
• decimal, •nonPositiveInteger,
• token,
• •negativeInteger,
float, • language, •long,
• double, • NMTOKEN, •int,
• duration, • NMTOKENS •short,
• dateTime, • Name, •byte,
• time, • NCName, •nonNegativeInteger
• date, •unsignedLong,
• ID,
• •unsignetInt
gYearMonth, • IDREF, •unsignedShort,
• gYear,gMonthDay, • IFREFS, •unsignedByte,
• gDay,gMonth, • ENITIY, •positiveInteger
• nexbinary, • ENTITIES,
• base64Binary,
XSD- Complex elements:
There are four kinds of complex elements:
A complex element contains other elements and/or attributes.
1.empty elements
<employee eid="1345"/>

2.elements that contain only other elements


<employee>
  <firstname>John</firstname>
  <lastname>Smith</lastname>
</employee>

3. elements that contain only text


<food type="dessert">Ice cream</food>

4.elements that contain both other elements and text


<description>
It happened on <date lang="norwegian">03.03.99</date> ....
How to Define a Complex Element
Complex elements are defined in two ways
<employee>
  <firstname>John</firstname>
  <lastname>Smith</lastname>
</employee>
1. The "employee" element can be declared directly by naming the element, like this:
<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
⮚only the "employee" element can use the specified complex type.
</xs:element>
⮚<sequence> indicator. This means that the child elements must appear in
the same order as they are declared.
2. The "employee" element can have a type attribute that refers to the name of the
complex type to use:
<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
If you use the method described above, several elements can refer to the
same complex type name="employee" type="personinfo"/>
<xs:element
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/>

<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
1.empty elements :
<product prodid="1345" />

<xs:element name="product">
  <xs:complexType>
    <xs:attribute name="prodid“ type="xs:positiveInteger"/>
  </xs:complexType>
</xs:element>
2. Complex Types Containing Elements Only
XML
<person>
  <firstname>John</firstname>
  <lastname>Smith</lastname>
</person>
XML schema
<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
Complex Text-Only Elements:
it contains simple Content(text/attributes)
⮚ you must use simpleContent element around the content.
⮚ you must define an extension OR a restriction
OR within the
simpleContent
<xs:element element, like this:
name="somename">
  <xs:complexType> <xs:element name="somename">
    <xs:simpleContent>   <xs:complexType>
      <xs:extension base="basetype">     <xs:simpleContent>
        ....       <xs:restriction base="basetype">
        ....         ....
      </xs:extension>         ....
    </xs:simpleContent>       </xs:restriction>
  </xs:complexType>     </xs:simpleContent>
</xs:element>   </xs:complexType>
</xs:element>
Example- XML
<carcost Cname=“swift”>600000</carcost>
XML schema
<xs:element name=“carcost">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name=“Cname" type="xs:string" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>
Complex Types with Mixed Content
• An XML element that contains both text and other elements:
XML
<address>
To,<name>Sree Ram</name>
Flat-no-207<aptname>S.S.Heavens</aptname>
<city>hyderabad</city>
</address> XML schema
<xs:element name=“address">
  <xs:complexType mixed="true">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name=“aptname" type="xs:string"/>
      <xs:element name=“city" type="xs:hyderabad"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
XSD indicators
⮚ We can control HOW elements are to be used in documents with indicators.

7 indicators are there


order indicators
1.All 2.Choice 3.Sequence
Occurrence indicators:
• maxOccurs
• minOccurs
Group indicators:
• Group name
• attributeGroup name
order indicators- All, Choice, Sequence
1. All Indicator: child can appear in any order and each child can occur only once.
<xs:element name="person">
  <xs:complexType>
    <xs:all>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:all>
  </xs:complexType>
</xs:element>
2. Choice Indicator: The <choice> indicator specifies that either one child element
or another can occur:
3. Sequence Indicator: The <sequence> indicator specifies that the child elements
must appear in a specific order:
Occurrence Indicators: define how often an element can occur.
1. <maxOccurs> indicator specifies the maximum number of times an element
can occur:
2. <minOccurs> indicator specifies the minimum number of times an element
can occur:
Group Indicators
• Group indicators are used to define related sets of elements
Element Groups
Element groups are defined with group declaration
<xs:group name="groupname">
...
</xs:group>
You must define an all, choice, or sequence element inside the group declaration.
<xs:group name="persongroup">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
example
<name>SriRam</name>
<rollno>1101232</rollno>
<age>36</age>
<dateborn>2010-03-27</
dateborn>
<xs:element name=“name" type="xs:string"/>
<xs:element name=“rollno" type="xs:string"/>
<xs:element name="age" type=" xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
Default and Fixed Values for Simple Elements

<xs:element name="color" type="xs:string"


default="red"/>
<xs:element name="color" type="xs:string"
fixed="red"/>
Attribute declaration
• All attributes are declared as simple types.
Syntax
<xs:attribute name="xxx" type="yyy"/>
<lastname lang="EN">Smith</lastname>- XML
<xs:attribute name="lang" type="xs:string"/>-XSD
Default and fixed values for attributes
<xs:attribute name="lang" type="xs:string" default="EN"/>
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Restrictions on Content/ facets
⮚ Restrictions are used to define acceptable values for XML elements
or attributes. Restrictions on XML elements are called facets.
⮚ When u define the data types for elements or attributes, it put
restrictions on content of elements and attributes.
⮚<xs:element name=“rollno">
If an XML element is of type "xs:date" and contains a string like
  <xs:simpleType>
"Hello World", the element will not validate.
    <xs:restriction <xs:element name="car">
base="xs:integer">   <xs:simpleType>
      <xs:minInclusive value=“1"/>     <xs:restriction
      <xs:maxInclusive base="xs:string">
value=“70"/>       <xs:enumeration
    </xs:restriction> value="Audi"/>
  </xs:simpleType>       <xs:enumeration
</xs:element> value="Golf"/>
      <xs:enumeration
value="BMW"/>
XSD Name Space concept:
• In java package concept is used to group all the classes as a single
Unit.
• When class is present in a package at the time of using we are
specifying with fully qualified names.
• Fully qualified name: class name along with package.
• If we use fully qualified name it is possible t avoid ambiguity
problem.
Ex:
Class Test{
java.util.Date d1;
Java.sql.Date d2;
• Similarly in XSD we have name space concept.
• Namespace are used to group all the elements as a single unit.
• At the time of using the element we will use fully qualified name.
• Fully qualified names: NameSpace+Element name
• In java package packagename;
• In XSD targetNameSpace
• targetNameSpace attribute is with in <schema>

Ex:

<schema targetNameSpace=“http://ushainfo.com”>
Create elements
</schema>
• In XSD one targetNameSpace declaration is posssible.
• In XML any no. of XMLNS declaration are possible.

1. Schema
2. complex Type http://www.w3.org/2001/xml/schema
3. Sequence etc

• It is also possible to define XMLNS in XSD


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="myNamespace">
</xs:schema>
<schema targetNameSpace=“http://ushainfo.com”>
<element name=“employee”>
<complexType>
<sequence>
<element name=“empno” type=“int”/>
<element name=“empname” type=“string”/>
<element name=“salary” type=“decimal”/>
</sequence></complexType> </schema >
----------------------------------------------------------------------
-
<http://ushainfo.com:employee>
< http://ushainfo.com:empno >1216</ http://ushainfo.com.com:empno >
Writing fully qualified name with elements it is over burden to over come this
problem we are using XMLNS.

<employee XMLNS:”http://ushainfo.com”>
<empno>1216</empno>
<empname>Ram</empname>
<salary>45000<salary/>
</employee>
--------------------------------------------------------------------------------
It also possible to create prefix for XMLNS
<e:employee XMLNS:e=”http://ushainfo.com”>
<e:empno>1216</e:empno>
<e:empname>Ram</e:empname>
<e:salary>45000<e:salary/>
XML Parsers
• The parser is the engine for interpreting our XML
documents
• The parser reads the XML and prepares the information for
your application
How to use a parser
• 1. Create a parser object
• 2. Pass your XML document to the parser
• 3. Process the results
Types of Parsers

– DOM is the Document Object Model

– SAX is the Simple API for XML (PUSH Parsing)


Diff b/w DOM & SAX
Document Object Model (DOM)
• DOM uses a tree based structure.
• DOM reads an entire XML document and builds a
Document Object.
– The Document object contains the tree structure.
– The top of the tree is the root node.
– The tree grows down from this root, defining the child
elements.
– DOM is a W3C standard.
103 • Using DOM, we can also perform insert nodes,
XML DOM Tree Example
XML DOM views an XML document as a node-tree.
All the nodes in the tree have a relationship to each other.

104
XML DOM Parsing
1. Most browsers have a build-in XML parser to read and manipulate XML.

2. The parser converts XML into a JavaScript accessible object.

Parsing XML
3. XML DOM contains methods (functions) to traverse XML trees, access, insert,
and delete nodes.

4. Before an XML document can be accessed and manipulated, it must be loaded


into an XML DOM object.

5. parser reads XML into memory and converts it into an XML DOM object that can
be accessed with JavaScript.
105
SAX ( Simple API for XML )

SAX parsers are event-driven:


⮚ The parser fires an event as it parses each XML element/item.
⮚ It is up to you to decide what you want to do with those events, if you
ignore them the information in the event is discarded.
⮚ The developer can write a java code that handles the events.
SAX events: The SAX API defines a number of events
⮚ startDocument : Signals the start of the document.
⮚ endDocument : Signals the end of the document
⮚ startElement : Signals the start of an element. The parser fires this event
when all of the contents of the opening tag have been processed.106That

You might also like