REVISED: Sunday, March 3, 2013
You will receive an introduction to XML DTD.
I. XML DTD OVERVIEW
XML "document type definition" (DTD). The DTD defines the elements that may be included in your document, what attributes these elements have, and the ordering and nesting of the elements. There are two ways to connect a DTD with the content of an XML file:
External DTD: You can put the DTD in a separate file from the XML file, and refer to it using a line at the beginning of the XML file. The advantage of this method is that many XML files can all refer to the same DTD.
Internal DTD: You can put the DTD inside the declaration at the front of the XML file. The advantage of an internal DTD is that the file can be validated by itself, without reference to any external files.
A. RULES FOR WELL FORMED XMLExternal DTD: You can put the DTD in a separate file from the XML file, and refer to it using a line at the beginning of the XML file. The advantage of this method is that many XML files can all refer to the same DTD.
Internal DTD: You can put the DTD inside the declaration at the front of the XML file. The advantage of an internal DTD is that the file can be validated by itself, without reference to any external files.
1. There must be exactly one top level element.
2. All opening tags bust be closed.
3. All elements are properly nested; i.e., there are no interleaved elements.
4. Attribute values must be enclosed in single or double quotes.
B. DTD
Here is a DTD:
<!DOCTYPE A [
<!ELEMENT A (B+, C)>
<!ELEMENT B (#PCDATA)>
<!ELEMENT C (B?, D)>
<!ELEMENT D (#PCDATA)>
]>
According to the DTD, an A element has within it one or more B subelements, and then a C element.
Within the C element is zero or one B elements followed by exactly one D element.
In terms of regular expressions, the tag sequences we can see are:
A (B /B)(B /B)* C (D /D | B /B D /D) /C /A.
Some text may appear between each B-/B pair and each D-/D pair, but text may not appear elsewhere.
C. XML
An XML document contains the following portion:
<EMP name = "Kermit">
<ADDR>123 Sesame St.</ADDR>
<PHONE type = "cell">555-1212</PHONE>
</EMP>
The XML must follow two rules:
1. A #REQUIRED attribute must appear in every element.
2. An attribute can have types CDATA, ID, or IDREF(S), but not #PCDATA.
D. XML DTD
Here is an XML DTD:
<!DOCTYPE meal [
<!ELEMENT meal (person*,food*,eats*)>
<!ELEMENT person EMPTY>
<!ELEMENT food EMPTY>
<!ELEMENT eats EMPTY>
<!ATTLIST person name ID #REQUIRED>
<!ATTLIST food name ID #REQUIRED>
<!ATTLIST eats diner IDREF #REQUIRED dish IDREF #REQUIRED>
]>
Focus on the ID and IDREF attributes.
A valid document needs to have unique values across ID attributes.
An IDREF attribute can refer to any existing ID attribute value.
E. XML
An XML document contains the following portion:
<INFO>
<ADDR>101 Maple St.</ADDR>
<PHONE>555-1212</PHONE>
<PHONE>555-4567</PHONE>
</INFO>
In the XML snippet, the info element has one address subelement and two phone subelements, in that order.
In the DTD the list of components for INFO must include ADDR, ADDR*, ADDR+,or ADDR? followed by PHONE* or PHONE+.
Interspersed with these may be any elements that are not required to appear-- that is, any components with a ? or *.
We might also have components like NAME* or MANAGER? at any point in the list.
F. XML SCHEMA SPECIFICATION
Study the following XML Schema specification:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="fname" type="xs:string"/>
<xs:element name="initial" type="xs:string"
minOccurs="0"/>
<xs:element name="lname" type="xs:string"/>
<xs:element name="address" type="xs:string"
maxOccurs="2"/>
<xs:choice>
<xs:element name="major" type="xs:string"/>
<xs:element name="minor" type="xs:string"
minOccurs="2" maxOccurs="2"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This question deals with the xs:element, xs:sequence, and xs:choice elements in XML Schema.
In order for XML to be valid according to the specified schema:
The elements contained in a sequence must appear in exactly the same order as specified in the xs:sequence.
Exactly one of the elements contained in an xs:choice must appear.
If an element specifies a minOccurs attribute, the XML must contain at least thatmany instances of the element.
If an element specifies a maxOccurs attribute, the XML must not contain more thanthat many instances of the element.
If minOccurs and maxOccurs are not specified, their default value is 1.
Elements not defined as a part of a sequence or choice cannot occur inside the corresponding xs:sequence and xs:choice.
The given schema specifies the following constraints:
The "fname", "initial", "lname", and "address" elements must occur in that sequennce.
The "initial" element is optional due to its minOccurs value being 0.
The "address" element can occur either 1 or 2 times due to its maxOccurs value being 2.
After the "address" element, either exactly one "major" element or exactly 2 "minor" elements can occur, but not both.
Elements not defined as a part of this schema specification are not allowed to occur as a part of the "person" element.
Here is an example of valid XML for this schema:
<person>
<fname>John</fname> <initial>Q</initial>
<lname>Public</lname>
<address>123 Public Avenue</address>
<address>Seattle, WA 98001</address>
<major>Computer Science</major>
</person>
You will have received an introduction to XML DTD, enjoy!
Elcric Otto Circle
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment