You are on page 1of 100

XML

- Introduction to XML: Extensible Mark Up Language


- XML document structure
- DTD, name space, Schemas
- XSLT style sheets
- XML processors

http://www.tizag.com/xmlTutorial/
Two problems with HTML:
– 1. Fixed set of tags and attributes
• User cannot define new tags or attributes
– 2. There are few restrictions on
arrangement or order of tag
appearance in a document

E. Swathi, Assist Prof, CSE,


CBIT
Why do we need XML?
• HTML is used to how the data is displayed on web.
<p><b>Mrs. Mary McGoon</b>
<br>
1401 Main Street
<br>
Anytown, NC 34829</p>
Tags in this document tell a browser how to display
this information, tags don’t tell the browser what
the information is

E. Swathi, Assist Prof, CSE,


CBIT
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
➢ XML tags are not predefined. You must define your
own tags
➢ XML is designed to be self-descriptive
➢ XML is a W3C Recommendation

E. Swathi, Assist Prof, CSE,


CBIT
<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>
</address>

-> XML is a software- hardware independent tool for carrying


information.
-> XML is a most common tool for data transmission between
all sorts of applications.

E. Swathi, Assist Prof, CSE,


CBIT
• 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. cellephones- xml data is sent to some cellphones. That data
is formatted by specifications of the cellphone 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. VoiceXML - 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.

E. Swathi, Assist Prof, CSE,


CBIT
Xml syntax
• Syntax is used to create well formed xml
document.
<?xml version="1.0" encoding=“UTF-8"?>
<class_list>
<student>
<name> Robert</name>
<grade> A+ </grade>
</student>
<student>
<name>Lenard</name>
<grade>A-</grade>
</student>
</class_list>

E. Swathi, Assist Prof, CSE,


CBIT
<?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


➢XML Tags are Case Sensitive
➢XML Elements Must be Properly Nested
In HTML
<b><i>This text is bold and italic</b></i>
In xml
<b><i>This text is bold and italic</i></b>
➢XML Documents Must Have a Root Element

<root>
<child>
<subchild>.....</subchild>
E. Swathi, </child>
Assist Prof, CSE,
CBIT
</root>
➢ Attributes must have quoted values
<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>
➢ Entity References
Some characters have a special meaning in XML.

<salary>if salary < 1000 then</salary>


to avoid such error use entity references

E. Swathi, Assist Prof, CSE,


CBIT
➢ Comments in xml- same as html
<!- - this is comment- ->
➢ XML Stores New Line as LF( line feed)
➢ White spaces are preserved in xml.
➢ Xml naming rules
1. names can contain letters, numbers, and other characters.
2. does not start with number or punctuation characters.
3. names cannot start with word xml or XML.
4. names cannot have spaces.
5. any name can used, no words are reserved.
avoid - , . , :

E. Swathi, Assist Prof, CSE,


CBIT
Difference of html and
xml
Html xml
1. HTML was designed to display 1. XML was designed to be a software
data with focus on how data looks and hardware independent tool used
to transport and store data, with
focus on what data is.

2.Case insensitive 2. case- sensitive


3. designing a web-page to be 3. transport data between the
rendered on the client side application and the database.
4. HTML does not preserve white 4. Does
space
5. Static 5. Dynamic
6. not strict if the user does not use 6. Closing tag is mandatory
the closing tags

E. Swathi, Assist Prof, CSE,


CBIT
• Prolog: The part of an XML document
that precedes the XML data
Includes
a. Processing Instructions :
XML declaration for version [, encoding,
standalone]

b. Document Type declaration i.e DTD ( optional )

Example
<?xml version="1.0" encoding="ISO-8859-1"?> <!– a →
<!DOCTYPE note SYSTEM “InternalNote.dtd"> <!– b →

E. Swathi, Assist Prof, CSE,


CBIT
12
XML Declaration for version

version :- Tells the XML parser which version to use .


XML parser: program responsible for processing XML
document.

encoding :- Defines the character encoding used in the


document.
1.ASCII :- 7 bits character set -128 characters.

2.ISO-8859-1:- 8 bits character set – 256 characters

( ASCII + Western European


E. Swathi, letters
Assist Prof, CSE, and symbols ).
CBIT
13
Standalone :- tells the XML processor whether
they are any
other files to load.

Syntax :-

<?xml name1=value1 name2=value2 name3=value3?>

Examples

E. Swathi, Assist Prof, CSE,


