XML Notes by Sekhar Sir JavabynataraJ
February 18, 2017 | Author: siddarthkanted | Category: N/A
Short Description
Download XML Notes by Sekhar Sir JavabynataraJ...
Description
XML,WebServices & AJAX Course Objective:
i
Developing an xml document that acts as textual databases and developing java applications that communicate with xml documc:nts, Use X M L to transport the data across different applications In language independent manner(XML)
3.
J B beans...etc) as services over the web. And writing an 2. Exposing java based business components (java B ~ ~ ~ //ESpring
.
application which consumes those services(Webservices)
8
I
!
Developing rich internet applications, which provides asynchronous/fast response without page refresh(AJAX)
3.
.
XML
%
/
Q.) What is XML?
i I
i
XNlL stands for Extensible Markup Language
I
I I I
I
I
I
1
-
"> > >
XML is a markup language much like HTML It is also a specification. [ From W3C {World Wide Web Consortium ) ] XML was designed t o describe data. XML is self describing.
I i -
;I \
8
I
Q.) Witat is markup? (Markup --
.
= Tagging)
Enclosing textual content with in textual codes(tags) is nothing but markup.
I
1
I
ii 1
I I I
Q.) What are the similarities between HTML and XML? ~3 Both are languages o f web. I
I
> Both are markup languages. > Both are originated form SGML. [ Standardized General Markup Language(comp1ex) Tags are basic building blocks o f both HTNlL and XlML documents. Q.) Is XML is Replacement for HTML?
No. T h c i r goals are different.
.Q.)What
are the differences between HTML and XML?
.........
.......
HTM L .._:_.,,~_
.
-.-
...
-.. -- -- ---
-.
-..---.-.
_- -
---
-.--.
--I
I Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 I An IS0 9001 : 2000 Certified Company . . I -----. -..... -- . . . - I . .-. --
.- .........
-~---....-..---,--p-~p--~--..-.
http://javabynataraj.blogspot.com
Page 1 1 of 99.
.....
- .................-.
-
.
.
I I
I 1 I
i
-
.
Mr.SekharRedd1.z
XML .
. . . . ... ..............
A
.....;..:A=
=.3
1.Pre defined tags
1. User defined tags
2. Limited no. of tags -
2. Tags are extensible
3. Tags are case insensitive
3. Tags are case sensitive
4. Tags are meant for displaying the
4.Tags are meant for describing the
data but not for describing the data.
data
5. HTML focuses on how data looks
5.) XML focuses on what data is
..................
........
--. ... -. . . . . -. .
................
?.;--.
Q.) What is the purpose of XML?
I
1
XML is used to exchange the information between applications in language (java, .net, PHP) independent, vendor independent and platform independent manner over web. 4 XML documents are used as textual databases.
I
4
I
:, XM L is Used to Create new Internet Languages(XHTML, WSDL, WAP, SNIIL etc) --
I
! I
4
XML documents are used as deployment descriptors
...
;! Q.) W h a t XML Does?
................
r-3 I
a
Nothing!!! Jusl XlVlL was created to structure, store and transport the data.
The following example is a email to SardarG, from Bantha, stored as XML: .......
,
....
... -.-:---c-.-.--->.-.-.e.-===-.r
Donlt f o r g e t me t h i s weekend!
The emall above is quite self descriptive. I t has sender and receiver information, it also has a subject and a message body.
I
But still, this XML document does not DO anything. I t is just information wrapped in tags. Someone must write a piecec of softwarc to send, receive or display it.
,I
Q.) W h o invented xml tags?
I
With XML You Invent Your Own Tags
I -
The tags in the example above (like and ) are not defined in any XML standard. These tags are "invented" by the author of the XML document.
I
I That is because the XML language has no predefined tags.
I
The tags used in HTML are predefined. HTML documents can only use tags defined in the HTML standard (like , chi>, etc.).
!
. A
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An ISO 9001 : 2000 Certified Company--. ..
http://javabynataraj.blogspot.com
.
...
I
I I
Page 2 2 of 99.
XML allows thc author t o define his/her own tags and his/her own document structure.
;
I
Q . ) W h a t is an X M L document?
I I
:) Any text file that is developed with .xml extension is an xrnl document.
Q . ) What is xrnl application? I
I
Any computer application that works with an xrnl document is nothing but an xrnl application. 3 Working with XML document means, perform CRUG operations o n the XNIL. 3
XML Syntax 1
I I
II
An example XML document:
cbody'>Don' t f o r g e t m e t h i s weekend! < / b o d y >
,- the first line in the document: The XML declaration should always be included. It defines the XML version of the document. In this case the docc~riieritcolifor~nsto the 1.0 specification of XML: i
i
The next line defines the first element of the document (the ro,ot element):
j
The next lines defines 4 child elements of the root (to, from, heading, and body):
,
Tove < f r o ~ n > J a n i < rom> /f F.eminder < b o d y > D o n l t f o r g e t me t h i s w e e k e n d ! < / b o d y >
'
'
I
1
I
The last line defines the end of the root element:
All X M L elements must have a closing tag In HTML some elements do not have to have a closing tag. The following code is legal in HTML: This i s a paragraph ............................
----
.. -..-. . ..... .-..-
-
-
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9001 : 2000 Certified Company --...
--. -
http://javabynataraj.blogspot.com
Pai6-3.. 3 of 99.
i
!
/
I I
;p:"Ti-!j.s
i:; another paragraph
In XML all elements must have a closing tag like this:
Thl-s 1s a paragraph (p>Ti-ils 1:; another paragraph
,
XML tags are case sensitive
;
XML tags are case sensitive The tag IS different from the tag .
:
Opening and closing tags must therefore be written with the same case.
I
1
.
i
All XML elements must be properly nested
I / I
'
i
i
I
In HTNlL some elements can be improperly nested within each other like this:
I
':l>Th~.s text 1s bold and italic
1
i j
In XML all elements must be properly nested within each other like ihis !
is text is bold and italic
I
-, . !I
I
.
'
I
All XML documents must have a root tag I
All XML documents must contain a single tag pair to define the root element. All other elements must be nested within the root element.: All elements can have sub (children) elements. Sub elements must be in pairs and correctly nested within their parent element:
I
I
,
I
i !
I ! ...........................
........
i
!
iI
2
Attribute values must always be quoted XML elements can have attributes in nameivalue pairs just likein HTML. In XML the attribute value must always be quoted. Study the two XML documents below. The first one is incorrect, the second is correct:
.........
- .......
...........
"""
...................
_
_
"."..
...,_*_ .
.- ,-
..
...-
-.
-
I
....
"
I
.i
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9 0 0 1 : 2000 Certified Company -
........-....--.-..
I
;
.
http://javabynataraj.blogspot.com
.
,
Page 4. ' 4 of 99.
.
.:~-!f-:i. + 3at.i=i.2/11/99>
.-. -. 7'.:l'.2:-2 ~:fr~m?Jani
ihea:ilng>Reminder
chodl"Don1t forget me this weekend! '::/not?;>
.-.::.;n-,~;+rs.ian="l.O"?> l . : n o t e date="12/11/99"> ,:.t03.'_Tii~:e
White-space is Preserved in XML tI-rML trunc:atcs multiple white-space characters to one s~nglewhlte-space: .....
.
.~
......
. .-.-p-p---.p-pp-p-
Tove . . . . . . . . . . .......... ..
.. .......
.........
-.-
.
I
i
With XML, the white-space in a document is not truncated.
I
I
/I
Q.) What is a well-formed I
>
I
>
.
XML document?
I f an XML document confirms the syntactical rules (above specified) o f XNlL specification is said t o be wellformed. An XWlL document is said t o be well-formed if it observes the following rules.
I
1. 2. 3. 4.
! i I
I
i . I I
I I
.
5. 6. 7. 8.
I t must begin with the XML declaration I t rrlust have one unique root element Start-tags must have matching end-tags Elements are case sensitive All elements must be closed All elements must be properly nested All attribute values must be quoted Entities must be used for special characters
!
Q.)What is an X M L Element? I
An XML clcmcnt is everything from (including) the ele'ment's start tag t o (including) the element's end tag.
II
An element can contain:
1. 2. 3. 4.
other elements text attributes o r a mix of all of the above ...
Q.) What are the
XML Naming Rules?
XML clcrncnts must follow these naming rules: 1. Names can contain letters, numbers, and other characters
2. Names cannot start with a number o r punctuation character 3. Names cannot start with the letters xml (or XML, o r Xml, etc) Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9001 : 2000 Certified Company ...........
p .---.p-.-.--.--
http://javabynataraj.blogspot.com
I I
pag&-6-.' 6 of 99.
I
4.
Names cannot contain spaces
Any name can be used, no words are reserved. Q;)What are t h e Best Naming Practices?
,
1. Make names descriptive. Names with an underscore separator are nice: , . 2. Narrles should be short and simple, like this: not like this: . 3 . Avoid "-" characters. I f you name something "first-name," some software may think you want t o subtract name from first. 4.. Avoid "." characters. I f you name something "first.name," some software may think that "name" is a property of the object "first." 5. Avoid ":" characters. Colons are reserved t o be used for something called namespaces.
XM L Attributes I I
> XML elements can have attributes, just like HTIVIL. > Attrrbutes provide additional information about an element.
I >
Attr~butescan occur in any order in an X M L element.
. ~ c , Attribute is unique in an X M L element. i.e. I t should not be repeated. example,
. irst:.n&me>Anna ( 2-
I
I
I n the flrst example gender is an attribute. I n the last, gender is an element. ~ o t examples h provide the same lnforrnat~on.
I
I
I
I
1
There are no rules about when t o use attributes or when t o use elements. Attributes are handy in HTML. I n XML m y advice IS t o avoid them. Use elements instead.
I
I!
--'/ I
I
--I
I
iI
I
I'
I
Avoid using attributes? (Isay yes!)
I
I i
I
These are some of the problems using attributes:
I
w w
i
attributes cannot contain multiple values (elements can) attributes are not expandable (for future changes) attributes cannot describe structures (like c h ~ l delements can) altributes are more difficult t o manipulate by program code attribute values are not easy t o test against a DTD/XSD
I
My Favorite Way
I
1
The following three XML documents contain exactly the same information: A datc attribute is used in the first example:
Tove Jani Reminder Donlt forget me this weekend! ......................... .... , . -. . =:
A date element is used in the second example:
\
I
-------
I
- --
--
Naresh i Technologies, Opp. Satyarn Theatre, Arneerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9001 : 2000 Certified Company --
- --
- --
http://javabynataraj.blogspot.com
I --
I
Page 8 8 of 99.
,
;..~ . . . . . . . . . . . . - .
...-....
~ e L * . . -
: --.note -
,I
?LO/01/2008
~
*:tc2.:To17e
cfrorn>Gani Reminder//heading, Don'lr forget me this weekend! Learning XML Erik T. Ray 2003 39.95
I
. . .
:II I ,
I
i
II
II
YI 11
'
-
Ii I 1
-- - --- --
-
I
i
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9 0 0 1 : 2000 Certified Company -.. - - .---.------
http://javabynataraj.blogspot.com
pagein- -
10 of 99.
Q.) How to view XML files? I
1
3
Q.) How to develop an XML document?
I
a I
I
a
In order to develop a useful xml document, we need to develop one more file. 1.e. DTD or XSD file. In DTD file or XSD file we specify the XML vocabulary.
Q.) What is a valid XML?
a
I
If an XML document is developed according t o rules specified in DTD / XSD, it is known as valid XML document.
I
1
[3y using Browsers, text editors, XML editors we can view XNlL files.
NOTE: Every valid XML is a well-formed, but vice versa .need not be true.
! [First well-formedness is checked then it checks for validness will be checked]
;
I
i i
Q.) H o w to check the well-formedness of XML document?
i I
I
a
Using XML parsers, XML Editors, Browsers ....etc.
Q.) H o w to validate XML document?
-
-
r,>
Using XlVlL parsers, XlVlL editors...etc.
Every XML application needs two things t o work with XML
1. Parser 2. API Q.) W h a t is an XML parser? What a r e its functions? 10 XML
I-
>
parser is an API that enables XML applications to work with XML document. An XML parser performs the following things. 1. read in^ the XFAL document. 2. Checking well-formedness . 3. Verify its validity. 4. Making XML data available t o XML application.
Naresh . ~ e c h n o l o ~ i e Opp. s , Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9001 : 2000 Certified Company
- - - - - - . .--- .
~aC-1 i-
--
http://javabynataraj.blogspot.com
11 of 99.
I
I I
1. XNII. application instantiating the parser and specifying the
I
I
2.
I
3.
I I
4.
1
5. 6.
XML file t o the parser. (i.e. Passing the name of the XML file name t o the Parser ) Parser reads the specified XML document and verifies its well-formedness. In the XML document parser gets the information about the D'TD or XSD. I'arser verifies the correctness of the DTD or XSD. Parser reads the metadata specified in the DTD or XSD file into the memory. Basing on the metadata read into memory; XML Parser verifies the validity of the XNlL document. Parser makes XML content (data) available t o the XML application either in the form of a tree structure or in the form of events.
{The way DOM makes available data t o the XML application is differ from JAXB } Valid XML document 1
3 XML parser can make data stored in XML document available t o
XML application if and only iff the XML documents are valid. I 3 There are two approaches for developing valid XML. o Using DTD o Using XSD Q.)What is DTD? 1
3
DTD stands for Document Type Definition.
I
>
A DTD is a text file with .dtd extension.
13 If XML file holds data, its corresponding DTD holds Meta data. I
> In a DTD legal building blocks of an XML documents are specified. 1.e. XML vocabulary is specified in a DTD.
Q.) What are the constituents of DTD file? (Contains)
I
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9001 : 2000 Certified Company
- ..-- - - .. --.-- -.
-
http://javabynataraj.blogspot.com
I I
Page 1 2 12 of 99.
-
- -
-
--
-- -
-
-
- - ---
-- --
- ---
.--
--
From a 111 0 p o ~ n of t vrew, all XML documents are made up by the follow~ngb u ~ l d ~ nblocks: g I
Elements ALLrrbutes Ent~t~es PCDATA CDATA
I
I Elements i
1 I
I
Elemcnts are the main building blocks of both XlYL and HTML documents.
I
Examplcs of HTML elements are "body" and "table". Examples of XML elements could be "note" and "message". Elcmerits can contain text, other elements, or be empty. Examples of empty HTML elements are "hr", "br" and "img".
II
Examples:
I
i
I
)
provide extra information about elements.
-4ttributcs
4ttributcs are always placed inside the opening tag of an element. Attributes always come in name/value pairs. The following "irng" element has additional information about a source file:
The name of the element is "irng". The name of the attribute is "src". The value of the attribute is "computer.gif". Since the element itself is empty i t is closed by a " /".
Entities Some characters have a special meaning i n XML, like the less than sign ( c )that defines the start of an XVIL tag. Most of you know the HTML entity: " ". This "no'-breaking-space" entity is used in HTML t o insert an extra space in a document. Entities are expanded when a document is parsed by an XML parser. 'The following entities are predefined in XML:
Entity References
Character
NJresh i Technologies, Opp. sat yam Theatre, Ameerpet, Hydera bad, Ph: 040-23746666,23734842 A n IS0 9001 : 2000 Certified Company
. .. . .~. .. . . ..
. -
http://javabynataraj.blogspot.com
Page i313 of 99.
. . . .
...........
....-
.-..
XML
....................
,
Mr.Sel".
Nested CDATA sections are not allowed.
The " ] ] > " that marks the end of the CDATA section cannot contain spaces o r line breaks.
I
--
DTD
I 1
- Elements
1TDeclaring an Element I
l~ In the DTU, XML elements are declared with an element declaration. An element declaration has the following syntax:
I\ I
< ! ELEMENT
II
element-name (element-content) >
I'
1
Empty elements
1% Enipty elements are declared with the keyword EMPTY inside the parentheses:
< ! ELEMENT element -name (EMPTY) > example : :! ELEYIENT b r
(EMPTY) >
Elements with data Elements with data are declared with the data type inside parentheses:
.
-
-
.. .
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9001 : 2000 Certified Company- -- - -- .-.
-
http://javabynataraj.blogspot.com
--
Page IS 15 of 99.
. ..
~~
~
....-.
~
...
~
-L-p----p-----.-p-p ~
XML ;
-:i C
I
ELEt.!!.
.: ! ELE'4ENT slement-name (#PCDATA)> .
,
01-
: j
1
c: ! ELEMENT element-name (ANY)> e:.;ample: *::! ZLEI.1kT,?1T note (#PCDATA)>
.
I i I
I
.--~-
r
! !
~
~ r . ~ e l < h a rclij-: ~ed
.
.
# c ~ ~ ~ A ' r n e the a n element s contains character data that is not supposed to be parsed by a parser #PCDATH mearis that the element contains data that IS going to be parsed by a parser. The keyword ANY declares an element with any content. If a #PCDATA sectlon contains elements, these elements must also be declared.
Elerner~tswith children (sequences)
I
.
I
/
I 1
Elemerits w ~ l hone or more children are defined with the name of the children elements inside the parentheses:
I
I
or i ELEPIENT example : ! EL.EIiE;l? t r 3 (#CDATA)> < ! ELEMENT from (BCDATA)> < ! ELEMENT heading (#CDATA)> K
:
-
Q.) What is cardinality operator in DTD? I
3
1
-
I
I
An XML element can occur zero or more times in the XML document.
Cardinality operator specifies the no. of times an XML element can occur in XNlL document. In a DTD we have three cardinality operators. o ? (0 or 1) o -t (1or n) o *(Oorn)
Note: If a cardinality operator is not associated with the XML element, that element should occur exactly once in the XML document.
Declaring only one occurrence of the same element ; ." .
-- -
-
,.,,,.".",%,--"----~-,.-.------.-,..--"--.-.."..-"-,."*.-"."..,--..A<
~
~
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An I S 0 9001 : 2000 Certified Company
--
Page 16
http://javabynataraj.blogspot.com
16 of 99.
1 1
. . ... -
--
.-
-.
- --
-- -- --
- ..--
-----
-
---
-
--
--. -.- . - ----- -. --
XML
- ---
-
-
.-
-..----
-. -...
~r.~ekhar~ed
2xa1nple -1 !
ELEMELJT note (message)>
The example declaration above declares that the child element message can only occur one time inside the note element.
Declaring minimum one occurrence of the same element .
'! ELEMENT
element-name (child-narnsa)>
zxample ( ! EL,EMEN'T I
note (message+)>
The + sign iri the example above declares that the ch~ldelement message must occur one or more times inside the note element
I
~
I
Declaring zero or more occurrences of the same element < 1 ELEMENT element -name (child-name*) > er.an;pl e < 1 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 the same element :! ELEMENT element--name (child-name?)>
example < ! ZLE,PENT cote (message?)> The ? sign In the example above declares that the child element message can occur zero or one times inside the note element.
Declaring mixed content
..
The example above declares that the element note must contain at least one t o child element, exactly one from child element, exactly ~ne header, zero or more message, and some other parsed character dataas well.
DTD - Attributes -
geclaring Attributes In the DTD, XML element attributes are declared with an ATTLIST declaration. An attribute declaration has the following syntax:
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 A n IS0 9 0 0 1 : 2000 Certified Company
http://javabynataraj.blogspot.com
17 of 99.
-
- --
-- - - ---- ---.
--- -
XML
----
~r.~ekhar~edd
As you can see from the syntax above, the ATTLIST declaration defines the element which can have the attribute, the name of the attribute, the type of the attribute, and the default attribute value
-1
-
1
The attribute-type can have the following values: Value - P -
The !evallevail..) The ID The i C K Z r' The T DREFS The t\!P4TOKEN The NLvIT~KE[\IS The L
,
Explanation
l7
-L!-.
ENTITY ENTITIES MOTATlON xml :
value value value value value value value
1s character data must be an enumerated value is an unique ld is the id of another element is a llst of other ids is a valid XML name is a list of valid XML names
The value is an entity The value is a list of enti e value 1s a name of a no
The attribute-default-value can have the following values: Value
Explanation
#IMPLIED #FIXED value
optional attribute The attribute value is fixed
Attribute declaration example
DT'D example: I < ! ATTLIST square width CDATA "O">! XML example: In the above example the element square is defined to be an empty element with the attributes width of type CDATA. The width ,attribute has a default value of 0.
Default attribute value Syntax :
I
1
DTD example:
,I I XML example:
/
Invalid XML:
I
I
I
Use a flxed attribute value 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.
--
!
I
--
-
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 A n I S 0 9 0 0 1 : 2000 Certified Company
-- --
http://javabynataraj.blogspot.com
Page
1 I
IT-'
19 of 99.
Enumerated attribute values
, - ,
I
Syntax :
-
_i I
!
I
example: or . I
DTD Example:
---
L-
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An Iso 9001 : 2000 Certified Company
..-: - --
http://javabynataraj.blogspot.com
Page 2(. 20 of 99.
i
J
. .
~
~
~
-
--...A-
-
YML
..
-
.-
Mr.SekharReddy ; I
-
'
ATD Example: ~ ! EPITI'T'L' copyright SYSTEM "http: //www.xmllol.com/entities/entities.dtdr'>' YML exsmple: .authcr>&wr~ter;icopyright;
Q.) How t o associate a DTD with xml? I I
> We need to specify DOCTYPE declaration after XML declaration,
I
>
in order to associate a DTD with the X M L document. A DTD can be declared
o o
Inside an XML document External reference. I
Internal DTD Declaration
I
.f the DTD is declared ins~dethe XML file, it should be wrapped in a DOCTYPE definition with the following syntax: - ---. --
1 < ! DOCTYPC root-element I
P
P
[~lement-declarationsp
Example XML document with an internal DTD:
- -..~ --.--- -- --.----
!
. .-. . , . . ..
-
-
-- .... . .. ..
< ! ELEMENT heading (#PCDATA)> < ! ELEMENT body ( # PCDATA)> Tove Jani Reminder Don1t forget me thls weekend - -.--
1 [I I
The DTD above is interpreted like this:
.
! I
I
-
0
!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 type "#PCDATA" !ELEMENT from defines the from element to be of ~ ~ ~ ~ ' " # P c D A T A ' ' !ELEMENT heading defines the heading element to be of type "#PCDATAn !ELEMENT body defines the body element to be of type "#PCDATAVt
---.-
I I --- -
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,-23734842 An IS0 9001 : 2000 Certified Company -
http://javabynataraj.blogspot.com
~a~e'2321 of 99.
External DTD Declaration 1
I f the DTD is declared in an external f~le,i t should be wrapped in a DOCTYPE definition with the following syntax:
< l P Q C T ' i P I I root-element SYSTEM "filename and location"> -
-
1
. u p
--.-----A-
-
This is thc same XML document as above, but with an external DTD
.
...
.
.
......
....
< ! DOCT-YPE note SYSTEM "note.dtdl'>
.
Reninder Don't forget me this weekend! . . . . . . . . . . . . . . . . . . . And this is the file "note.dtdUwhich contains the DTD:
.
-- --
..
~
... . ... -......
< ! E L E M E ~ T note T < ! ELEMENT to ( # PCDATA) > < ! ELEMENT from (#PCDATA) > < ! ELEMENT heading (#PCDATA) > < ! ELEMENT body ( # PCDATA) > ........... ..........
II I
Note: Internal DTD's are seldom used. Reusability o f DTD's across XML documents is n o t possible w i t h internal DTD.
Q.) Develop a DTD file for the following XML file? ,-
I
!
Message.xml
I i
ram sam hi, how are you messape.dtd
Q;) Develop an external DTD for the following XML document?.
II
- -
.I
1
.- ,
_
.
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666, 23734842 An IS0 9001 : 2000 Certified Company _._ _ __-
http://javabynataraj.blogspot.com
/
e T22 pofa g99.
'-
'
1 items.dtd 1 I- < ! A T K I S T item code ID #REQUIRED >
boo kstore.dtd
I . 1 -- .-.-I
I
.
-.-----
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9001 : 2000 Certified Company
http://javabynataraj.blogspot.com
Page 23 23 of 99.
bookstore.xmI < booltstore > HydBookStore XML lVlikels Guide to DTD's and XML Schemas Mike Jervis 68.0 XSD in 24 hours
, 1
I
I I
~1
*
David
I
i
/
I
i
!
,
Q.) What is a prolog in an XML file or in an XML document?
I
I
> An XML document is divided into two parts.
I
o
I i
o
1 1 1
Prolog Body
.
>
Root element is nothing but the body of the XML document. Prolog is that part o f the XML document which comes before the root element. 1 3 Generally prolog contains XML declaration and DOCTYPE declaration. I
-
I
i
Q.) What do you know about SYSTEM keyword in DOCTYPE declaration? I
I
3 SYSTEM keyword indicates that the specified DTD is a private one. Almost always we use this (SYSTEM) keyword
only in DOCTYPE declaration. 3 Another keyword i.e. an alternative for SYSTEM is PUBLIC.
Example: Smit?i .
- ..
--
---
XSD definition:
: x s : a t t r i b u t e name="langl' t y p e = " x s : s t r i n g u / > n a m r = " l a s t n a m e " t y p o = " x s : s t r i n g " />
( ..-~ .. ~
Restrictions on a Set of Values
hi
To l ~ m i the t content o f an XML element t o a set of acceptable values, we would use the enumeration constra~nt.
I
The example below defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW: . .
......
-. . . . .
............
I .....
I
The example above could also have been written like this:
1
< x s :pattern value=" [a-z]" / > /
1
Exarnple#Z Acceptable value is THREE of the UPPERCASI letters from a t o z:
i
l
.
:s: s?ernent>
E x a m p l e # 6 Acceptable value is zero or more occurrences of lowercase letters from a to z:
i
i :s:el s~nentname="letter"> .-. ...,. :;:restriction base="xs: stringu> .pattern . value=" ( [a-z]) * " / > , I'
x..
P
I
Exarnple#7 Acceptable value is one or more pairs of letters, each pair consisting of a lower case letter followed by an uppcr case letter. For example, "STOP"will be validated by this pattern, but not "Stop" or "STOP" or "stop":
I
I
< x s : element narne="letterV'> . , . : i : x s :restriction> < / x s : simpleType> . E x a m p l e # 8 Acceptable value is male OR female:
/
E x a r n p l e # 9 There must be exactly eight characters in a row and those characters must be lowercase or uppercase letters from a t o z, or a number from 0 t o 9:
< x s : sc?cqucnce> < / K : : : socguence> ( -,,,., . - .. : :;(:q~~(:nce> .(...,., ,. . . (: I orncnt name="nameU type=="xs: s t r i n g u / > ':;
< x s : r:omp I cxrl'ype name="personTypef'> < x S : sc:(qlle.nce> < x s : el-ement name="namel' t y p e = " x s : s t r i n g 1 ' / > < x s : (?l cmcnt name="age" t y p e = " x s : i n t e g e r 1 ' / > < / x s :cornp.l.ex'Type>
1 1
< i1
NOTE: Advantage of this approach is several elements can refer the same complex type. Example
I
I
I
I L-.
. .
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An I S 0 9001 : 2000 Certified Company
-. ..
-
http://javabynataraj.blogspot.com
' .-
ge338 ~ofa99.
---, her-e a r e Cour kinds of complex elements: 1 . Elements with attributes 2. Elements with sub elements 3 . Elements with attributes and t e x t
Elements with sub elements and text
4.
Note: Each of these elements may contain attributes as well!
1.Elements with attributes
Ii !
,An errlpty complex element cannot have contents, only attributes. .
1.1
11
.cVc. ...,.. (:I
(:rn(:r1\.
r~nmc-"product">
< x >i : cornplexType> < x s : CJ ( . ( . r . i b u t e name--="prodid1'Lype=lTxs:p o s i t i v e I n t e g e r " / > < 1xi; : complexType> -:/>::I
:
I
It is possible to declare the "product" element like ,below -1
I'
I l(
(x:i : (:ornp.LoxContent>
< x s : a t t r i b u t e name-"prodid" type="xs:positiveInteg~r~~/> < / x s : c:omplexContenl:> < / X S : complexType>
I
I I n t h e example above, The complexcontent element signals that we intend to restrict or extend t h e
I
content model o f a complex type, and t h e restriction of integer declares one attribute but does not introduce any element content.
1
Example
i i
2.Elements with sub elements You can define the "person" element i n a schema, like this:
I T;
.-
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An I S 0 9001 :.2000 Certified Company . -_ . _ .. ... ...
http://javabynataraj.blogspot.com
gm--' 39 ~ofa99.
Example
1
1
I I
, I
' I 3 . E l e m e n t s with a t t r i b u t e s a n d text
,
I I
;
1)
I
T h ~ stype contains only simple content (text and attributes), therefore we add a simplecontent element around the conlent. When using simple content, you must define an extension OR a restriction withln Lhe simplecontent element
I
NOTE: Use Lhe extension/restriction element to expand or to limit the base simple type for the element.
I
I.
1 rnctr)L:> OII
l.cns:i.on> I < / x:; c:orn~i )rnplcContent> .cx1l?ype> I :
I
Order ~nd~cator-s are used to define the order of the elements.
'/
i
All Indicator
t
The indicator specifies t h a t the child elements can appear in any order, and that each child element
-
m u s t occur only once: -..--
...-
.
I orn(!nl: name="person"> ::; : camp I.cxType> < : x : ; : t 3I I > , , ; :c: I c?rnc?nL name-"chj.ld-name" < / x : ; : sc:clucnce>
< /x:; 1 I . I i!rrlc!n I:> . . - - - --
-
The example above indicates that the "child-name" element can occur a minimum o f one time (the default value for minoccurs is 1) and a maximum of ten times in the "person" element.
II
minOccurs Indicator
I
The indicator specifies the minimum number of times an element can occur: .......
< x : ; : c:olrlp I c!xType> < / x : ; :comp l.exType> . < / x :;: (: I :c rric? ri I:>.... '
, I
.
..-- ...
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9001 : 2000 Certified Company
http://javabynataraj.blogspot.com
43 of 99.
The c:xample above indicates that the "child-name" element can occur a minimum of zero times and a maximum
of ten t ~ m e sin the "person" element.
x
-
NOTE: To allow an element to appear an unlimited number of times, use the m a x O c c u r s = " u n b o u n d e d l ' statement.
i
-1.
!
NO'I'i~::rninOccurs and m a x O c c u r s can also be applied to model groups (e.g, xs:sequence) to control the 11uniber of times a model group can be repeated.
Group Indicators I
4
Group ~rid~cators are used to define related sets of elements.
1
Elernent G r o u p s ElcrncriL groups are defined with the g r o u p declaration, like this:
You must d e f ~ n ean all, choice, or sequence element inside the group declaration. The following example d e r ~ r ~ ea sgroup named "persongroup", that defines a group o f elements that must occur in an exact sequence: .....
--
> c,u:; : c; l . o r n e ~ ~n.arne="birthday" t type="xs :dateu/> < / x s : c~ r.o up>.......... -After you have defined a group, you can reference it in another definition, like this:
i x s : CJ r o u p re E = " p e r s o n g r o u p " / >
.
c x s : c l c m e n t name="country"
type="xs:string"/>
I
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An I S 0 9001 : 2000 Certified Com~anv .
.
http://javabynataraj.blogspot.com
I Page 44' 44 of 99.
A t t r i b u t e Groups
I
I
!
I
. ~ t l r ~ b ugroups lc are defined with the attributeGroup decla'ration, like this:
The rollowing example defines an attribute group named "personattrgroup":
::;:;:
I < / x : j : P 1 c:mc:rll;> .....
-................................. m I m ? a . E v
1
-.
G r o u p name="personattrgroup">
-. -
-.-..-. -----.- --. -
--
.-
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842
--
-. ...........
-.....
-... --
An IS0 9001 : 2000 Certified Company -
http://javabynataraj.blogspot.com
-._-I
45 Page of 99.4.5
l. name-"children"> < s : ; : c:oinp I cxll'ype> < x : i : :;(:ql~(:nce> < x : ; : (1 1 c!rr~c?nl:name-"chj.lclname"
< / x : i : (: I
c!rrlor1 L>
farnil\;.x@ !
/
I
T h s X M L rile uses components from two different schemas; "perosn.xsd" and "children.xsd":
NOTE: The and elements are used to make EXTENSIBLE documents!
Element
i '
The element enables us to extend the XML document with attributes not specified by the schema. person.xsd
--- -- ---
-- -
-
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 -- -- An I S 0 9001 :a2000 Certified Company
http://javabynataraj.blogspot.com
Page 44 46 of 99.
1
1
(
rhis XMI. file uses components from two different schemas; "perosn.xsd" and "children.xsd":
'
i
I
Overview .............................................................................................................................................................................................
1
Or-lc of the nice features of XML Schema is that comments about the schema itself can be made within builtin XMI.. elements. This makes it possible to run a transformation against a schema to builds documentation i / in I-ITPJiLor some other human-readable format. -
I
-
-- - -
-.
-.
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9001 : 2000 Certified Company ---
http://javabynataraj.blogspot.com
47 47Page of 99.
.-
..- -
-.
-... .~ -.
Ar~:.natatinga Schema .......................................................................................................................................................................................... 1
;
' I
,
-. ,y.... . . . . .
!
,~\;,
ii: . :-;. ;~-:r:,:\ta t i s n element is used to document a schema. It can take two clciliei'lt~::..s: d o c ~ l m @ n t a t i r ,andxs n : a p p I n f o , which are used to provide human-readable and i.?iai.:lii5~:-l-?adablenotes, respectively. -. i i.:cr -: : :-:::i; n element can go at the beginning of most schema constructions, . . *. ..... I:.! ~ i iI i. ; .::-: : :.:::I~en:a,xs : c?1.e.~1en t-, xs : a - t - t r i b u . t exs , : s.mx11 type, xs : coniplexType,xs : gro~iip, . . . . . . ; 1. -1. .? ~2i.i.; . --,, ,..: ..Group. a,I:.< ...:.. r... ........ ! - i : :..:!oc-;i:r:.:cntnt.ior! and x s : a r ~ p I n f o elements can contain any content, including undeclared t:i:~o 1 %O A < [ : narno>AI:rican Coffee Table < i : w i (.]I h > t l O < / f : width> < I : i l . Z O < / f : length> < / r : I ~ : I L ) I (:>
I
I n t h e example above, the xmlns attribute in t h e t a g give the h: and f: prefixes a qualified
I namespace. -
1 I'
W h e n a namespace is defined for an element, all child elements with the same prefix are associated with thc samc namespace.
Narncspaces can be declared in the elements where they are used or i n the XML root element:
I i
I
.
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An I S 0 9001 : 2000 Certified Company - ---
http://javabynataraj.blogspot.com
49 ~ajjFI-9-of 99.
< l : l . c . i l > 1 ,::> < I : r - l ~ i r n ( : > / \ l cican Coffee Table < i i (Jl,h>f3O I 2 0 < / f : length> . < / I : t. ::%I
1
I
I
!
1I
Note: Thc namespace URI is n o t used b y the parser to look up information.
/
The put-pose is to give the namespace a unique name. However, often companies use the namesp'ace as a
..I
1 i
1
I
i
poini.cr l o a web page containing namespace information. 1-
DeT~nir,ga derault namespace for an element saves us from using prefixes in all the child elements. I t has
i i
1
the foilowrr~ysyntax: xrn I
"
t~i;mo~pilceURI~~
Thls XML carries HTML table information: cl
I
11:;
1
I
x s I rls "http://www.w3 .org/TR/htmll/">
This XML carries information about a piece of furniture: . . .
<
1
i
I
1
I ( xm I ns--''http: //www.sekharit.com/furnitureW> . /\li.rj can Coffee Table < w i cll.h>ilO :, i (1r1(31..1.1>1 %0
-,z.-. ---.................... -
I.--. . . . . . . . . . . . . .
.-
Naresh i Technologies, Opp. satyarn Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An I S 0 9001 : 2000 Certified Company - -.----
http://javabynataraj.blogspot.com
-
50 Page of 99.5: -
I
I
-1 j D e v e l o p a XSD file for the following xml file?
-.,ol..2. xrr1l
:-,.;rsion="l. Ow:'>
I,.:!,:]
:
.
..
..
tu>Tove !?l-orn>Jani ... heading>Reminder .h,-dy>Donlt forget me this weekend! -.
--
.-
-
... .-.. -
---
--.
:I
r a xmlns:xs="http: //widw.w3 .org/200l/XMLSchema" I.il-imespace="http://www. sekharit . com" :.i-l! ; . : : ; = . " n t t p ://www.sekharit.co~n" i.:!?:?n l : F ' o r m D e f a u l t = " q u a l i f i e d " > :
,;,
L-I:
i:
c -,... . c: : elernent
name="noteW>
.-':.::< : co~nplexType> .is?; : sequence>
c:?:s:element name="tof'type="xs:stringl'/> element name="fromf' type="xs: stringw/> -.-ia.element ,...= . name="headingN type="xs:stringf'/> ..:/ss : .?f?quence>
-.: r:s : coinplexType> r
.:j
r
.
I
_
:.:s : eI.e~rierit>
I hc clement is the root element of every XML Schema.
Thc .=schcrna> element may contain some attributes. A schema declaration often looks something like this:
!
.' 1 -
I
. ..~
-.-
--
< I I.c!rn>
I1 -
11 1
dcn~onstratehow to write an XML Schema. You will also learn that a schema can be written in different ways.
l~tliswill
(I. i I. 1 o>ll:rnp.i.re B u r l e s q u e < / t i t l e > < n o l ~ a > S p c c i . a lE d i t i o n < / n o t e >
l < / q u a n t i i ; y > < p r i.c:c?>l 0.9 0 < / p r i c e > < / i l.orn> < i l.orn> lli-de y o u r h e a r t < / t i t l e > l
!EF=-........ x= ' .-............-...-.-
LA..
.
-.-
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An I S 0 9001 : 2000 Certified Company . .. -. -
http://javabynataraj.blogspot.com
53 Page of 99.53
1-llc X M L document above consists of a root element, "shiporder", that contains a required attribute called "oi~li'iid". The "shiporder" element contains three different child elements: "orderperson", "shipto" a n d "11cr11"I he " ~ t e m element " appears twice, and it contains a "title", an optional "note" element, a " q ~ ~ a ~ ~and l ~ l ay "price" ", element.
1
,
1
,-1 I
Thc 111icabove: xmIns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML parser t h a t this d o c u ~ l i e nshould l be validated against a schema. The line: xs,:rioN~mespaceSchemaLocation="shiporder.xsd"specifies WHERE the schema resides (here i t is i n the s;lii:c_. loldcr as "shiporder.xml").
--i
I
.! I
Ci-sate ~n XML Schema
I I
Now w e want to create a schema for the XML document above.
Wc star1 by opening a new file that we will call "shiporder.xsdfl. To create the schema we could simply follow l h c structure in the XML document and define each element as we find it. We will start with t h e staridald XML declaration followed by the xs:schema element that defines a schema:
I
I I
i
1 ( -.:.. :5 : : ~ C ! C J I I ( ?nu-:> -'.;:; : (? I c?rnc:n t riame-="namentype-"xs : stringM/> < s : ; ::? I cmcnt name="address" type="xs:stringu/> . . . . ... ,. \:,. . . . .. .,k.(q,lc~:nce> . , x:; : i:i)rrip I ox'I1ype> ..;.::j
I
I I ,I
; i;ijlii()
c.,,
::; : ( x : ; : :;o(qucrlc:c> < / x : ; : ( 1 I (irriorl1 .>
Divide the Schema I
I I Thc
p r c v ~ o u sdesign method is very simple, but can be difficult to read and maintain when documents are
I
COII~~~CX.
1
Ttit.ricxt desrgn method is based on defining all 'elements and attributes first, and then referring t o t h e m usliiy ihc r e l attribute.
I
I
-i
I
I
1 Hcrc 1s the new design of the schema file ("shiporder.xsd"):
xmlns:xs="http: //www ,w3.org/20OljXMLSchema">
:r
x
1 (:rr~c:ri 1.. r~ame="orderperson" type="xs:stringw/>
:oi(:~n(:ri.L. rlilme="nameV1type="xs:stringn/> . o~i-~criI; name==ltshipto"> .
;:/-:i : complcx'i'ype> .
. : j .
k i n g Named Types
I
I '(
The ILli~t-ddesign method defines classes or types, that enables us t o reuse element definitions. This is doric by namlng the simpleTypes and complexTypes elements, and then point to them through the type a t t r i buLe of the element.
I
I-lci-r: i.;,I-hc lhird design of the schema file ("shiporder.xsdU):
i1I II
i/
. -.
--
v c ! r s i or)-'!I.Oftencoding="ISO-8859-1" ?>
< x :j : :; i rrip I
I L . -
..-...........
cl'l'ype name="dectype "> ........-
.
.-
...........
.
IYaresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 .........-
..
-
An I S 0 9001 : 2000 Certified Company
http://javabynataraj.blogspot.com
57Page of 99.ST--
1r11: I o'l'\jpc! name="orderidtype"> i r i o n base="xs : stri.ngM> ..::.:.:i : ; ) ; I ?(.[I rrl value=" [O-91 {6}" / > ..',,',:.::: ?!.::jl*r,; c:Liorl> c
: :;(!
,:.:X:;
~-: x , . : (1 I (;rn.c:rl L name="orderperson" type="stringtype"/> . I
~ a . ,p"jS A M t i t r r n a l V a l i d a t a r . j a v a JRE 5ystct-n L i b r a r y f5i.!1-1 f l 'i., 1 ,6.f; I-: 4 \
, m e
.-
A*.-
Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyderabad, Ph: 040-23746666,23734842 An IS0 9001 : 2000 Certified Company
http://javabynataraj.blogspot.com
Page 18 --
77 of 99.
r-.~.-.
-~
~.
~-
~
~
-
.~
..
~
----
-
~
-
-.-.p--.--p-
XML
Bv Mr. SekharReddv. I
' II
message.xm1 1. 2.
View more...
Comments