Document Type DefinitionsXML
DTD stands for Document Type DefinitionAllows an XML document to go further than meeting the requirements of being well-formedSpecifies requirements to be validA valid XML document matches definitions of allowable elements, attributesDTD Overview
Validation can be done in code (i.e. using javascript, VB and DOM)DTD’s allow use of a validating parser that compares the document against specificationsTypically makes application changes and maintenance easierLess tied to a particular programming language/environmentValidation
Declarations are used to specify document requirementsDocument type declarationElement declarationAttribute List declarationEntity declarationDeclarations
Includes name of root elementAllows specification of where the DTD is locatedDTD can be embedded in the XML file (local)DTD can refer to external file, Uniform Resource Identifier (URI)Local takes precedence over externalDocument Type Declaration
Element Declaration has 3 parts:DeclarationElement nameElement contentElement content can include a list of child elements or dataElement Declaration
DTD included in XML documentDefinition of a student:<!DOCTYPE student[<!ELEMENT student(first, last, studentID)><!ELEMENT first (#PCDATA)><!ELEMENT last(#PCDATA)><ELEMENT studentID(#PCDATA)>]>LocalDTDDocument Type DeclarationElement DeclarationA student element is made up of first name, last name, and student id elements
DTD exists in external file/locationMust use keyword to specify type of locationSYSTEM is a reference to local file systemPUBLIC is reference to DTD accessed through a catalogCan use both togetherIf can’t find catalog reference can use specified fileExternal Definition
Reference in XML file:<!DOCTYPE student SYSTEM “student.dtd”>External file:<!ELEMENT student(first, last, studentID)><!ELEMENT first (#PCDATA)><!ELEMENT last(#PCDATA)><ELEMENT studentID(#PCDATA)>]>Sample External DefinitionDocument Type DeclarationElement Declaration
Element name must match name in XML documentIf using namespaces, prefixes must matchContent Model defines what the element can storeAn elementMixed (i.e. data and element)EmptyAnyWorking With Elements
One element can contain anotherCan specify the elements contained by sequenceCan specify the elements contained as a choiceElement Content
Error raised if an element is missingError raised if there are extra elementsError raised if elements in a different orderFor a student, our content must be in firstname, lastname, studentID orderIf find an element “major”, errorIf order varies, errorIf missing first, last, or studentID, errorContent by Sequence
Can allow content to vary between elements| (vertical bar or pipe) indicates ORIf  add a Grade element to a student that can be a letter or percent:<!ELEMENT grade (letter | percent)><!ELEMENT letter (#PCDATA)><!ELEMENT percent (#PCDATA)Indicates that must have letter or percent element  Content by Choice
A name may be a full name (first, middle, last) or just first and last:<!ELEMENT name (fullName | (first, last))><!ELEMENT fullName (first, middle, last)><!ELEMENT first (#PCDATA)><!ELEMENT middle (#PCDATA)><!ELEMENT last (#PCDATA)>By Choice: Example
Allows combination of elements and parsed character dataCan include additional information within an element, eg. how to displayRules:Managed by using Choice (or)PCDATA must appear first in list of elementsList cannot include inner content model (only simple elements)If there are child elements, include * * Indicates that may appear zero or more timesMixed Content
If want to include emphasis with the letter gradeData: <letter><em>4</em></letter>Declaration:<!ELEMENT letter (#PCDATA | em)*>Describes a letter element as the content (pcdata) plus emphasis elementMixed Content -2
An element can be empty<br /> (never has child, content)Declaration includes EMPTY:<!ELEMENT br EMPTY>Means that the element CANNOT contain contentEmpty Content
An element can contain any kind of value (or be empty)Any elements declared in the DTD can occur, any number of timesOnly elements that are part of the DTD can be part of the document!May be emptyMay contain PCDATALeast restrictive modelAny Content
How many times can an element occur?How many times must an element occur?Cardinality
A student must have a first nameA student may or may not have a last nameA student may have one or more majors, or none (undeclared)	<!ELEMENT student (first, last?, major*)>Note: Cardinality indicator doesn’t affect the element declaration (i.e. major)Cardinality: Example
Elements tend to be used to describe a logical unit of informationAttributes are typically used to store data about characteristics (properties)May have a Movie element with attributes for Title, Rental Price, Rental DaysNo specific rules about how to use elements and attributesAttributes and DTD’s
Attributes allow more limits on dataCan have a list of acceptable valuesCan have a default valueSome ability to specify a data typeConcise, about a single name/value pairAttributes have limitsCan’t store long strings of textCan’t nest valuesWhitespace can’t be ignoredAttributes and Elements
Declaration:<!ATTLISTElementNameAttrNameAttrType Default>Specify the Element the attribute belongs toSpecify the Name of the attributeSpecify the Type of data the attribute storesSpecify characteristics of the values (Default or attribute value)List either the default value or other characteristic of value – required, optionalSpecifying Attributes
CDATA – unparsed character dataEnumerated – series/list of string valuesEntity/Entities – reference entity definition(s)ID – unique identifier for the elementIDREF – refer to the ID of another elementIDREFS – list of ID’s of other elements separated by whitespaceNMTOKEN/NMTOKENS – value(s) of attribute can be anything that follows rules for XML nameSample Attribute Data Types
Specifies that attribute value must be found in a particular listEach value in list must be valid XML nameLimits on spaces, charactersUse | (pipe) to separate members of listIf specifying list letter grades for a student:<!ATTLIST student grade (A | B | C | D | F | V | W | I) #IMPLIED>Enumerated AttributesElementAttributeEnumerated List
An ID specifies that the element must have a unique value within the documentAllows reliable way to refer to a specific elementNo spaces allowed in valueTypically replace space with underscoreAttribute list can include only one IDIDREF, IDREFS allows an element to be associated with another or multiple other elementsA student element must have a student ID:<!ATTLIST student studentID ID #REQUIRED>ID, IDREF, IDREFS
Attributes can refer to entities“Entity” refers to substituting a reference for a text value& refers to the & characterUnparsed Entity is a reference that isn’t parsedCan reuse references for long values, or hard to manage characters (i.e. tab, line feed)Entity must be declared in the DTD<!ENTITY classTitle “XML”>When classTitle found in document, replaced with XMLEntities and Attributes
Can specify how the value will appear in the documentMust always specify a value declarationDEFAULT sets a value for an attribute if a value isn’t providedInclude default value in double quotesFIXED sets a value that must occur; if an attribute has a different value, a validation error occursREQUIRED specifies that the attribute (and value) must existIMPLIED means the attribute is optionalAttribute Value Declarations

Document Type Definitions

  • 1.
  • 2.
    DTD stands forDocument Type DefinitionAllows an XML document to go further than meeting the requirements of being well-formedSpecifies requirements to be validA valid XML document matches definitions of allowable elements, attributesDTD Overview
  • 3.
    Validation can bedone in code (i.e. using javascript, VB and DOM)DTD’s allow use of a validating parser that compares the document against specificationsTypically makes application changes and maintenance easierLess tied to a particular programming language/environmentValidation
  • 4.
    Declarations are usedto specify document requirementsDocument type declarationElement declarationAttribute List declarationEntity declarationDeclarations
  • 5.
    Includes name ofroot elementAllows specification of where the DTD is locatedDTD can be embedded in the XML file (local)DTD can refer to external file, Uniform Resource Identifier (URI)Local takes precedence over externalDocument Type Declaration
  • 6.
    Element Declaration has3 parts:DeclarationElement nameElement contentElement content can include a list of child elements or dataElement Declaration
  • 7.
    DTD included inXML documentDefinition of a student:<!DOCTYPE student[<!ELEMENT student(first, last, studentID)><!ELEMENT first (#PCDATA)><!ELEMENT last(#PCDATA)><ELEMENT studentID(#PCDATA)>]>LocalDTDDocument Type DeclarationElement DeclarationA student element is made up of first name, last name, and student id elements
  • 8.
    DTD exists inexternal file/locationMust use keyword to specify type of locationSYSTEM is a reference to local file systemPUBLIC is reference to DTD accessed through a catalogCan use both togetherIf can’t find catalog reference can use specified fileExternal Definition
  • 9.
    Reference in XMLfile:<!DOCTYPE student SYSTEM “student.dtd”>External file:<!ELEMENT student(first, last, studentID)><!ELEMENT first (#PCDATA)><!ELEMENT last(#PCDATA)><ELEMENT studentID(#PCDATA)>]>Sample External DefinitionDocument Type DeclarationElement Declaration
  • 10.
    Element name mustmatch name in XML documentIf using namespaces, prefixes must matchContent Model defines what the element can storeAn elementMixed (i.e. data and element)EmptyAnyWorking With Elements
  • 11.
    One element cancontain anotherCan specify the elements contained by sequenceCan specify the elements contained as a choiceElement Content
  • 12.
    Error raised ifan element is missingError raised if there are extra elementsError raised if elements in a different orderFor a student, our content must be in firstname, lastname, studentID orderIf find an element “major”, errorIf order varies, errorIf missing first, last, or studentID, errorContent by Sequence
  • 13.
    Can allow contentto vary between elements| (vertical bar or pipe) indicates ORIf add a Grade element to a student that can be a letter or percent:<!ELEMENT grade (letter | percent)><!ELEMENT letter (#PCDATA)><!ELEMENT percent (#PCDATA)Indicates that must have letter or percent element Content by Choice
  • 14.
    A name maybe a full name (first, middle, last) or just first and last:<!ELEMENT name (fullName | (first, last))><!ELEMENT fullName (first, middle, last)><!ELEMENT first (#PCDATA)><!ELEMENT middle (#PCDATA)><!ELEMENT last (#PCDATA)>By Choice: Example
  • 15.
    Allows combination ofelements and parsed character dataCan include additional information within an element, eg. how to displayRules:Managed by using Choice (or)PCDATA must appear first in list of elementsList cannot include inner content model (only simple elements)If there are child elements, include * * Indicates that may appear zero or more timesMixed Content
  • 16.
    If want toinclude emphasis with the letter gradeData: <letter><em>4</em></letter>Declaration:<!ELEMENT letter (#PCDATA | em)*>Describes a letter element as the content (pcdata) plus emphasis elementMixed Content -2
  • 17.
    An element canbe empty<br /> (never has child, content)Declaration includes EMPTY:<!ELEMENT br EMPTY>Means that the element CANNOT contain contentEmpty Content
  • 18.
    An element cancontain any kind of value (or be empty)Any elements declared in the DTD can occur, any number of timesOnly elements that are part of the DTD can be part of the document!May be emptyMay contain PCDATALeast restrictive modelAny Content
  • 19.
    How many timescan an element occur?How many times must an element occur?Cardinality
  • 20.
    A student musthave a first nameA student may or may not have a last nameA student may have one or more majors, or none (undeclared) <!ELEMENT student (first, last?, major*)>Note: Cardinality indicator doesn’t affect the element declaration (i.e. major)Cardinality: Example
  • 21.
    Elements tend tobe used to describe a logical unit of informationAttributes are typically used to store data about characteristics (properties)May have a Movie element with attributes for Title, Rental Price, Rental DaysNo specific rules about how to use elements and attributesAttributes and DTD’s
  • 22.
    Attributes allow morelimits on dataCan have a list of acceptable valuesCan have a default valueSome ability to specify a data typeConcise, about a single name/value pairAttributes have limitsCan’t store long strings of textCan’t nest valuesWhitespace can’t be ignoredAttributes and Elements
  • 23.
    Declaration:<!ATTLISTElementNameAttrNameAttrType Default>Specify theElement the attribute belongs toSpecify the Name of the attributeSpecify the Type of data the attribute storesSpecify characteristics of the values (Default or attribute value)List either the default value or other characteristic of value – required, optionalSpecifying Attributes
  • 24.
    CDATA – unparsedcharacter dataEnumerated – series/list of string valuesEntity/Entities – reference entity definition(s)ID – unique identifier for the elementIDREF – refer to the ID of another elementIDREFS – list of ID’s of other elements separated by whitespaceNMTOKEN/NMTOKENS – value(s) of attribute can be anything that follows rules for XML nameSample Attribute Data Types
  • 25.
    Specifies that attributevalue must be found in a particular listEach value in list must be valid XML nameLimits on spaces, charactersUse | (pipe) to separate members of listIf specifying list letter grades for a student:<!ATTLIST student grade (A | B | C | D | F | V | W | I) #IMPLIED>Enumerated AttributesElementAttributeEnumerated List
  • 26.
    An ID specifiesthat the element must have a unique value within the documentAllows reliable way to refer to a specific elementNo spaces allowed in valueTypically replace space with underscoreAttribute list can include only one IDIDREF, IDREFS allows an element to be associated with another or multiple other elementsA student element must have a student ID:<!ATTLIST student studentID ID #REQUIRED>ID, IDREF, IDREFS
  • 27.
    Attributes can referto entities“Entity” refers to substituting a reference for a text value& refers to the & characterUnparsed Entity is a reference that isn’t parsedCan reuse references for long values, or hard to manage characters (i.e. tab, line feed)Entity must be declared in the DTD<!ENTITY classTitle “XML”>When classTitle found in document, replaced with XMLEntities and Attributes
  • 28.
    Can specify howthe value will appear in the documentMust always specify a value declarationDEFAULT sets a value for an attribute if a value isn’t providedInclude default value in double quotesFIXED sets a value that must occur; if an attribute has a different value, a validation error occursREQUIRED specifies that the attribute (and value) must existIMPLIED means the attribute is optionalAttribute Value Declarations