XML Part II DTD (cont.) and XML Schema
Attribute Declarations Declare a list of allowable attributes for each element These lists are called ATTLIST declarations Consists of 3 basic parts The ATTLIST keyword The associated element s name The list of declares attributes Each attribute in list consists of 3 parts The attribute name The attribute type The attribute value declaration
Example 1 <!ATTLIST contacts source CDATA #IMPLIED> ATTLIST keyword Associated element List of declared attributes Attribute name Attribute type Attribute value
Attribute naming rules are similar to element naming rule Duplicate attribute names are not allow in attribute list Attribute types (next page)
ID, IDREF, IDREFS ID s value must follow the rules for XML names The value of ID must be unique with in the entire XML document Only one ID is allowed per element The attribute value declaration for ID must be #IMPLIED, #REQUIRED For the value of IDREF, the value must match the value of some ID within the XML document
ENTITY and ENTITIES References to unparsed entities Unparsed entities refers to reference to an external file that processor connot parse, e.g. image file ENTITY or ENTITIES must refer to an ENTITY that has been declared somewhere in the DTD Value of ENTITY must follow the rules for XML name
NMTOKEN and NMTOKENS Used to refer to a concept or single word Value can be any word as long as it follow XML naming rules ENUMERATED Allow ability to specify a list of allowable values Each value must be a valid XML name <!ATTLIST phone kind (Home Work Cell Fax) #IMPLIED>
Attribute Value Declarations Within each attribute declaration, how the value will appear in the document must be specified as one of the following: Has a default value Has a fixed value Is required Is implied ( is optional)
Default value Specifying default value is just include the value in after the attribute type <!ATTLIST phone kind (Home Work Cell Fax) "Home"> Fixed values Use the #FIXED keyword followed by fixed value <!ATTLIST contacts version CDATA #FIXED "1.0"> Required value Used to specify that the attribute must be included with in XML document <!ATTLIST phone kind (Home Work Cell Fax) #REQUIRED>
Implied values In most case the attribute declared won t be required <!ATTLIST knows contacts IDREFS #IMPLIED>
Declaring multiple attribute for an element <!ATTLIST contacts version CDATA #FIXED 1.0 source CDATA #IMPLIED> OR <!ATTLIST contacts version CDATA #FIXED 1.0 > <!ATTLIST contacts source CDATA #IMPLIED>
Entities Refer to character reference Refer to sections of replacement text, etc. Refer to external file 4 kinds of entities: Built-in entities Character entities General entities Parameter entities
Built-in entities Escape characters: & < > &apos " Character entities Use for characters that are difficult to type or for non-ascii characters &#yyy; &#xhhh;
General entities Unlike built-in and character entities, general entities must be declared with in DTD There are 2 ways to declare general entities Internal entity declaration External entity declaration <!ENTITY source-text "Beginning' of List"> Keyword Entity name Value
References to general entities Reference to general entities Start with & Immediately follow the & is general entity s name End with semi-colon &general-reference;
Parameter entities Quite similar to general entities Unlike general entities, parameter entities can be used within DTD only Reference to parameter entities use % instead of & <!ENTITY % DefaultPhoneKind ""Home""> <!ATTLIST phone kind (Home Work Cell Fax) %DefaultPhoneKind;>
DTD Limitation DTD syntax and XML syntax are different Poor support for XML namespaces Poor data typing Limited content model description
XML Schemas A schema is any type of model document that defines the structure of something XML Schema refers to the specific W3C XML Schema technology XML Schema technology use for describing the structure of XML document XML Schema definitions = XSDs
Benefits of XML Schemas Using basic XML Fully support the Namespace Recommendation Enable validating text element based on built-in and user-defined data-types Enable concept such as object inheritance Creation of complex and reusable content model is easy
<?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/xmlschema" xmlns:target="http://www.example.com/name" targetnamespace="http://www.example.com/name" elementformdefault="qualified"> <element name="name"> <complextype> <sequence> <element name="first" type="string"/> <element name="middle" type="string"/> <element name="last" type="string"/> </sequence> <attribute name="title" type="string"/> </complextype> </element> </schema>
<?xml version="1.0"?> <name xmlns="http://www.example.com/name" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.example.com/name name5.xsd" title="mr."> <first>john</first> <middle>fitzgerald Johansen</middle> <last>doe</last> </name>
<schema> declaration Root element of XML Schema Default namespace and version attribute can be declared in this element <schema xmlns="http://www.w3.org/2001/xmlschema"> or <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"> or <xsd:schema xmlns:xsd="http://www.w3.org/2001/xmlschema">
Target namespace Use for referring to declarations within the XML Schema <schema xmlns="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.example.com/name" xmlns:target="http://www.example.com/name"> or <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.example.com/name" xmlns="http://www.example.com/name">
Element and Attribute Qualification The element or attribute is qualify if there is an associates to it <n:name xmlns:n="http://www.example.com/name"> <n:first>john</n:first> <n:middle>fitzgerald</n:middle> <n:last>doe</n:last> </n:name> <n:name xmlns:n="http://www.example.com/name"> <first>john</first> <middle>fitzgerald</middle> <last>doe</last> </n:name>
Within <schema>, the attributes use to modify the default specifying how elements should be qualified: elementformdefault attributeformdefault Default value of both attributes are unqualified <element> declarations <element name="name of the element" type="global type" ref="global element declaration" form="qualified or unqualified" minoccurs="non negative number" maxoccurs="non negative number or unbounded " default="default value" fixed="fixed value">
2 types of elements Local,Global Local Type Example <element name="name"> <complextype> <sequence> <element name="first" type="string"/> <element name="middle" type="string"/> <element name="last" type="string"/> </sequence> <attribute name="title" type="string"/> </complextype> </element>
Local Type Example <element name= name > <simpletype> <restriction base= string > <enumeration value= Home /> <enumeration value= Work /> <enumeration value= Cell /> <enumeration value= Fax /> </restriction> </simpletype> </element>
Global Type Examples <schema xmlns= http://www.w3.org/2001/xmlschema xmlns:target= http://www.example.com/name targetnamespace= http://www.example.com/name elementformdefault= qualified > <complextype name= NameType > <sequence> <element name= first type= string /> <element name= middle type= string /> <element name= last type= string /> </sequence> <attribute name= title type= string /> </complextype> <element name= name type= target:nametype /> </schema>
Global Type Examples <xs:schema xmlns:xs= http://www.w3.org/2001/xmlschema xmlns= http://www.example.com/name targetnamespace= http://www.example.com/name elementformdefault= qualified > <xs:complextype name= NameType > <xs:sequence> <xs:element name= first type= xs:string /> <xs:element name= middle type= xs:string /> <xs:element name= last type= xs:string /> </xs:sequence> <xs:attribute name= title type= xs:string /> </xs:complextype> <xs:element name= name type= NameType /> </xs:schema>
<?xml version= 1.0?> <schema xmlns= http://www.w3.org/2001/xmlschema xmlns:target= http://www.example.com/name targetnamespace= http://www.example.com/name elementformdefault= qualified > <element name= first type= string /> <element name= middle type= string /> <element name= last type= string /> <complextype name= NameType > <sequence> <element ref= target:first /> <element ref= target:middle /> <element ref= target:last /> </sequence> <attribute name= title type= string /> </complextype> <element name= name type= target:nametype /> </schema>
Cardinality Represent number of occurrences of specific element within content model minoccurs and maxoccurs are attributes that used to modify an element s cardinality <element name= first type= string minoccurs= 2 maxoccurs= 2 /> <element ref= target:first maxoccurs= 10 /> <element name= location minoccurs= 0 maxoccurs= unbounded />
Default and Fixed values Default values <element name= last type= string default= Doe /> In XML document <last></last>or<last/> equivalent to <last>doe</last>
fixed values <element name= ver type= string fixed= 1.0 /> In XML document <ver></ver> or <ver/> or <ver>1.0</ver> are legal <ver></ver> or <ver/> are equivalent to <ver>1.0</ver> <ver>2.0</ver> is not legal
Element wildcards <any minoccurs= non negative number maxoccurs= non negative number or unbounded namespace= allowable namespaces processcontents= lax or skip or strict > Parameter of namespace attribute
<complextype name= NameType > <sequence> <element ref= target:first /> <element ref= target:middle /> <element ref= target:last /> <!-- allow any element from any namespace --> <any namespace= ##any processcontents= lax minoccurs= 0 maxoccurs= unbounded /> </sequence> <attribute name= title type= string /> </complextype>
<complextype> Declarations <complextype mixed= true or false name= Name of complextype > Local <complextype> definitions are never named, they are called anonymous complex types Global <complextype> definitions are always named mixed attribute allows the creation of element that include both text and element within content model
<element name= description > <complextype mixed= true > <choice minoccurs= 0 maxoccurs= unbounded > <element name= em type= string /> <element name= strong type= string /> <element name= br type= string /> </choice> </complextype> </element> XML document example <description>jeff is a developer & author for Beginning XML <em>4th edition</em> 2006 Wiley Publishing.<br/>Jeff <strong>loves</strong>xml! </description>
Empty content model <element name= knows > <complextype> </complextype> </element> <element name= knows > <complextype/> </element> <knows/> or <knows></knows>
<complextype> definitions can contain <attribute> declaration <element name= knows > <complextype> <attribute name= contacts type= IDREFS /> </complextype> </element>
<group> Declarations XML Schemas allow defining reusable groups of elements <group name= name of global group>
<?xml version= 1.0?> <schema xmlns= http://www.w3.org/2001/xmlschema xmlns:target= http://www.example.com/name targetnamespace= http://www.example.com/name elementformdefault= qualified > <group name= NameGroup > <sequence> <element name= first type= string minoccurs= 1 maxoccurs= unbounded /> <element name= middle type= string minoccurs= 0 maxoccurs= 1 /> <element name= last type= string /> </sequence> </group> <complextype name= NameType > <group ref= target:namegroup /> <attribute name= title type= string /> </complextype> <element name= name type= target:nametype /> </schema>
Content Models In XML Schemas, an element s content model can be specified using the following: A <sequence> declaration A <choice> declaration A reference to a global <group> declaration An <all> declaration Each of the above declarations may contain: Inner content models Element declarations Element wildcards
<sequence> Declarations The attributes minoccurs and maxoccurs are allowed minoccurs and maxoccurs used to specified how many time this sequence of elements occurs <sequence minoccurs= non negative number maxoccurs= non negative number or unbounded >
Example <sequence> <element name= first type= string minoccurs= 1 maxoccurs= unbounded /> <element name= middle type= string minoccurs= 0 maxoccurs= 1 /> <element name= last type= string /> </sequence> <first>john</first> <middle>fitzgerald Johansen</middle> <last>doe</last> <last>doe</last> <middle>fitzgerald</middle> <first>john</first>
<choice> Declarations minoccurs and maxoccurs are allowed for modification of cardinality of <choice> Multiple child declarations are declared within <choice> but only one declaration may be used <choice minoccurs= non negative number maxoccurs= non negative number or unbounded > <choice> <element name= first type= string minoccurs= 1 maxoccurs= unbounded /> <element name= middle type= string minoccurs= 0 maxoccurs= 1 /> <element name= last type= string /> </choice>
<group> reference Allows reference to global element groups within content model <group ref= global group definition minoccurs= non negative number maxoccurs= non negative number or unbounded > <group name= NameGroup > <sequence> <element name= first type= string minoccurs= 1 maxoccurs= unbounded /> <element name= middle type= string minoccurs= 0 maxoccurs= 1 /> <element name= last type= string /> </sequence> </group> <element name= name > <complextype> <group ref= target:namegroup /> <attribute name= title type= string /> </complextype> </element>
<all> Declarations Allow the elements in content model to appear in any order There are several rules for using <all> The <all> declaration must be the only content model that appear as a child of a <complextype> The <all> declaration is not permitted to contain <sequence>, <choice>, or <group> declarations The <all> declaration s children may appear once each
Example <element name= name > <complextype> <all> <element name= first type= string /> <element name= middle type= string /> <element name= last type= string /> </all> <attribute name= title type= string /> </complextype> </element>
<first>john</first> <middle>fitzgerald</middle> <last>doe</last> Or <first>john</first> <last>doe</last> <middle>fitzgerald</middle>
<attribute> Declarations Attribute declarations are similar to element declarations There are 2 primary method for declaring attributes Local type Global type Unlike element declarations, attribute declarations are restricted to simple type
<attribute> declaration format <attribute name= name of the attribute type= global type ref= global attribute declaration form= qualified or unqualified use= optional or prohibited or required default= default value fixed= fixed value >
Creating a local type <attribute name= title > <simpletype> <!-- type information --> </simpletype> </element>
Creating a global type <schema xmlns= http://www.w3.org/2001/xmlschema xmlns:contacts= http://www.example.com/contacts targetnamespace= http://www.example.com/contacts elementformdefault= qualified > <simpletype name= KindType > <!-- type information --> </simpletype> <element name= phone > <complextype> <!-- content model information --> <attribute name= kind type= contacts:kindtype /> </complextype> </element> </schema>
<schema xmlns= http://www.w3.org/2001/xmlschema xmlns:contacts= http://www.example.com/contacts targetnamespace= http://www.example.com/contacts elementformdefault= qualified > <attribute name= test type= string /> <element name= phone > <complextype> <!-- content model information --> <attribute ref= contacts:test /> </complextype> </element> </schema>
Attribute use Use to specify that an attribute is required or optional or prohibited Default and fixed Allow declaring default and fixed value Example <attribute name= kind type= contacts:kindtype default= Home /> <attribute name= version type= string fixed= 1.0 />
<attributegroup> Declarations Define reusable groups of attributes <attributegroup name= ContactAttributes > <attribute name= version type= string fixed= 1.0 /> <attribute name= source type= string /> </attributegroup> <element name= contacts > <complextype> <sequence> <element name= contact minoccurs= 0 maxoccurs= unbounded > <complextype> <sequence> <element name= name type= contacts:nametype /> <element name= location type= contacts:locationtype /> </sequence> </complextype> </element> </sequence> <attributegroup ref= contacts:contactattributes /> </complextype> </element>
<element name= latitude type= float /> <element name= longitude type= float /> <attributegroup name= ContactAttributes > <attribute name= version type= decimal fixed= 1.0 /> <attribute name= source type= string /> </attributegroup>
<simpletype> Declaration User-defined datatypes Every <simpletype> definition is derived from another datatype <simpletype> definitions are often called derived types There are 3 primary derived types: Restriction types List types Union types
<restriction> Declaration <attribute name= kind > <simpletype> <restriction base= string > <enumeration value= Home /> <enumeration value= Work /> <enumeration value= Cell /> <enumeration value= Fax /> </restriction> </simpletype> </attribute>
<list> Declaration <simpletype name= ContactTagsType > <restriction base= string > <enumeration value= author /> <enumeration value= xml /> <enumeration value= poetry /> <enumeration value= consultant /> <enumeration value= CGI /> <enumeration value= semantics /> <enumeration value= animals /> </restriction> </simpletype> <simpletype name= ContactTagsListType > <list itemtype= contacts:contacttagstype /> </simpletype>
<union> Declarations <simpletype name= UnknownString > <restriction base= string > <enumeration value= Unknown /> </restriction> </simpletype> <simpletype name= UnknownOrFloatType > <union membertypes= float contacts:unknownstring /> </simpletype>
<element name= latitude type= contacts:unknownstringorfloattype /> <element name= longitude type= contacts:unknownstringorfloattype /> <latitude>43.847156</latitude> <longitude>unknown</longitude> <latitude>unknown</latitude> <longitude>43.847156 Unknown</longitude>