XML>DTD&Schema Notes

 The need for XML “schemas”

•Unlike any other data format, XML is totally flexible, elements can be nested in arbitrary ways
•We can start by writing the XML data -- no need for a priori design of a schema
–Think relational databases, or Java classes
•However, schemas are necessary:
–Facilitate the writing of applications that process data
–Constraint the data that is correct for a certain application
–Have a priori agreements between parties with respect to the data being exchanged
•Schema: a model of the data
–Structural definitions
–Type definitions
–Defaults
  • 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!

Parsing XML Documents

•Parsers
–Validating
•Able to read DTD
•Determine whether XML document conforms to DTD 

Valid document conforms to DTD

» Document is then well formed, by definition

» Documents can be well formed, but not valid

–Nonvalidating
•Able to read DTD
•Cannot check document against DTD for conformity
 

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)

  • 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 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" />

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

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/bruceyo/p/3635225.html