CBIT
14
Xml elements
• An XML element is everything from
(including) the element's start tag to
(including) the element's end tag.
An element can contain:
• other elements
• text
• attributes
• or a mix of all of the above...

Delimited by angle brackets


General format: <element> …
</element>
Empty element: </empty-Element>
Ex. <br/>
E. Swathi, Assist Prof, CSE,
CBIT
XML attributes
• Attributes provide additional information about an element.
• XML Attribute values Must be Quoted
<city state=“ap”>

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 isE.attribute
Swathi, Assist Prof, CSE, gender is element
CBIT

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>

note- root element


to, from, heading, body are child elements

E. Swathi, Assist Prof, CSE,


CBIT
E. Swathi, Assist Prof, CSE,
CBIT
Well Formed XML Documents
A "Well Formed" XML document has correct
XML syntax.
➢ XML documents must have a root element
➢ XML elements must have a closing tag
➢ XML tags are case sensitive
➢ XML elements must be properly nested
➢ XML attribute values must be quoted

E. Swathi, Assist Prof, CSE,


CBIT
Document type definition
➢ Defines structure of the XML document
1. elements that may be included in your
document
2. what attributes these elements have
3. the ordering and nesting of the elements.
4. no of occurrences of tags.
➢ The DTD is declared in a DOCTYPE
declaration beneath the XML declaration
contained within an XML document
➢ A DTD can be declared inline inside an
XML document, or as an external
reference.

E. Swathi, Assist Prof, CSE,


CBIT
Internal DTD Declaration/
inline DTD declaration
• 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]>

<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this
E. Swathi, Assistweekend</body>
Prof, CSE,
</note> CBIT
The DTD above is interpreted like this:

!DOCTYPE note defines that the root element of this


document is note.
!ELEMENT note defines that the note element contains four
elements: "to,from,heading,body".
!ELEMENT to defines the to element to be of the type
"#PCDATA".
!ELEMENT from defines the from element to be of the type
"#PCDATA".
!ELEMENT heading defines the heading element to be of the
type "#PCDATA".
!ELEMENT body defines the body element to be of the type
"#PCDATA".

E. Swathi, Assist Prof, CSE,


CBIT
External DTD Declaration
• If the DTD is declared in an external file, it should be wrapped in
a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element SYSTEM/PUBLIC “url of file dtd">

SYSTEM – DTDs defined by individuals and organizations.


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

some DTDs are available as international standards, such as those


recommendations of W3C which relate to HTML.
Other DTDs are developed by individuals and organizations for their
own use.

E. Swathi, Assist Prof, CSE,


CBIT
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

note.dtd
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

E. Swathi, Assist Prof, CSE,


CBIT
<?xml version=“1.0” encoding=“US-ASCII”
standalone=“no” ?>
<!DOCTYPE authorslist SYSTEM “authors.dtd” >
<authorslist>
<name>
<firstname>chris</firstname>
<lastname>bates</lastname>
<book>web programming</book>
<year>1999</year>
</name>
<name>
<firstname>subramanyam</firstname>
<lastname>allamaraju</lastname>
<book>java server programming</book>
<year>2000</year>
</name>
</authorslist>
E. Swathi, Assist Prof, CSE,
CBIT
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 E.DTD to verify our own data.


Swathi, Assist Prof, CSE,
CBIT
• 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
• CDATA

E. Swathi, Assist Prof, CSE,


CBIT
PCDATA:

1. PCDATA means parsed character data.


think of character data as the text found between the start tag and the
end tag of an XML element.
2. PCDATA is text that WILL be parsed by a parser. The text will
be examined by the parser for entities and markup.
3. Tags inside the text will be treated as markup and entities will be
expanded.
4. However, parsed character data should not contain any &, <, or >
characters; these need to be represented by the &amp; &lt; and &gt;
entities, respectively.

CDATA:

1. CDATA means character data.


2. CDATA is text that will NOT be parsed by a parser. Tags inside
the text will NOT be treated as markup and entities will not be
expanded E. Swathi, Assist Prof, CSE,
CBIT
Elements
In a DTD, elements are declared with an ELEMENT declaration.
Declaring Elements

In a DTD, XML elements are declared with an element declaration with the
following syntax:

<!ELEMENT element-name category> or


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

1. Empty Elements

Empty elements are declared with the category keyword EMPTY:


<!ELEMENT element-name EMPTY>

Example:<!ELEMENT br EMPTY>
XML example:<br />
E. Swathi, Assist Prof, CSE,
CBIT
2.Elements with Parsed Character Data

