2014年2月13日星期四

XML-->DTD&Schema Notes


  • Introduction


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

  • Building Block

Elements, Attributes, Entities, CDATA, PCDATA
PCDATA should not contain any characters like &, > or < which should be represented by &amp, &lt and &gt entities, respectively.
CDATA will not be parsed by a parser.

elements:

  1. Declaring Elements:<!ELEMENT element-name category>
  2. Empty elements: <!ELEMENT oven EMPTY>  <oven/>
  3. Elements with only parsed character data: <!ELEMENT from (#PCDATA)>
  4. can contain any combination of parable data: <!ELEMENT note ANY> replace by specific content now.
  5. Elements with one or more children: <!ELEMENT element-name (child1,child2,…)>
  6. Only one occurrence: <!ELEMENT element-name (child-name)>
  7. one or more occurrence: <!ELEMENT element-name (child-name+)>
  8. zero or more occurrence: <!ELEMENT element-name (child-name*)>
  9. zero or one occurrence: <!ELEMENT element-name (child-name?)>
  10. either or occurrence: <!ELEMENT note (to,from,header,(message|body))>
  11. mixed content: <!ELEMENT note (#PCDATA|to|from|header|message)*>
5:consecutively; 11: no specific sequence

attributes:

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


Declaration:     <!ATTLIST payment type CDATA "check">  
XML example:     <payment type="check" /> 

TypesDescription
CDATA (strings)The value is character data except <, >, &, and
(en1|en2|..)  enumeratedThe value must be one from an enumerated list
ID tokenized most restrictiveThe value is a unique id--> Uniquely identifies an element
IDREF tokenizedThe value is the id of another element--> Point to element with ID
IDREFS tokenizedThe value is a list of other ids  consistency to ID
NMTOKENThe value is a valid XML name
NMTOKENSThe value is a list of valid XML names
ENTITY tokenizedThe value is an entity
ENTITIES tokenizedThe value is a list of entities
NOTATIONThe 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" />

elements vs attributes:

  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.


Entities:

internal entities: <!ENTITY entity-name "entity-value">
DTD Example:

<!ENTITY writer "Donald Duck.">
<!ENTITY copyright "Copyright W3Schools.">

XML example:

<author>&writer;&copyright;</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;&copyright;</author>

  • A General XML Validator Errors in XML documents will stop your XML program. 

To help you check your xml files, you can syntax-check any XML file here.

Semantic Web
















没有评论:

发表评论