Today's Topics:
Homework:
Markups
Understanding markups
Open, Closed markups
Problems in markups
Specific and generalized markup
SGML
Important Points:
XML_r15 Page 1
What is markup?
It refers to marking up (tagging) documents in the print and design world.
Purposes:
To modify look and formatting of text
To describe the structure and meaning of a documents for output to some medium such as printer or world
wide web
How Markup works?
Markup consists of codes or tags - these are added to text to change the look or meaning of the tagged text. For
example - <b>IDB-BISEW</b> is a HTML tagged text which is used to change the text IDB-BISEW into bold faced.
Generally a processor is used to interpret the marked up document and display or process the content.
Some common markups :
RTF - Rich Text Format
Word processors use RTF to format documents for print or display
HTML - Hyper Text Markup Language
HTML is used to format and layout document in world wide web. HTML are generally processed by
browsers.
What is a markup processor?
A markup processor interprets the meaning of the markup codes.
The markup codes tell the processor just about everything about the application needs to know about it.
A processor does not processes all markups. HTML documents are processed by browsers, RTF documents
are processed by word processors.
What is the difference between open and closed language?
The Specifications for an open language is publicly available and any vendor can create an processor for it.
For example - RTF, HTML.
The specification for a closed language is not publicly available and vendors do no have access to the rules of
the markup language and can not create a processor for it. For example - Microsoft Word uses an closed
markup for its documents and only Microsoft' s word processor only can process word documents.
Problems in Markups
Difficult to identify the structure of the document
Looseness in structure makes it difficult to interpret
Lack of rules makes difficult to create a processor for it
Specific and generalized markup
Specific markup languages are used to generate code that is specific to particular application and device. They are
often built to serve a particular need. For example - RTF is used to format texts for printing and displaying, HTML is
used to format documents for web.
Generalized markup languages are used to describe the structure and meaning of a document but they do not
define how the document should be processed. SGML , XML are generalized markup languages.
SGML
Stands for Standard Generalized Markup Language
SGML is used for the structure of document but not its formatting
It is extensible - it allows a author to create his/her own structure
It is actually a metalanguage - it allows you to create your language.
Both HTML and XML originated from SGML. XML is a subset of SGML. HTML is an application of SGML
HTML vs. SGML
SGML
Generalized language
Extensible
Used to describe the structure of a document
HTML
Not generalized
Not extensible
Used to format and layout documents on web
There are also differences in coding
XML_r15 Page 2
Homework:
Today's Topics:
Important Points:
XML_r15 Page 3
Understanding XML
HTML vs. XML
SGML vs. XML
Goals of XML
Use of XML
XML standards
What is XML?
XML is a subset of SGML
XML is used to describe the structure and meaning of document
XML allows you to create own vocabularies (tag)
XML is extensible; you can create other language using XML
HTML vs. XML
Look at the HML below
Look at the XML below
<HTML>
<books>
<HEAD><TITLE>Books in store</TITLE>
<book>
<name>XML</name>
</HEAD>
<BODY>
<price>700.00</price>
<TABLE>
</book>
<book>
<CAPTION>Price
<name>HTML</name>
List</CAPTION>
<price>350.00</price>
<TR>
<TH>Name</TH><TH>
</book>
Price</TH>
</books>
</TR>
<TR>
<TD>XML</TD>
<TD>700.00</TD>
</TR>
<TR>
<TD>HTML</TD>
<TD>350.00</TD>
</TR>
</TABLE>
</BODY>
</HTML>
The HTML above tells the browser how to
format the contents
HTML is about displaying
XML_r15 Page 4
XML_r15 Page 5
DTD Basics
Saturday, July 14, 2012
8:41 PM
Homework:
Today's Topics:
Important Points:
XML_r15 Page 6
What is DTD
Valid documents
Internal and External DTD
How to write DTD
Anatomy of DTD
Working with element and
element content model
Sequence
Choice
DTD - Basics
Saturday, July 14, 2012
8:41 PM
XML_r15 Page 7
Internal DTD
Lets write some DTD rules
Ex. #1 Internaldtd.xml
<?xml version='1.0'?>
<!DOCTYPE contact [
<!ELEMENT
contact (#PCDATA)>
]>
<contact>907865</contact>
The DTD declaration says that the document is about contact (root element or document element)
Inside definition, it declares an element contact whose content model is declared inside parenthesis ()
contact can contain PCDATA ( Parsed character data)
[You already have learnt about PCDATA]
External DTD
Ex. #2 contact.dtd
<!ELEMENT
contact (#PCDATA)>
externaldtd.xml
<?xml version='1.0'?>
<!DOCTYPE contact SYSTEM "contact.dtd">
<contact>907865</contact>
The contact.dtd defines only one element
The contact.dtd is referenced in externaldtd.xml
NOTICE use of SYSTEM, it is called SYSTEM identifier
External DTD can be referenced using
1. SYSTEM identifiers: it is used to reference a DTD file on your local computer
2. PUBLIC identifier : It uses a specialized identifier to locate actual DTD file
Example:
<!DOCTYPE trainee PUBLC "-//IDB-BISEW//Trainee Description //EN" "trainee.dtd">
Lets work with example
Ex #1
Open notepad and write
<?xml version="1.0" ?>
<!DOCTYPE person [
<!ELEMENT person (name, phone ) >
<!ELEMENT name (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>
<person>
<name>Habibul Haq</name>
<phone>9870760</phone>
</person>
Save the file and name it internaldtd.xml
Validate it
Is it successful?
Ex #2
Open notepad and write
<!ELEMENT person (name, phone ) >
<!ELEMENT name (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
Save the file and name it person.dtd
Open notepad and write
<?xml version="1.0" ?>
<!DOCTYPE person SYSTEM "person.dtd">
<person>
<name>Habibul Haq</name>
<phone>9870760</phone>
</person>
Save the file and name it externaldtd.xml
Validate it
Is it successful?
If not, why?
XML_r15 Page 8
Anatomy of DTD
Element
Content Model
Parsed Character
Empty
Element (s)
Sequence
CDATA
Choice
Cardinality ([nill] , *, ?, +)
Mixed
Any
Attribute
Values
Types
ID
IDREF
IDREFS
ENTITY
ENTITIES
NMTOKEN
NMTOKENS
Enumerated List
Constraints
Entity
Notation
XML_r15 Page 9
DEFAULT VALUES
Fixed Values
Required (REQUIRED
Optional (IMPLIED)
XML_r15 Page 10
Homework:
Today's Topics:
Important Points:
XML_r15 Page 11
Anatomy of DTD
Working with element and
element content model
Sequence
Choice
Cardinality
Attributes
Attribute - data types
Constraints
Entities
What are they
Types
Syntax
Workouts
DTD Basics
Saturday, July 14, 2012
9:41 PM
Cardinality indicators
? means 0 or 1
+ means 1 or more
* means 0 or more
If no cardinality is defined then the element must appear
once and only once
Lets work with an example
Ex. #6 cardinality2.dtd
<!ELEMENT trainee (id, name, contact?, email+)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT contact (#PCDATA)>
<!ELEMENT email (#PCDATA)>
Description
CDATA
ID
10:42 PM
XML_r15 Page 12
<?xml version="1.0"?>
<!DOCTYPE person [
<!ELEMENT person (name+, profession*)>
<!ELEMENT name EMPTY>
<!ATTLIST name first CDATA #REQUIRED
last CDATA #REQUIRED>
DTD -Example
Tuesday, July 29, 2008
10:42 PM
<books >
<book bid="b001" print="paperback">
<isbn>109-998-997-8080</isbn>
<name>All Quiet on The Western Front</name>
<author>
<first></first>
<last></last>
</author>
<description>
This is a <b>Great</b> book on war!!!
</description>
<publish-date><date></date></publish-date>
</book>
<book bid="b002" print="whiteprint">
<isbn>109-998-997-8080</isbn>
<name>All Quiet on The Western Front</name>
<author>
<first></first>
<last></last>
</author>
<author>
<first></first>
<last></last>
</author>
<description>
This is a <b>Great</b> book on war!!!
</description>
<publish-date>
<edition>1st</edition>
<date>12-02-04</date>
<edition>2nd</edition>
<date>12-02-06</date>
</publish-date>
</book>
</books>
Entities
An entity is a declared content that is referenced in the
document.
Entities can be parsed or unparsed and also can be part
of the document or external to the document.
An entity is declared once but can be referenced many
times.
When an entity is referenced, processor retrieves the
content of the entity and the content is inserted at the
point where the entity is referenced
Various types of entities
Parsed entities
The content of an parsed entity is extracted
(parsed) by XML parsers. So it can not contain
symbols or keywords reserved for XML.
Unparsed entities
An unparsed entity is often called binary entity. Its
content is not directly parsed by XML parsers. It
uses extra information provided by NOTATION
declaration to manipulate an unparsed entity. An
unparsed generally references binary file such
images.
XML_r15 Page 13
XML_r15 Page 14
Some problems
#1
A family consists of father, mother and
children. Some families have no children
Parameterentity.dtd
<!ENTITY % carnv-model " ( name, prey,
habitat)">
<!ENTITY % harv-model " ( name, food, habitat )
">
<!ELEMENT animals ( carnivore | harbivore ) >
<!ELEMENT carnivore %carnv-model; >
<!ELEMENT harbivore %harv-model; >
<!ELEMENT name (#PCDATA)>
<!ELEMENT prey (#PCDATA)>
<!ELEMENT food (#PCDATA) >
<!ELEMENT habitat (#PCDATA)>
Parameterentity.xml
<?xml version="1.0" ?>
<!DOCTYPE animals SYSTEM
"parameterentity.dtd">
<animals>
<carnivore>
<name>Chitah</name>
<prey>Springbuck, Gazelle</prey>
<habitat>African grass
plain</habitat>
</carnivore>
</animals>
Important points
XML_r15 Page 15
Homework:
Today's Topics:
Important Points:
XML_r15 Page 16
JavaScript basics
DOM basics
Accessing XML data using JavaScript
Workouts
Introduction to JavaScript
Saturday, July 14, 2012
8:00 AM
Variables
JavaScript is not a strongly typed language
You do need to declare data type for your variables
To create a variable, use var keyword.
var a;
a = 213;
var traineeName = "Enamul Haque";
A variable may not be a Javascript reserved word or
begin with a number or any symbol other than $ and
_ signs.
You can store any type of value in same varibale
JavScript
Operator Description
Web Page
XML
JavaScript
Subtraction
Multiplication
Division
++
Increment
--
Decrement
var x =
var y =
// y=5,
var x =
5;
x++;
x=6
5; var y = ++x;
// y=6, x=6
Asignment, var x; x = 5;
==
Equality, is x==5?
!=
>
<
<=
>=
&&
||
<script type='text/javascript'
src='scriptfile.js'></script>
XML_r15 Page 17
Introduction to JavaScript
var x=5;
// Everything from the
// to end of line is ignored
Decision constructs
The if statement lets you execute a block of code if
some test is passed.
var x=5;
if (x==5)
{
alert('x is equal to 5!');
}
/*
Comment
Comment
*/
Loops: WHILE
while loops in Javascript also follow basic C syntax
and are easy to understand and use.
The while loop will continue to execute until its test
condition evaluates to false or the loop encounters a
break statement.
var x
while
{
x
}
var x
while
{
x
= 1;
(x<5)
= x +1;
= 1;
(true)
= x + 1;
if (x>=5) {
break;
Block quotes begin a comment block with a slashasterisk (/*) and Javascript will ignore everything from
the start of the comment block until it encounters an
asterisk-slash (*/).
Loops: FOR
The for loop follows basic C syntax, consisting of an
initialization, an evaluation, and an increment.
Comments
Javascript supports two types of comments.
Double-slashes (//) tell javascript to ignore
function doSomething()
{
// code
}
function doMoreThings( a, b )
{
//code
}
Output (writeln)
The first method is to use the document.writeln(string)
command.
This can be used while the page is being constructed.
Here's how a simple web-page will look...
<html>
<head> </head>
<body>
<script type='text/javascript'>
document.writeln('Hello World!');
</script>
</body>
</html>
Function
A JavaScript function is written with function keyword
function can accept parameters
Output (alert)
The second method is to use a browser alert box.
<html>
<head> </head>
<body>
<script type='text/javascript'>
alert('Hello World!');
</script>
</body>
</html>
Output (getElementById)
The last method is the most powerful and the most
complex (but don't worry, it's really easy!).
You can give each and every element in HTML a
unique identifier (an ID), and Javascript can find
elements you have labeled and let you manipulate
them.
XML_r15 Page 18
Introduction to JavaScript
Saturday, July 14, 2012
8:00 AM
JavaScript Events
JavaScript supports events
The documents, for m element fires events
When a HTML page finished, load event is fired. When
a user clicks on a button, a click event is fired.
To write code for an event, we use onxxx attribute,
xxx is the eveent name
For example
<input type"button " value="Click Here"
onclick="alert( 'Hi');">
Things to know
JavaScript is case sensitive
A JavaScript statement ends with ; (though ; is not
required , if you write on statement on one line. But
putting ; is good practice)
<html>
<head>
<title>Test</title>
</head>
<body>
<form name="formLogin">
User name: <input type=text
name="txtname"><br>
Password: <input type="password"
name="txtpass"><br>
<input type="submit"
value="Login">
</form>
<div id="msg"></div>
</body>
</html>
JavaScript Basics
It is a object based language.
The top level object is window.
window.location - returns the address bar of the
browser
Window.documet - it returns the document part of
the web page
window is the default object, so, window.document is
equivalent document.
An example:
Suppose you created a web page using the following
HTML:
XML_r15 Page 19
JavaScript Basics
Saturday, July 14, 2012
8:00 AM
<books>
<book id="B01">
<name>XML</name>
<price>700.00</price>
</book>
<book id="B02">
<name>HTML</name>
<price>300.00</price>
</book>
</books>
book
book
name
XML
HTML
</books>
root.childNodes - returns two books under the top
books element
var bkXml = root.chilNodes.item(0)
Now bkXml holds
<book id="B01">
<name>XML</name>
<price>700.00</price>
</book>
XML_r15 Page 20
<html>
<head> <title>Book info</title>
</head>
<body>
<h3>Book info</h3>
Name: <span id="bn"></span>
<br>
Price: <span id="bp"></span>
</body>
</html>
Scripting XML
Saturday, July 14, 2012
8:34 PM
XML_r15 Page 21
Habib, BITL
Homework:
Today's Topics:
Name
hq@gmail.com
107899 Enamul
1
Haque
enam@gmail.c
om
Important Points:
Lecture Topic:
During the lecture, take notes here.
XML_r15 Page 22
Now roots holds the all the book elements along with
parent element books
You wrote
var book1 = root.childNodes.item(0)
book1 holds
<book id="B01" genre="Academic">
<name>C Sharp</name>
<price>900.00</price>
<publisher>Que</publisher>
</book>
You wrote
var bid = book.getAttribute("id")
bid holds value "B01"
getAttribute("attributname") - returns attribute value
of a node
You wrote
var books = root.getElementsByTagName("book")
books holds two book elements under books
getElementsByTagName( "elementname") - returns a
set of elements that matches the supplied tag
You wrote
var books = root.selectNodes( "book" )
books holds two book element under books
selectNodes( "pattern") - returns a set of elements that
matches the supplied pattern
[We will explore patterns later]
You wrote
var book =
root.selectSingleNode( "book[name='C Sharp']" )
book holds
<book id="B01" genre="Academic">
<name>C Sharp</name>
<price>900.00</price>
<publisher>Que</publisher>
</book>
selectSingleNode("pattern") - returns an
element that matches the supplied pattern
Now we will explore with an example
We will use books.xml as shown on the left
And We will create book_search.html
The interface will look like as below
Book Store
Seach Book B02
By Id
OR
By Name
Search result:
Id: B02
Name: All Quiet On the Western Front
Price: 1600.00
Publisher: IPL
XML_r15 Page 23
Scripting XML
Saturday, July 14, 2012
9:00 PM
<html>
<head>
<title>Book Store</title>
<script>
var xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
xmlDoc.load( "books_data.xml" );
var books= xmlDoc.documentElement;
function searchById()
{
var id = document.frm.bid.value;
var b=books.selectSingleNode( "book[@id='" + id +"']");
if( b == null )
{
document.getElementById( "divmsg" ).innerHTML = "book not found";
return;
}
var s = "Seach Result: <br>";
s += "ID: " + b.getAttribute("id" ) + "<br>";
s += "Genre: " + b.getAttribute("genre" )+"<br>";
s += "Name: " + b.childNodes.item(0).text+"<br>";
s += "Price: " + b.childNodes.item(1).text+"<br>";
document.getElementById( "divmsg" ).innerHTML = s;
}
function searchByName()
{
var bname = document.frm.bname.value;
var b=books.selectSingleNode( "book[name='" + bname +"']");
if( b == null )
{
document.getElementById( "divmsg" ).innerHTML = "book not found";
return
}
var s = "Seach Result: <br>";
s += "ID: " + b.getAttribute("id" ) + "<br>";
s += "Genre: " + b.getAttribute("genre" )+"<br>";
s+= "Name: " + b.childNodes.item(0).text+"<br>";
s+= "Price: " + b.childNodes.item(1).text+"<br>";
document.getElementById( "divmsg" ).innerHTML = s;
}
</script>
</head>
<body>
<form name="frm">
Seach Book : <input type="text" name="bid"><input type=button value="By ID" onclick="searchById();"> or
<input type="text" name="bname"> <input type=button value="By Name" onclick="searchByName();">
<br>
<br> <div id=divmsg style="font-family: tahoma; font-size: 8pt; border: 1px solid #e4e4e4; width:70%;padding:
5px;"></div>
</form>
</body>
<html>
XML_r15 Page 24
XML_r15 Page 25
XML as Data
Saturday, July 14, 2012
9:00 AM
<p>
Title: <span datasrc="#xmldso" datafld="id"></span>
<br />Artist:<span datasrc="#xmldso" datafld="name"></span>
<br />Year:<span datasrc="#xmldso" datafld="price"></span>
</p>
<p>
<input type="button" value="Previous book"onclick="moveprevious()" />
<input type="button" value="Next book"onclick="movenext()" />
</p>
</table>
</body>
</html>
As HTML document loads the first record from the XML file is shown
Whenever an user clicks on Next Book button, the movenext() javascript function is called
It first get record from the using x.recordset
It checks whether currently last record is showing or not. x.recordcount returns the number rows in x and
x.abosuluteposition return the row number currently showing. Position satrts at 1. so if x.absoluteposition
< x.recordcount is true, we are not in the last record position and we can call x.movenext(). movenext()
method moves the position to next record.
Whenever an user clicks on Previous Book button, moveprevious() javascript function is called
It checks whether we are at first record position or not
If we are not at first position it moves to the previous record position
XML_r15 Page 26
Homework:
Today's Topics:
Address
Mark
Tracey
Important Points:
XML_r15 Page 27
XSL
What is it?
Templates and patterns
XSL loop
Multiple template
XSL functions
XSL Basics
Saturday, July 14, 2012
8:21 PM
Trainee Info
Transformation
XML_r15 Page 28
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transf
orm" version="1.0">
<xsl:template match="/">
<html>
<head><title>Trainee Info</title>
</head>
<body>
<xsl:for-each select="trainees/trainee">
<div style="border-bottom: solid
1px #e4e4e4;">
<b><xsl:value-of
select="name"/></b><br/>
Trainee ID: <xsl:value-of
select="@id"/><br/>
Batch: <xsl:value-of
select="batch"/><br/>
Round: <xsl:value-of
select="round" />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XSL
Saturday, July 14, 2012
9:27 PM
Lets explore
Example #2
We have XML file - trainees.xml
<?xml version="1.0"?>
<trainees>
<trainee id="1098769">
<name>Md. Enam Ahmed</name>
<course>ESAD-CS</course>
<round>7</round>
</trainee>
<trainee id="1098709">
<name>Hasan IQbal</name>
<course>ESAD-VB</course>
<round>8</round>
</trainee>
</trainees>
XML_r15 Page 29
XSL
Saturday, July 14, 2012
11:06 PM
XML_r15 Page 30
XSL - Chapter 08
Saturday, July 14, 2012
9:00 AM
Today's Topics:
Homework:
You have the following xml
<?xml version="1.0"?>
<tsps>
<tsp code="BITL">
<name>Bhuiyan IT Ltd.</name>
<address> H 59/A, H 7/A, Dhanmondi</address>
<web>www.bit.com</web>
</tsp>
<tsp code="HTCL">
<name>Hi-Tech It Ltd.</name>
<address>12th Fl. GB Bhaban, Mirpur</address>
<web>www.htcl.com</web>
</tsp>
</tsps>
Show the data in xml as below (using XSL)
TSP
Code
Address
Web
Important Points:
Lecture Topic:
During the lecture, take notes here.
XML_r15 Page 31
Conditional Processing
xsl:if
xsl:choose
Sorting
xsl:if element
The xsl:if element test a Boolean condition, if condition is
true, the contents inside the xsl:if construct is added to
result tree otherwise not
Syntax:
<xsl:if test="boolean expresson">
.
</xsl:if>
You wrote
<xsl:if test="result = 'A+'"></xsl:if>
If the value of the node result is equal to A+, the
contents inside the xsl:if will be processed
= is used to test equality
Legal operators are
= (equal)
!= (not equal)
< less than
> greater than
Lets work with an example
Example #6
Lets create Books.xml
<?xml version="1.0"?>
<books>
<book isbn="10-90-887-909" available="yes">
<name>SQL ins and outs</name>
<author>Clerk</author>
<listprice>24.99</listprice>
<price>17.55</price>
<edition>4</edition>
<publisher>QUE</publisher>
</book>
<book isbn="10-90-3467-819" available="no">
<name>Programming practice</name>
<author>Dave Hart</author>
<listprice>24.99</listprice>
<price>17.55</price>
<edition>4</edition>
<publisher>Prentice Hall</publisher>
</book>
<book isbn="10-90-3467-819" available="yes">
<name>OOSAD using UML</name>
<author>Bennet</author>
<listprice>24.99</listprice>
<price>17.55</price>
<edition>3</edition>
<publisher>Prentice Hall</publisher>
</book>
</books>
Author
Price
Edition
Publisher
Clerk
$17.55
QUE
OOSAD using
UML
Bennet
$17.55
Prentice
Hall
XML_r15 Page 32
xsl:choose element
xsl:choose allow testing multiple condition
It is like switch case statement in c#
Syntax <xsl:choose>
<xsl:when test="condition 1">
..
</xsl:when>
<xsl:when test="condition 2">
..
</xsl:when>
.
<xsl:otherwise>
...
<xsl:otherwise>
</xsl:choose>
If test ="condition 1" evaluates to true
<xsl:when test="condition 1"> ..</xsl:when> is processed and
proceesing braks out from xsl:choose block
If test ="condition 1" evaluates to false, then test ="condition
2" in the next xsl:when block is tested
If all the xsl:when block fail, <xsl:otherwise></xsl:otherwise>
is processed.
Lets work with another example
Example #7
Lets create books1.xsl as below
Lets create Books.xml
<?xml version="1.0"?>
<books>
<book isbn="10-90-887-909" available="yes">
<name>SQL ins and outs</name>
<author>Clerk</author>
<listprice>24.99</listprice>
<price>17.55</price>
<edition>4</edition>
<publisher>QUE</publisher>
</book>
<book isbn="10-90-3467-819" available="no">
<name>Programming practice</name>
<author>Dave Hart</author>
<listprice>24.99</listprice>
<price>17.55</price>
<edition>4</edition>
<publisher>Prentice Hall</publisher>
</book>
<book isbn="10-90-3467-819" available="yes">
<name>OOSAD using UML</name>
<author>Bennet</author>
<listprice>24.99</listprice>
<price>17.55</price>
<edition>3</edition>
<publisher>Prentice Hall</publisher>
</book>
</books>
XML_r15 Page 33
Price
Edition
Publisher
Author
$17.55
QUE
Program Dave
ming
Hart
practice
$17.55
Prentice
Hall
OOSAD
using
UML
$17.55
Prentice
Hall
Bennet
xsl:sort
Saturday, July 14, 2012
9:00 AM
Sorting - xsl:sort
xsl:sort is used to specify sort order for node-sets
xsl:sot is used with xsl:apply-templates and xsl:for-each
elements
Syntax:
<xsl:sort select="traineeid" /> - it instructs to a node-set
based tha values of traineeid field, by default sort will in
ascending order
<xsL; sort select="price" data-type="number"
order="descending" /> - it instructs to a node-set based
the values of price field whose data type is number, sort
will in descending order
<xsL; sort select="location" data-type="text"
order="descending" /> - it instructs to a node-set based
the values of location field whose data type is text
Lets work with an example
Example #9
Lets create books2.xml
<?xml version="1.0" ?>
<books>
<book>
<name>XML</name>
<price>240.00</price>
</book>
<book>
<name>HTML</name>
<price>190.00</price>
</book>
<book>
<name>UML</name>
<price>560.00</price>
</book>
<book>
<name>WML</name>
<price>370.00</price>
</book>
<book>
<name>VRML</name>
<price>140.00</price>
</book>
<book>
<name>SGML</name>
<price>340.00</price>
</book>
</books>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head><title>Using xsl:sort</title></head>
<body>
<h4>As they are in XML file</h4>
<xsl:for-each select="books/book">
<xsl:value-of select="name"/> - $<xsl:value-of
select="price"/>
<br/>
</xsl:for-each>
<h4>Sorted on name in ascending order:</h4>
<xsl:for-each select = "books/book">
<xsl:sort select="name" data-type="text"
order="ascending"/>
<xsl:value-of select="name" /> $<xsl:value-of select="price" /><br/>
</xsl:for-each>
<h4>Sorted on price in descending order:</h4>
<xsl:for-each select = "books/book">
<xsl:sort select="price" data-type="number"
order="descending"/>
<xsl:value-of select="name" /> $<xsl:value-of select="price" />
<br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Change the xml file as below
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<books>
.
</books>
The output will be
XML_r15 Page 34
Homework:
Today's Topics:
Schema
Need for Schema
Goals
Structure of Schema Documents
Workouts
complexType
Elements
Sequence
Attributes
Data Type
Occurrence
simpleType
Restrictions
Important Points:
Lecture Topic:
During the lecture, take notes here. Append extra page(s) if required
XML_r15 Page 35
XML-Data - Schema
Saturday, July 14, 2012
9:00 AM
XML-Data
An specific application of XML
It is used create schema
Schema
A schema identifies the structure and constraints of a
particular XML document
Why use Schema
We used DTD to define structure of our XML documents
There are some limitations
It does not use XML syntax
It does not have rich data type
It does not apply constraints like - data length,
pattern
Primary key , relation are not supported
And that is why we use Schema
XSD - XML Schema Definition
XSD is used to define structure and constraints of XML
Documents
XSD is more powerful than DTD
Writing XSD
To create a XSD, you have to import a URI of schema
namespace http://www.w3.org/2001/XMLSchema
The schema element
Everything in a Schema document must in side the
schema element of W3C's XMLSchema namespace
<?xml version="1.0" ?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- Schema Definitions Here -->
</xs:schema>
In the schema above we use the standard namespace (xsd),
and the URI associated with this namespace is the Schema
language definition, which has the standard value of
http://www.w3.org/2001/XMLSchema.
Element definitions
To Define an element we use <xsd:element >
<xsd:element name="person" />
Here we defined an element person which have data
type string and string data type is prefixed with xsd
as it is predefined data type in XML Schema
Element can have data type specified by type
attribute
<xsd:element name="person" type="xsd:string" />
[Look how data tyep is specified, we have written
xsd: string. String data type is impoted from
XMLSchema. We will explore all the supported data
type later]
Element cardinality is defined by two attribute
minOccurs - can have an integer value, 0
means the element may not appear. Default
value is 1
maxOccurs - an integer values or unbounded
can be specified. Unbounded means the
element can appear any number of times.
Default value is one
<element name="trainee" minOccurs="0"
maxOcuurs="15" />
The element trainee may not appear or it can
be there 15 times
A element can contain complexType or simpleType
complexType
When a element contains other element(s) or
attribute(s), the element is a complexType
A complexType contains defines allowable elements
sequence
Sequence specifies the order in which elements
appear
choice
Allows elements to choose from a set
simpleType
allows you create your own data type from existing
xsd data types.
Attribute definitions
Attributes similar to elements
Attribute use can controlled using the use attribute in the
attribute definition
use="required" - attribute must appear with an
element
use="optional" - attribute is optional
use="prohibited" -attribute can be specified
Ex: <attribute name="traineeid" type="xsd:string"
use="required" />
Attribute can have default value
<attribute name="location" type="xsd:string"
default="Dhaka" />
Lets work with an example
Book.xsd
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:element name="book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="title"
type="xsd:string" />
<xsd:element name="price"
type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="isbn" type="xsd:string"
use="required" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
Here we defined a an element book, it is the top-level
element
book contains other elements, so it is a complexType
book contains other element in a sequence
name - it is of type string
price - it is of type string
Lets a create sample xml file
Book.xml
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xsi:noNamespaceSchemaLocation="book.xsd">
<name>XML</name>
<price>780.00</price>
</book>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
and xsi:noNamespaceSchemaLocation="book.xsd"
These two things are to associate an XSD with xml file
It imports a namespace
"http://www.w3.org/2001/XMLSchema-instance" given a
prefix xsi. xsi:noNamespaceSchemaLocation="xsd file" is
used to point the location and name of the XSD file
XML_r15 Page 36
<name>ASP .NET</name>
<publisher>MPress</publisher>
<price>880.00</price>
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="6"/>
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xsd:simpleType name="nameType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="publisher"
type="xsd:string" maxOccurs="1"
minOccurs="1" />
<xsd:element name="price"
type="xsd:decimal" maxOccurs="1"
minOccurs="1" />
</xsd:sequence>
<xsd:attribute name="isbn" type="xsd:string"
use="required"/>
</xsd:complexType>
</xsd:schema>
Here we created a Global Type BookType and then we
defined our element book is type is declared as BookType
Lets validate an XML file
Book_1.xml
<?xml version='1.0' ?>
<book isbn="8890-786-90-92"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xsi:noNamespaceSchemaLocation="book_
1.xsd" >
Ex:
</book>
SimpleType
We use simpleType to create own Data Type
simpleType allows you to create you data type based on the
basic supported data types.
Suppose you want to define an element that will have
content 20 characters long text. You can do this using a
simpleType
You can apply constraints to your data type setting
restrictions
Syntax:
<xsd:simpleType name="a-name">
<xsd:resstriction base="base-type">
<!-- resctriction -->
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="isbnType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{6}"/>
</xsd:restriction>
</xsd:simpleType>
This example defines an element called "isbnType". The
value will have 6 digits
<xsd:simpleType name="priceType">
<xsd:restriction base="xsd:decimal">
<xsd:minExclusive value="0"/>
</xsd:restriction>
</xsd:simpleType>
This example defines an element called "priceType".
The value will must be over zero.
Description
enumeration
length
maxExclusive
maxInclusive
maxLength
minExclusive
minInclusive
minLength
pattern
XML_r15 Page 37
XSD - Workout
Saturday, July 14, 2012
8:00 AM
Lets Create a XSD file
namedtypes.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:simpleType name="nameType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="isbnType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{6}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="priceType">
<xsd:restriction base="xsd:decimal">
<xsd:minExclusive value="0"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="books">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="book" maxOccurs="unbounded" minOccurs="1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="nameType" />
<xsd:element name="price" type="priceType"/>
</xsd:sequence>
<xsd:attribute name="isbn" type="isbnType" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Create namedtypes.xml
<?xml version="1.0" ?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="namedtypes.xsd">
<book isbn="898989">
<name>A Tale of Two citie</name>
<price>23.99</price>
</book>
<book isbn="898990">
<name>A Tale of Two cities</name>
<price>27.0</price>
</book>
</books>
Important Notes
XML_r15 Page 38
Homework:
Today's Topics:
Important Points:
Summary
After the lecture, use this space to summarize the main points of this Lecture Topic.
XML_r15 Page 39
More on Restrictions
Saturday, July 14, 2012
8:00 AM
Restrictions are used to define acceptable values for XML elements or
attributes.
Restrictions on Values
The following example defines an element called "age" with a
restriction. The value of age cannot be lower than 0 or greater than
120:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on a Set of Values
To limit the content of an XML element to a set of acceptable values,
we would use the enumeration constraint.
The example below defines an element called "car" with a restriction.
The only acceptable values are: Audi, Golf, BMW:
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Other Restrictions on a Series of Values
The example below defines an element called "letter" with a
restriction. The acceptable value is zero or more occurrences of
lowercase letters from a to z:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example also defines an element called "letter" with a
restriction. The acceptable value is one or more pairs of letters, each
pair consisting of a lower case letter followed by an upper case letter.
For example, "sToP" will be validated by this pattern, but not "Stop" or
"STOP" or "stop":
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example defines an element called "gender" with a restriction.
The only acceptable value is male OR female:
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example defines an element called "password" with a
restriction. There must be exactly eight characters in a row and those
characters must be lowercase or uppercase letters from a to z, or a
number from 0 to 9:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on Length
To limit the length of a value in an element, we would use the
length, maxLength, and minLength constraints.
This example defines an element called "password" with a
restriction. The value must be exactly eight characters:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This example defines another element called "password" with
a restriction. The value must be minimum five characters and
maximum eight characters:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
XSD Constraints
XSD allows allow you to define contains on elements/attributes
Also allows you to implement relations amongst elements or
attributes
Unique
XSD allows describing uniqueness constraints
The following declaration done under the declaration of our
book element indicates that the ISBN must be unique:
<xs:unique name="charName">
<xs:selector xpath="book"/>
<xs:field xpath="@ISBN"/>
</xs:unique>
<xs:selector xpath="book"/> - describes the node to which the
uniqueness apply (relative to context node). The node must be
an element node
<xs:field xpath="@ISBN"/This is the node whose value will be
checked for uniqueness. The node can be an element or an
attribute node.
Key
he key constraint acts same as unique constraint. But key
allows null but unique does not
To use the ISBN as a key, we can just replace the xs:unique by
xs:key:
<xs:key name="charName">
<xs:selector xpath="character"/>
<xs:field xpath="name"/>
</xs:key>
Keyref
The third construct, xs:keyref, allows us to define a reference
to a xs:key or a xs:unique.
It allows to create relationship like RDBMS
<character>
<name>Snoopy</name>
<friend-of>Peppermint Patty</friend-of>
<since>1950-10-04</since>
<qualification>
extroverted beagle
</qualification>
</character>
XML_r15 Page 40
XSD Constraints
Saturday, July 14, 2012
8:00 AM
To indicate that friend-of needs to refer to a character from
this same book, we will write, at the same level as we defined
our key constraint, the following:
<xs:keyref name="charNameRef" refer="charName">
<xs:selector xpath="character"/>
<xs:field xpath="friend-of"/>
</xs:keyref>
Some examples
The "xpath" attribute of "selector" and "field" elements is a
restricted XPath relative to the element in which it is declared
(here it is relative to the "root" element).
#1
Ex_0.xsd
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:element name="root" >
<xsd:complexType>
<xsd:sequence minOccurs="1">
<xsd:element name="a" minOccurs="1"
maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="id"
type="xsd:NCName"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="myId">
<xsd:selector xpath="./a"/>
<xsd:field xpath="@id"/>
</xsd:unique>
</xsd:element>
</xsd:schema>
Valid document
<root xsi:noNamespaceSchemaLocation="Ex_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<a id="x"/>
<a id="y"/>
<a id="z"/>
</root>
#2
Ex_1.xsd
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="1">
<xsd:element name="AAA">
<xsd:complexType>
<xsd:sequence minOccurs="1">
<xsd:element name="a"
minOccurs="1"
maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="id"
type="xsd:NCName" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="BBB">
<xsd:complexType>
<xsd:sequence minOccurs="1">
<xsd:element name="b"
minOccurs="1"
maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="idref"
type="xsd:NCName" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="myId">
<xsd:selector xpath="./AAA/a"/>
<xsd:field xpath="@id"/>
</xsd:key>
<xsd:keyref name="myIdref" refer="myId">
<xsd:selector xpath="./BBB/b"/>
<xsd:field xpath="@idref"/>
</xsd:keyref>
</xsd:element>
</xsd:schema>
<root
xsi:noNamespaceSchemaLocation="correct_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AAA>
<a id="x"/>
<a id="y"/>
</AAA>
<BBB>
<b idref="x"/>
<b idref="y"/>
<b idref="y"/>
</BBB>
</root>
XML_r15 Page 41
XSD Constraints
Relation.xml
<publications
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="relation.xsd">
<authors>
<author>
<auid>au001</auid>
<name>Bell</name>
</author>
<author>
<auid>au002</auid>
<name>Kim</name>
</author>
</authors>
<books>
<book auref="au001">
<name>SQL</name>
<price>290</price>
</book>
<book auref="au001">
<name>SQL</name>
<price>290</price>
</book>
</books>
</publications>
A problem
Look at Products table
<xsd:complexType name="authorsType">
<xsd:sequence minOccurs="1">
<xsd:element name="author" minOccurs="1"
maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="auid" type="xsd:string"
maxOccurs="1" minOccurs="1" />
<xsd:element name="name"
type="xsd:string" maxOccurs="1" minOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="booksType">
<xsd:sequence minOccurs="1">
<xsd:element name="book" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="price" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="auref" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
XML_r15 Page 42
char
productname
varchar
price
Decimal
Length
Other Constraints
4 value follows a
pattern, starts with
"P-" then two digits
like P-01, P-02 and it
is primary key
30
must be over zero
Homework:
Today's Topics:
What is XML Schema Document
Need for it
XML Schema Document Structure
ElementType
AttributeType
element
attribute
Workouts
Important Points:
Lecture Topic:
During the lecture, take notes here. Append extra page(s) if required
XML_r15 Page 43
XML Schema
Saturday, July 14, 2012
8:00 AM
XML Schema
It is a language to express the structure and
constraints of a particular XML Document.
It is a Microsoft terminology
It is Microsoft's first effort to improve weaknesses of
DTD [W3C Schema is now different and Microsoft
currently implementing W3C's XSD]
Schema Document Structure
XML Schema Document Structure
The following are the elements of a Schema document
Schema - Serves the root element for XML Schema
Document
datatype - describes the data types for elements and
attribute
ElementType - Describes a type of element
element - defines an element that appears within
another ElementType
group - organizes elements in a group in order
AttributeType - defines a type of attribute
attribute - defines an attribute that appears within
another AttributeType
[ElementType, AttributeType are to-level element in
XML Schema document. They appear directly under
the root Schema. Look all start with Capital Letter. All
the except these are local-level and they appear within
other to-level elements]
The Schema Element
It servers as the root element of an XML Schema
document.
It has two attributes
name - the name of the schema
namespace (xmlns) - the namespace for the
Schema. It must be set to "urn:schemasmicrosift-con:xml-data"
Example:
<Schema name="MySchema" xmlns="urn:schemasmicrosift-con:xml-data">
<!-- content goes here -->
</Schema>
In order to use XML Schema dat type like date,
time, int, float and so on you import another
namespace "urn:schemas-microsiftcon:datatypes"
<Schema name="MySchema" xmlns="urn:schemasmicrosift-con:xml-data" xmlns:dt="urn:schemasmicrosift-con:datatypes">
<!-- content goes here -->
</Schema>
The datatype element
dataype element is used to set data type for an
element or attribute.
It is set using dt:type attribute. Ex.: dt:type="float"
[See book appendix for full list of supported data
type ]
XML_r15 Page 44
Now come to
<ElementType name="book" content="eltOnly"
order="seq">
<element type="name" minOccurs="1"
maxOccurs="1" />
<element type="price" minOccurs="1"
maxOccurs="1" />
<attribute type="genre"/>
</ElementType>
It declares an ElementType named book which
can contain only elements and elements must be
in order they are declared
<element type="name" minOccurs="1"
maxOccurs="1" /> an element of type "name" as
define by the
<ElementType name="name" dt:type="string"/>
name lement can appear once and only once
<attribute type="genre"/> an attribute named
genre defined by
<AttributeType name="genre"
dt:type="enumeration" dt:values="Classics
Autobigraphy Others" default="Others"/>
Now create an Sample XML file to validate with
the Schema
Book.xml
<?xml version='1.0' ?>
<book genre="Classics" xmlns="xschema:BookSchema.xml">
<name>All Quiet on the Western
Front</name>
<price>700.99</price>
</book>
Look how we associate the XML file with the
Schema
<book genre="Classics" xmlns="xschema:BookSchema.xml">
xmlns="x-schema:BookSchema.xml" actually
associates the Schema document with XML file
It must be within the root element in the XML
file
XML_r15 Page 45
Important Points:
XML_r15 Page 46