Sie sind auf Seite 1von 9

Manual Definicin de Tipo de Documento

Introduction to DTD
default.asp default.asp dtd_building.asp dtd_building.asp
The purpose of a Document Type Definition is to define the legal building blocks of an XML document. It
defines the document structure with a list of legal elements.
DTD can be declared inline in your XML document! or as an e"ternal reference.
Internal DOCTYPE declaration
If the DTD is included in your XML source file! it should be wrapped in a D#$T%&' definition with the
following synta"(
<!DOCTYPE root-element [element-declarations]>
'"ample XML document with a DTD( )#pen it in I'*! and select +iew source,(
<?xml version="!""?>
<!DOCTYPE note [
<!E#E$E%T note &to'(rom')eadin*'+od,->
<!E#E$E%T to &.PCD/T/->
<!E#E$E%T (rom &.PCD/T/->
<!E#E$E%T )eadin* &.PCD/T/->
<!E#E$E%T +od, &.PCD/T/->
]>
<note>
<to>Tove<0to>
<(rom>1ani<0(rom>
<)eadin*>2eminder<0)eadin*>
<+od,>Don3t (or*et me t)is 4ee5end<0+od,>
<0note>
The DTD abo+e is interpreted like this(
!DOCTYPE note )in line -, defines that this is a document of the type note.
!ELEMENT note )in line ., defines the note element as ha+ing four elements( /to!from!heading!body/.
!ELEMENT to )in line 0, defines the to element to be of the type /1&$DT/.
!ELEMENT from )in line *, defines the from element to be of the type /1&$DT/
and so on.....
External DOCTYPE declaration
If the DTD is e"ternal to your XML source file! it should be wrapped in a D#$T%&' definition with the
following synta"(
<!DOCTYPE root-element 6Y6TE$ "(ilename">
This is the same XML document as abo+e! but with an e"ternal DTD( )#pen it in I'*! and select +iew source,
<?xml version="!""?>
<!DOCTYPE note 6Y6TE$ "note!dtd">
<note>
<to>Tove<0to>
<(rom>1ani<0(rom>
<)eadin*>2eminder<0)eadin*>
<+od,>Don3t (or*et me t)is 4ee5end!<0+od,>
<0note>
nd this is a copy of the file /note.dtd/ containing the DTD(
<!E#E$E%T note &to'(rom')eadin*'+od,->
<!E#E$E%T to &.PCD/T/->
<!E#E$E%T (rom &.PCD/T/->
<!E#E$E%T )eadin* &.PCD/T/->
<!E#E$E%T +od, &.PCD/T/->
Why ue a DTD!
2ith DTD! your XML files can carry a description of its own format with it.
2ith a DTD! independent groups of people can agree to use a common DTD for interchanging data.
%our application can use a standard DTD to +erify that the data you recei+e from the outside world is +alid.
%ou can also use a DTD to +erify your own data.
lot of forums are emerging to define standard DTDs for almost e+erything in the areas of data e"change.
Take a look at http(33www.schema.net.
DTD " #ML $uildin% $loc&
dtd_intro.asp dtd_intro.asp dtd_elements.asp dtd_elements.asp
The main building blocks of both XML and 4TML documents are tagged elements like 5body6....53body6.
The $uildin% $loc& of #ML document
7een from a DTD point of +iew! all XML documents )and 4TML documents, are made up by the following
simple building blocks(
'lements
Tags
ttributes
'ntities
&$DT
$DT
The following is a brief e"planation of each of the building blocks(
Element
'lements are the main $uildin% $loc& of both XML and 4TML documents.
'"amples of 4TML elements are /body/ and /table/. '"amples of XML elements could be /note/ and
/message/. 'lements can contain te"t! other elements! or be empty. '"amples of empty 4TML elements are
/hr/! /br/ and /img/.
Ta%
Tags are used to mar&u' element.
starting tag like 5element_name6 mark up the beginning of an element! and an ending tag like
53element_name6 mark up the end of an element.
'"amples(
+od, element mar5ed 78 4it) +od, ta*s9
<+od,>+od, text in +et4een<0+od,>!
messa*e element mar5ed 78 4it) messa*e ta*s9
<messa*e>some messa*e in +et4een<0messa*e>
(ttri$ute
ttributes pro+ide extra information a$out element.
ttributes are always placed inside the starting tag of an element. ttributes always come in name3+alue pairs.
The following /img/ element has additional information about a source file(
<im* src="com87ter!*i(" 0>
The name of the element is /img/. The name of the attribute is /src/. The +alue of the attribute is
/computer.gif/. 7ince the element itself is empty it is closed by a / 3/.
Entitie
'ntities are +ariables used to define common text. 'ntity references are references to entities.
Most of you will know the 4TML entity reference( /8nbsp9/. This /no:breaking:space/ entity is used in
4TML to insert an e"tra space in a document. 'ntities are e"panded when a document is parsed by an XML
parser.
The following entities are predefined in XML(
Entity )eference Character
8lt9 5
8gt9 6
8amp9 8
8;uot9 /
8apos9 <
PCD(T(
&$DT means parsed character data.
Think of character data as the te"t found between the start tag and the end tag of an XML element.
PCD(T( i text that *ill $e 'ared $y a 'arer. Tags inside the te"t will be treated as markup and entities
will be e"panded.
CD(T(
$DT also means character data.
CD(T( i text that *ill NOT $e 'ared $y a 'arer. Tags inside the te"t will =#T be treated as markup
and entities will not be e"panded.
DTD " Element
dtd_building.asp dtd_building.asp dtd_attributes.asp dtd_attributes.asp
In a DTD! XML elements are declared with a DTD element declaration.
Declarin% an Element
In the DTD! XML elements are declared with an element declaration. n element declaration has the
following synta"(
<!E#E$E%T element-name cate*or,>
or
<!E#E$E%T element-name &element-content->
Em'ty element
'mpty elements are declared with the category keyword 'M&T%(
<!E#E$E%T element-name E$PTY>
DTD exam8le9
<!E#E$E%T +r E$PTY>
:$# exam8le9
<+r 0>
Element *ith character data
'lements with only character data are declared with 1&$DT inside parentheses(
<!E#E$E%T element-name &.PCD/T/->
exam8le9
<!E#E$E%T note &.PCD/T/->
1&$DT are character data that will be parsed by a parser. If a 1&$DT section contains elements! these
elements must also be declared.
Element *ith any content
'lements declared with the category keyword =%! can contain any combination of parsable data(
<!E#E$E%T element-name /%Y>
exam8le9
<!E#E$E%T note /%Y>
Element *ith children +e,uence-
'lements with one or more children are defined with the name of the children elements inside parentheses(
<!E#E$E%T element-name
&c)ild-element-name->
or
<!E#E$E%T element-name
&c)ild-element-name'c)ild-element-name'!!!!!->
exam8le9
<!E#E$E%T note &to'(rom')eadin*'+od,->
2hen children are declared in a se;uence separated by commas! the children must appear in the same
se;uence in the document. In a full declaration! the children must also be declared! and the children can also
ha+e children. The full declaration of the note document will be(
<!E#E$E%T note &to'(rom')eadin*'+od,->
<!E#E$E%T to &.PCD/T/->
<!E#E$E%T (rom &.PCD/T/->
<!E#E$E%T )eadin* &.PCD/T/->
<!E#E$E%T +od, &.PCD/T/->
Declarin% only one occurrence of the ame element
<!E#E$E%T element-name &c)ild-name->
exam8le9
<!E#E$E%T note &messa*e->
The e"ample declaration abo+e declares that the child element message can only occur one time inside the
note element.
Declarin% minimum one occurrence of the ame element
<!E#E$E%T element-name &c)ild-name;->
exam8le9
<!E#E$E%T note &messa*e;->
The > sign in the e"ample abo+e declares that the child element message must occur one or more times inside
the note element.
Declarin% .ero or more occurrence of the ame element
<!E#E$E%T element-name &c)ild-name<->
exam8le9
<!E#E$E%T note &messa*e<->
The ? sign in the e"ample abo+e declares that the child element message can occur @ero or more times inside
the note element.
Declarin% .ero or one occurrence of the ame element
<!E#E$E%T element-name &c)ild-name?->
exam8le9
<!E#E$E%T note &messa*e?->
The A sign in the e"ample abo+e declares that the child element message can occur @ero or one times inside
the note element.
Declarin% either/or content
exam8le9
<!E#E$E%T note &to'(rom')eader'messa*e=+od,->
The e"ample abo+e declares that the element note must contain a to element! a from element! a header
element! and either a message element or a body element.
Declarin% mixed content
exam8le9
<!E#E$E%T note &.PCD/T/=to=(rom=)eader=messa*e-<>
The e"ample abo+e declares that the element note can contain parsed character data or any number of to!
from! header! message elements.
DTD " Entitie
dtd_attributes.asp dtd_attributes.asp dtd_+alidation.asp dtd_+alidation.asp
'ntities are +ariables used to define shortcuts to common te"t.
: 'ntity references are references to entities.
: 'ntities can be declared internal! or e"ternal
Internal Entity Declaration
6,ntax9
<!E%T>TY entit,-name "entit,-val7e">
DTD Exam8le9
<!E%T>TY 4riter "Donald D7c5!">
<!E%T>TY co8,ri*)t "Co8,ri*)t ?@6c)ools!">
:$# exam8le9
<a7t)or>A4riterBAco8,ri*)tB<0a7t)or>
External Entity Declaration
6,ntax9
<!E%T>TY entit,-name 6Y6TE$ "C2>0C2#">
DTD Exam8le9
<!E%T>TY 4riter
6Y6TE$ ")tt8900444!4@sc)ools!com0entities0entities!xml">
<!E%T>TY co8,ri*)t
6Y6TE$ ")tt8900444!4@sc)ools!com0entities0entities!dtd">
:$# exam8le9
<a7t)or>A4riterBAco8,ri*)tB<0a7t)or>
DTD 0alidation
dtd_entities.asp dtd_entities.asp dtd_e"amples.asp dtd_e"amples.asp
Internet '"plorer *.B can +alidate your XML against a DTD.
0alidatin% *ith the #ML Parer
If you try to open an XML document! the XML &arser might generate an error. Cy accessing the parse'rror
obDect! the e"act error code! the error te"t! and e+en the line that caused the error can be retrie+ed(
var xmlDoc = ne4 /ctive:O+Dect&"$icroso(t!:$#DO$"-
xmlDoc!as,nc="(alse"
xmlDoc!validateOnParse="tr7e"
xmlDoc!load&"noteEdtdEerror!xml"-
doc7ment!4rite&"<+r>Error Code9 "-
doc7ment!4rite&xmlDoc!8arseError!errorCode-
doc7ment!4rite&"<+r>Error 2eason9 "-
doc7ment!4rite&xmlDoc!8arseError!reason-
doc7ment!4rite&"<+r>Error #ine9 "-
doc7ment!4rite&xmlDoc!8arseError!line-
Try it %ourself or or Dust look at the XML file
Turnin% 0alidation off
Ealidation can be turned off by setting the XML parser<s +alidate#n&arseF/false/.
var xmlDoc = ne4 /ctive:O+Dect&"$icroso(t!:$#DO$"-
xmlDoc!as,nc="(alse"
xmlDoc!validateOnParse="(alse"
xmlDoc!load&"noteEdtdEerror!xml"-
doc7ment!4rite&"<+r>Error Code9 "-
doc7ment!4rite&xmlDoc!8arseError!errorCode-
doc7ment!4rite&"<+r>Error 2eason9 "-
doc7ment!4rite&xmlDoc!8arseError!reason-
doc7ment!4rite&"<+r>Error #ine9 "-
doc7ment!4rite&xmlDoc!8arseError!line-
Try it %ourself
( %eneral #ML 0alidator
To help you +alidate your "ml files! we ha+e created this link so that you can Ealidate any XML file.
The 'areError O$1ect
%ou can read more about the parse'rror obDect in our XML D#M 7chool.
DTD " Exam'le from the Net
dtd_+alidation.asp dtd_+alidation.asp default.asp default.asp
T0 2chedule DTD
Cy Da+id Moisan. $opied from his 2eb( http(33wwwG.shore.net3Hdmoisan3
<!DOCTYPE TF6CGEDC#E [
<!E#E$E%T TF6CGEDC#E &CG/%%E#;->
<!E#E$E%T CG/%%E# &H/%%E2' D/Y;->
<!E#E$E%T H/%%E2 &.PCD/T/->
<!E#E$E%T D/Y &&D/TE' GO#>D/Y- = &D/TE' P2OI2/$6#OT;--;>
<!E#E$E%T GO#>D/Y &.PCD/T/->
<!E#E$E%T D/TE &.PCD/T/->
<!E#E$E%T P2OI2/$6#OT &T>$E' T>T#E' DE6C2>PT>O%?->
<!E#E$E%T T>$E &.PCD/T/->
<!E#E$E%T T>T#E &.PCD/T/->
<!E#E$E%T DE6C2>PT>O% &.PCD/T/->
<!/TT#>6T TF6CGEDC#E %/$E CD/T/ .2EJC>2ED>
<!/TT#>6T CG/%%E# CG/% CD/T/ .2EJC>2ED>
<!/TT#>6T P2OI2/$6#OT FT2 CD/T/ .>$P#>ED>
<!/TT#>6T T>T#E 2/T>%I CD/T/ .>$P#>ED>
<!/TT#>6T T>T#E #/%IC/IE CD/T/ .>$P#>ED>
]>
( )e'ort DTD
Cy Iichard 'rlander. $opied from his 2eb( http(33pdbeam.uwaterloo.ca3Hrlander3
<!DOCTYPE 2EPO2T [
<!E%T>TY xml "Extensi+le $ar578 #an*7a*e">
<!E%T>TY s*ml "6tandard IeneraliKed $ar578 #an*7a*e">
<!E%T>TY 8xa "Pro(essional :$# /7t)orin*">
<!E%T>TY L TE:T "&.PCD/T/=CODE=MEY?O2D=JCOT/T>O%-<">
<!E%T>TY L H#OCM "&P/2/=#>6T-;">
<!E%T>TY L HODY "&LH#OCMB=E:/$P#E=%OTE-;">
<!E#E$E%T 2EPO2T &T>T#E'&6ECT>O%=6GO2T6ECT-;->
<!E#E$E%T 6ECT>O% &T>T#E'LHODYB'6CH6ECT>O%<->
<!E#E$E%T 6CH6ECT>O% &T>T#E'LHODYB'6CH6ECT>O%<->
<!E#E$E%T 6GO2T6ECT &T>T#E'LHODYB->
<!E#E$E%T T>T#E LTE:TB>
<!E#E$E%T P/2/ LTE:TB>
<!E#E$E%T #>6T &>TE$-;>
<!E#E$E%T >TE$ &LH#OCMB->
<!E#E$E%T CODE &.PCD/T/->
<!E#E$E%T MEY?O2D &.PCD/T/->
<!E#E$E%T E:/$P#E &T>T#E?'LH#OCMB->
<!E#E$E%T I2/PG>C E$PTY>
<!/TT#>6T 2EPO2T sec7rit, &)i*) = medi7m = lo4 - "lo4">
<!/TT#>6T CODE t,8e CD/T/ .>$P#>ED>
<!/TT#>6T I2/PG>C (ile E%T>TY .2EJC>2ED>
<!%OT/T>O% I>N 6Y6TE$ "">
<!%OT/T>O% 1PI 6Y6TE$ "">
<!%OT/T>O% H$P 6Y6TE$ "">
]>
Ne*'a'er (rticle DTD
$opied from http(33www.+er+et.com3
<!DOCTYPE %E?6P/PE2 [
<!E#E$E%T %E?6P/PE2 &/2T>C#E;->
<!E#E$E%T /2T>C#E &GE/D#>%E' HY#>%E' #E/D' HODY' %OTE6->
<!E#E$E%T GE/D#>%E &.PCD/T/->
<!E#E$E%T HY#>%E &.PCD/T/->
<!E#E$E%T #E/D &.PCD/T/->
<!E#E$E%T HODY &.PCD/T/->
<!E#E$E%T %OTE6 &.PCD/T/->
<!/TT#>6T /2T>C#E /CTGO2 CD/T/ .2EJC>2ED>
<!/TT#>6T /2T>C#E ED>TO2 CD/T/ .>$P#>ED>
<!/TT#>6T /2T>C#E D/TE CD/T/ .>$P#>ED>
<!/TT#>6T /2T>C#E ED>T>O% CD/T/ .>$P#>ED>
<!E%T>TY %E?6P/PE2 "Fervet #o*ic Times">
<!E%T>TY PCH#>6GE2 "Fervet #o*ic Press">
<!E%T>TY COPY2>IGT "Co8,ri*)t OOP Fervet #o*ic Press">
]>
Product Catalo% DTD
$opied from http(33www.+er+et.com3
<!DOCTYPE C/T/#OI [
<!E%T>TY /CTGO2 "1o)n Doe">
<!E%T>TY CO$P/%Y "1D Po4er Tools' >nc!">
<!E%T>TY E$/># "DdQDd-tools!com">
<!E#E$E%T C/T/#OI &P2ODCCT;->
<!E#E$E%T P2ODCCT
&6PEC>N>C/T>O%6;'OPT>O%6?'P2>CE;'%OTE6?->
<!/TT#>6T P2ODCCT
%/$E CD/T/ .>$P#>ED
C/TEIO2Y &GandTool=Ta+le=6)o8-Pro(essional- "GandTool"
P/2T%C$ CD/T/ .>$P#>ED
P#/%T &Pitts+7r*)=$il4a75ee=C)ica*o- "C)ica*o"
>%FE%TO2Y &>n6toc5=Hac5ordered=Discontin7ed- ">n6toc5">
<!E#E$E%T 6PEC>N>C/T>O%6 &.PCD/T/->
<!/TT#>6T 6PEC>N>C/T>O%6
?E>IGT CD/T/ .>$P#>ED
PO?E2 CD/T/ .>$P#>ED>
<!E#E$E%T OPT>O%6 &.PCD/T/->
<!/TT#>6T OPT>O%6
N>%>6G &$etal=Polis)ed=$atte- "$atte"
/D/PTE2 &>ncl7ded=O8tional=%ot/88lica+le- ">ncl7ded"
C/6E &Gard6)ell=6o(t=%ot/88lica+le- "Gard6)ell">
<!E#E$E%T P2>CE &.PCD/T/->
<!/TT#>6T P2>CE
$62P CD/T/ .>$P#>ED
?GO#E6/#E CD/T/ .>$P#>ED
6T2EET CD/T/ .>$P#>ED
6G>PP>%I CD/T/ .>$P#>ED>
<!E#E$E%T %OTE6 &.PCD/T/->
]>

Das könnte Ihnen auch gefallen