The need for XML “schemas”
DTD is the abbreviation for "Document Type Definition" to define the legal building blocks of an XML document with a list of legal elements and attributes, which can be defined inline an XML doc or as an external reference. With the DTD, your can verify the data that you receive from the outside world is valid. Elements/attributes names in XML are case-sensitive, so DTD must be case-sensitive also!
Parsing XML Documents
– Valid document conforms to DTD
» Document is then well formed, by definition
» Documents can be well formed, but not valid
Example: inline example
<?xml version="1.0"?> <? PI ?>
<!DOCTYPE note [ // defines that the root element of this document is note
<!ELEMENT note (to,from,heading,body)> //defines that the note element contains four elements: "to,from,heading,body"
<!ELEMENT to (#PCDATA)> // defines the to element to be of type "#PCDATA"
<!ELEMENT from (#PCDATA)> Parsable character data
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note> Root element
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
external example:
use <!DOCTYPE note SYSTEM "note.dtd"> to replace the inline DTD block
SYSTEM indicates its a private DTD (not for public distribution)
Elements, Attributes, Entities, CDATA, PCDATA
PCDATA should not contain any characters like &, > or < which should be represented by &, < and > entities, respectively.
CDATA will not be parsed by a parser.
5:consecutively; 11: no specific sequence
<!ATTLIST element-name attribute-name attribute-type attribute-value>
Declaration: <!ATTLIST payment typeCDATA "check">
XML example: <payment type="check" />
Types |
Description |
---|---|
CDATA (strings) |
The value is character data except <, >, &, ’and ” |
(en1|en2|..) enumerated |
The value must be one from an enumerated list |
ID tokenized most restrictive |
The value is a unique id--> Uniquely identifies an element |
IDREF tokenized |
The value is the id of another element--> Point to element with ID |
IDREFS tokenized |
The value is a list of other ids consistency to ID |
NMTOKEN |
The value is a valid XML name |
NMTOKENS |
The value is a list of valid XML names |
ENTITY tokenized |
The value is an entity |
ENTITIES tokenized |
The value is a list of entities |
NOTATION |
The value is a name of a notation |
xml: |
The value is a predefined xml value |
value: the value of the attribute
Required:
<!ATTLIST person number CDATA #REQUIRED>
Valid XML:
<person number="5677" />
Invalid XML:
<person />
Implied:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />
Fixed:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company="W3Schools" />
Enumerated attribute values:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />
There is no rule for when to use elements or attributes
Store data in element is better and use attribute to provide information not relevant to data.
Metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.
internal entities: <!ENTITY entity-name "entity-value">
DTD Example:
<!ENTITY writer "Donald Duck.">
<!ENTITY copyright "Copyright W3Schools.">
XML example:
<author>&writer;©right;</author>
external entities:<!ENTITY entity-name SYSTEM "URI/URL">
DTD Example:
<!ENTITY writer SYSTEM "http://www.w3schools.com/entities.dtd">
<!ENTITY copyright SYSTEM "http://www.w3schools.com/entities.dtd">
XML example:
<author>&writer;©right;</author>
To help you check your xml files, you can syntax-check any XML file here.
Semantic Web