Elements with only parsed character data are declared with #PCDATA
inside
parentheses:
<!ELEMENT element-name (#PCDATA)>
Example: <!ELEMENT from (#PCDATA)>

3. Elements with any Contents

Elements declared with the category keyword ANY, can contain any
combination of parsable data:

<!ELEMENT element-name ANY>


Example: <!ELEMENT note ANY>

E. Swathi, Assist Prof, CSE,


CBIT
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>

E. Swathi, Assist Prof, CSE,


CBIT
4. Elements with Children (sequences)

Elements with one or more children are declared with the name of the
children elements inside parentheses:

<!ELEMENT element-name (child1)> or


<!ELEMENT element-name (child1,child2,...)>
Example:<!ELEMENT note (to,from,heading,body)>

When children are declared in a sequence separated by commas, the


children must appear in the same sequence in the document. In a full
declaration, the children must also be declared, and the children can also
have children. The full declaration of the "note" element is:

<!ELEMENT note (to, from, heading, body)>


<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
E. Swathi, Assist Prof, CSE,
CBIT
5.Occurrences:

E. Swathi, Assist Prof, CSE,


CBIT
Occurrences cont…..

a. 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.

b. 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.
E. Swathi, Assist Prof, CSE,
CBIT
Occurrences cont…..

c. Declaring Zero or More Occurrences of an 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.

d. 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
E. Swathi,
"message" can occur zero or one timeAssist Prof,
inside theCSE,
"note" element.
CBIT
6. Declaring either/or Content

Example:<!ELEMENT note (to,from,header,(message|body))>

The example above declares that the "note" element must contain a "to"
element, a "from" element, a "header" element, and either a "message" or
a "body" element.

7.Declaring Mixed Content

Example:<!ELEMENT note (#PCDATA|to|from|header|message)*>

The example above declares that the "note" element can contain zero or
more occurrences of parsed character data, "to", "from", "header", or
"message" elements.
E. Swathi, Assist Prof, CSE,
CBIT
• Location of modifiers
1. If modifiers put after the parenthesis it applies to whole group
of elements
<!ELEMEMT student (name|rollno)*>
2. Any no of links or any no of img elements but not both
<!ELEMENT body (link*|img*)>
3. Any no of child link and img elements but they must come in
pairs
<!ELEMENT body (link, img)*>
4. Any no fo child link followed by any number of img element
<!ELEMENT body ( link*, img*)>
5. <!ELEMENT person ( name|(firstname, lastname))>
A person element can have either single element name or a
firstname and lastname.

E. Swathi, Assist Prof, CSE,


CBIT
<studentInf>
<student name=“venkat”
rollno=“123”/>
</studentInf>

E. Swathi, Assist Prof, CSE,


CBIT
Attributes
1.In a DTD, attributes are declared with an ATTLIST declaration.
Declaring Attributes

An attribute declaration has the following syntax:

<!ATTLIST element-name attribute-name attribute-type default-value>

DTD example:<!ATTLIST payment type CDATA "check">


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

The attribute-type can be one of the following:


Type Description
CDATA The value is character data
(en1|en2|..) The value must be one from an enumerated
list
xml: The value is a predefined xml value
ENTITY The value is an entity
E. Swathi, Assist Prof, CSE,
CBIT
The default-value can be one of the following:

Value Explanation
value The default value of the attribute
#REQUIRED The attribute is required
#IMPLIED The attribute is not required
#FIXED value The attribute value is fixed

A Default Attribute Value


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.
E. Swathi, Assist Prof, CSE,
CBIT
#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.

E. Swathi, Assist Prof, CSE,


CBIT
#IMPLIED

Syntax

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

Example
DTD: <!ATTLIST contact fax CDATA #IMPLIED>
Valid XML: <contact fax="555-667788" />
Valid XML: <contact />

Use the #IMPLIED keyword if you don't want to force the author to
include an attribute, and you don't have an option for a default value.

E. Swathi, Assist Prof, CSE,


CBIT
#FIXED

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

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.
E. Swathi, Assist Prof, CSE,
CBIT
2. 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.

E. Swathi, Assist Prof, CSE,


CBIT
example-1
<!DOCTYPE bookstore [

<!ELEMENT bookstore (topic+)>

<!ELEMENT topic (name,book*)>

<!ELEMENT name (#PCDATA)>

<!ELEMENT book (title,author)>

<!ELEMENT title (#CDATA)>

<!ELEMENT author (#CDATA)>

<!ELEMENT isbn (#PCDATA)>

<!ATTLIST book isbn CDATA "0">


]>

E. Swathi, Assist Prof, CSE,


CBIT
<?xml version="1.0"?>
<!DOCTYPE bookstore SYSTEM "http://webserver/bookstore.dtd">
<bookstore>
<topic>
<name>XML</name>
<book isbn="123-456-789">
<title>Mike's Guide To DTD's and XML Schemas<</title>
<author>Mike Jervis</author>
</book>
</topic>
</bookstore>

E. Swathi, Assist Prof, CSE,


CBIT
Example-2

E. Swathi, Assist Prof, CSE,


CBIT
E. Swathi, Assist Prof, CSE,
CBIT
3. DTD Entities
Entities are variable used to define the shortcut names
for text or special characters.

Syntax:

– General entity references start with & and end with


;
– The entity reference is replaced by its true value
when parsed.
– The characters < > & “ ‘ require entity references to
avoid conflicts with the XML application ( parser )

E. Swathi, Assist Prof, CSE,


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

• Ex: <!ENTITY c “CBIT”>


• Instruction: <college> my colllege name is &c;</college>
• Note: An entity has three parts: an ampersand (&), an entity name,
and a semicolon (;).

E. Swathi, Assist Prof, CSE,


CBIT
50
• a. An Internal Entity Declaration Syntax

• <!ENTITY entity-name "entity-value">


• Example
• DTD Example:
• <!ENTITY writer "Donald Duck.">
• <!ENTITY copyright "Copyright W3Schools.">
• XML example:
• <author>&writer;&copyright;</author>

E. Swathi, Assist Prof, CSE,


CBIT
2. External Entity
Included in the different file and referred in the xml
file.
b. An External Entity Declaration
Syntax
<!ENTITY entity-name SYSTEM "URI/URL">
Example

DTD 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>

E. Swathi, Assist Prof, CSE,


CBIT
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>

E. Swathi, Assist Prof, CSE,


CBIT
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>
<length>120</length>
</table>

Both contain a <table>


E. Swathi,element, but the elements have
Assist Prof, CSE,
different content and meaning. CBIT
54
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>
E. Swathi, Assist Prof, CSE,
In the example above, there will beCBIT
no conflict because the two
<table> elements have different names. 55
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

E. Swathi, Assist Prof, CSE,


CBIT
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).

E. Swathi, Assist Prof, CSE,


CBIT
What is an XML Schema?
The purpose of an XML Schema is to define the legal building blocks of an XML
document, just like a DTD.

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

E. Swathi, Assist Prof, CSE,


CBIT
XML Schemas are the Successors of DTDs:

We think that very soon XML Schemas will be used in


most Web applications as a replacement for DTDs.
Here are some reasons:

1. XML Schemas are extensible to future additions


2. XML Schemas are richer and more powerful than
DTDs
3. XML Schemas are written in XML
4. XML Schemas support data types
5. XML Schemas support namespaces
6. XML document that conforms to a particular XML schema is
called “instance document” of that schema

E. Swathi, Assist Prof, CSE,


CBIT
example

DTD file

E. Swathi, Assist Prof, CSE,


CBIT
XML scheme- employee.xs

E. Swathi, Assist Prof, CS


CBIT
XML document

E. Swathi, Assist Prof, CSE,


CBIT
XML schema structure
XML schema element
➢ <schema> element is used as a root element in XML schema
<?xml version="1.0"?>

<xs: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>
E. Swathi, Assist Prof, CSE,
CBIT
The <schema> element may contain some attributes.
A schema declaration often looks something like this:
<?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
must be namespaceE. qualified.
Swathi, Assist Prof, CSE,
CBIT
Element declaration
➢ In XSD to declare the elements element is used
➢ There are two types of elements
1. simple element 2. complex element
1. Simple element- A simple element is an XML element that can
contain only text. It cannot contain any other elements( child
elements) or attributes
- when no child element the element is designated with attribute
name and data type is designated with the type attribute.

syntax
<xs:element name="xxx" type="yyy"/>

E. Swathi, Assist Prof, CSE,


CBIT
Built in data types in XSD

E. Swathi, Assist Prof, CSE,


CBIT
E. Swathi, Assist Prof, CSE,
CBIT
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"/>

E. Swathi, Assist Prof, CSE,


CBIT
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"/>

E. Swathi, Assist Prof, CSE,


CBIT
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.
➢ If an XML element is of type "xs:date" and contains a string
like "Hello World", the element will not validate.

xs:element name=“rollno">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value=“1"/>
<xs:maxInclusive value=“70"/>
</xs:restriction> <xs:element name="car">
</xs:simpleType> <xs:simpleType>
/xs:element> <xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
E. Swathi, Assist Prof, CSE,
CBIT
</xs:element>
E. Swathi, Assist Prof, CSE,
CBIT
XSD- Complex elements
A complex element contains other elements and/or attributes.

There are four kinds of complex elements:


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> ....
</description>
Note: Each of these elements may contain attributes as well!

E. Swathi, Assist Prof, CSE,


CBIT
How to Define a Complex Element

Complex elements are defined in two ways

<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
he "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>
</xs:element>
➢only the "employee" element can use the specified complex type.
➢<sequence> indicator. This means that the child elements must
appear in the same order as they are declared.
E. Swathi, Assist Prof, CSE,
CBIT
. 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
<xs:element name="employee" type="personinfo"/>
<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>
</xs:complexType>E. Swathi, Assist Prof, CSE,
CBIT
1.empty elements
<product prodid="1345" />

<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid“
type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>

E. Swathi, Assist Prof, CSE,


CBIT
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>

E. Swathi, Assist Prof, CSE,


CBIT
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 within the
impleContent element, like this:
OR
xs:element 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>
E. Swathi, Assist Prof, CSE,
CBIT
Complex Text-Only Elements
it contains simple
content(text/attributes
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>
E. Swathi, Assist Prof, CSE,
CBIT
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>
E. Swathi, Assist Prof, CSE,
CBIT
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

E. Swathi, Assist Prof, CSE,


CBIT
order indicators- defines the order of
elements
1.All 2.Choice 3.Sequence
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>

E. Swathi, Assist Prof, CSE,


CBIT
2. Choice Indicator
<choice> indicator specifies that either one child element or
ther can occur:
3. Sequence Indicator

The <sequence> indicator specifies that the child elements


must appear in a specific order:

urrence Indicators- define how often an element can occur.


maxOccurs> indicator specifies the maximum number of times an element
ccur:
minOccurs> indicator specifies the minimum number of times an element
occur:

E. Swathi, Assist Prof, CSE,


CBIT
Family.xsd <?xml version="1.0" encoding="ISO-8859-1
Family.xml <xs:schema
<?xml version="1.0" encoding="ISO-8859-1"?> xmlns:xs="http://www.w3.org/2001/XMLS
a"
<persons elementFormDefault="qualified">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" <xs:element name="persons">
xsi:noNamespaceSchemaLocation="family.xsd"> <xs:complexType>
<xs:sequence>
<person> <xs:element name="person"
<full_name>Hege Refsnes</full_name> maxOccurs="unbounded">
<child_name>Cecilie</child_name> <xs:complexType>
</person> <xs:sequence>
<xs:element name="full_name"
<person> type="xs:string"/>
<full_name>Tove Refsnes</full_name> <xs:element name="child_name"
<child_name>Hege</child_name> type="xs:string"
<child_name>Stale</child_name> minOccurs="0" maxOccurs="5"/>
<child_name>Jim</child_name> </xs:sequence>
<child_name>Borge</child_name> </xs:complexType>
</person> </xs:element>
</xs:sequence>
<person> </xs:complexType>
<full_name>Stale Refsnes</full_name> </xs:element>
</person>
</xs:schema>
E. Swathi, Assist Prof, CSE,
</persons> CBIT
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>
must define an all, choice, or sequence element inside the group declaration.
s:group name="persongroup">
xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
/xs:sequence>
xs:group>

E. Swathi, Assist Prof, CSE,


CBIT
ter you have defined a group, you can reference it in another definition,
e this:

<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>

<xs:element name="person" type="personinfo"/>

<xs:complexType name="personinfo">
<xs:sequence>
<xs:group ref="persongroup"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
E. Swathi, Assist Prof, CSE,
CBIT
Attribute Groups

<xs:attributeGroup name="groupname">
...
</xs:attributeGroup> <xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname"
type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

Used in other definition also like this


<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

<xs:element name="person">
<xs:complexType>
<xs:attributeGroup ref="personattrgroup"/>
</xs:complexType>
</xs:element>
E. Swathi, Assist Prof, CSE,
CBIT
Presenting XML
➢ XSL stands for extensible stylesheet language used to present XML as
HTML.
CSS = Style Sheets for HTML
➢ HTML uses predefined tags, and the meaning of each tag is well understood
➢ Adding styles to HTML elements are simple. Telling a browser to display an element in
a special font or color,
XSL = Style Sheets for XML
XSL describes how the XML document should be displayed!

➢ XSL is more than CSS for HTML


It contains
• XSLT - a language for transforming XML documents
• XPath - a language for navigating in XML documents
• XSL-FO - a language for formatting XML documents

E. Swathi, Assist Prof, CSE,


CBIT
<?xml version="1.0" encoding="ISO-
8859-1"?>
<?xml-stylesheet type="text/xsl"
href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>

E. Swathi, Assist Prof, CSE,


CBIT
XSLT- extensible stylesheet
language transformation
➢ XSLT is a language for transforming XML documents into XHTML
documents or to other XML documents or any other document
which could understand by the browser.
➢ Write XML file and write XSL file link to XML file by using
following statement in XML file

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


<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>

⚫An XSLT style sheet is an XML document with a root element,


xsl:stylesheet/xsl:transform , which defines namespaces

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

E. Swathi, Assist Prof, CSE,


CBIT
XSL- style sheet
XSLT <xsl:template> Element
➢ An XSL style sheet consists of one or more set of rules that are
called templates.
➢ Template contains rules to apply when the particular node is
matched.
➢ The <xsl:template> element is used to build templates.
➢ Content of template element specifies what is to be placed in the
output document in HTML
– E.g. to match the root node of the XML doc:
– <xsl:template match = "/"> …

E. Swathi, Assist Prof, CSE,


CBIT
Xsl file
ml file <?xml version="1.0" encoding="ISO-8859-1"?>
xml version="1.0" encoding="ISO- <xsl:stylesheet version="1.0"
59-1"?> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- Edited by XMLSpy® -->
talog> <xsl:template match="/">
d> <html>
tle>Empire Burlesque</title> <body>
tist>Bob Dylan</artist> <h2>My CD Collection</h2>
untry>USA</country> <table border="1">
mpany>Columbia</company> <tr bgcolor=“green">
ice>10.90</price> <th>Title</th>
ear>1985</year> <th>Artist</th>
d> </tr>
atalog> <tr>
<td>.</td>
<td>.</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

E. Swathi, Assist Prof, CSE,


CBIT
<xsl:value-of> Element
extract the value of an XML element and add it to the output stream
of the transformation:

<?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>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
</tr>
</table>
</body> </html>
</xsl:template>
</xsl:stylesheet> E. Swathi, Assist Prof, CSE,
CBIT
<xsl:for-each> Element
• <xsl:for-each> element can be used to select every
XML element of a specified node-set:
<?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>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th> </tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table> </body> </html>
</xsl:template> E. Swathi, Assist Prof, CSE,
</xsl:stylesheet> CBIT
<xsl:sort> Element
• <xsl:sort> - sort the elements
• add an <xsl:sort> element inside the <xsl:for-each> element
in the XSL file:

example
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>

Select element indicates the what element to sort on

E. Swathi, Assist Prof, CSE,


CBIT
<xsl:if> Element

• <xsl:if> Element to put conditions against the


context of XML elements
• Use this element inside of the <xsl:for-each>

<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>

E. Swathi, Assist Prof, CSE,


CBIT
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:if test="price &gt; 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
E. Swathi, Assist Prof, CSE,
CBIT
<xsl:apply-templates>
• The <xsl:apply-templates> element applies a template to the
current element or to the current element's child nodes.
• If we add a select attribute to the <xsl:apply-templates>
element it will process only the child element that matches
the value of the attribute
• the select attribute to specify the order in which the child
nodes are processed.

<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>

E. Swathi, Assist Prof, CSE,


CBIT
xsl:apply-templates Example
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="name">
<xsl:value-of select="last_name"/>,
<xsl:value-of select=“middle_name"/>,
<xsl:value-of select="first_name"/>
</xsl:template>

<!-- Apply templates only to name children -->

<xsl:template match="person">
<xsl:apply-templates select="name"/>
</xsl:template>
E. Swathi, Assist Prof, CSE,
CBIT
</xsl:stylesheet>
xsl:choose, xsl:when, xsl:otherwise:

We can also select content using:

<xsl:choose>
<xsl:when test=criteria>
</xsl:when>

<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>

The test attribute works in the same fashion as xsl:if

E. Swathi, Assist Prof, CSE,


CBIT
<xsl:template match="/">
<html> <body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price &gt; 10">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
E. Swathi, Assist Prof, CSE,
CBIT

You might also like