Date post: | 12-Jan-2016 |
Category: |
Documents |
Upload: | magnus-gregory |
View: | 212 times |
Download: | 0 times |
XML (2)
DTD
Sungchul Hong
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE simple [
<!ELEMENT DOCUMENT (#PCDATA)>
<!ATTLIST DOCUMENT
trackNum CDATA #REQUIRED
secLevel (unclassified|classified) "unclassified">
<!ENTITY Description "This is a very simple
sample document.">
]>
<DOCUMENT trackNum="1234">This is an entity inside
an element:&Description; </DOCUMENT>
Document Type Definition
• A DTD is used to validate an XML document.• Only one DTD per document• Document type declaration
– <!DOCTYPE name […]>
• XML documents– Well formed documents
• Document must comply with the XML specification.
– Valid documents• A valid document is a document that has a DTD and follows the
rule laid out in that DTD.
Document Type Declarations
• <! DOCTYPE JU:LunchMenu SYSTEM http://catering.com/menus/lunch/Menu.DTD>
• <!DOCTYPE JU:LunchMenu PUBLIC “/lunch/Menu.DTD”>
Internal DTD Subset
• A DTD can be declared internal to the XML document
• Internal subsets are parsed before external subsets
• Internal declarations that match external declarations will override the corresponding declarations.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE simple [
<!ELEMENT DOCUMENT (TITLE,AUTHOR+,SUMMARY*,NOTE?)>
<!ATTLIST DOCUMENT
trackNum CDATA #REQUIRED
secLevel (unclassified|classified) "unclassified">
<!ELEMENT TITLE (#PCDATA)>
<!ELEMENT AUTHOR (#PCDATA)>
<!ELEMENT SUMMARY (#PCDATA)>
<!ENTITY Description "This is a very simple sample document.">
]>
<DOCUMENT trackNum="1234">
<TITLE>Sample Document</TITLE>
<AUTHOR>Simon St.Laurent</AUTHOR>
<SUMMARY>This is an entity inside an element:&Description;
</SUMMARY></DOCUMENT>
External DTD Subset
• The Document Type Declaration will now provide a URL and either a system or public identifier
• Provides more flexibility than internal DTD subsets
• <! DOCTYPE JU:LunchMenu SYSTEM http://catering.com/menus/lunch/Menu.DTD>
• <!DOCTYPE JU:LunchMenu PUBLIC “/lunch/Menu.DTD”>
<?xml version="1.0" standalone="no""UTF-8"?> <!DOCTYPE SIMPLE SYSTEM "http://127.0.0.1/simple.dtd"> <SIMPLE><DOCUMENT trackNum="1234"> <TITLE>Sample Document</TITLE> <AUTHOR><FIRSTNAME>Simon</FIRSTNAME> <LASTNAME>St.Laurent</LASTNAME> <COMPANY>XML Mania</COMPANY></AUTHOR><SUMMARY>This is an entity inside an element:&Description; </SUMMARY></DOCUMENT></SIMPLE>
Elements
• <!ELEMENT name data>
• <!ELEMENT DOCUMENT (TITLE,AUTHOR+,SUMMARY*,NOTE?)>
• Declares what elements are legal, what order they should appear in and the number of elements
• Data can be one of five types: Empty, Elements-only, Characters-only, any and mixed.
Elements• Empty: the corresponding element cannot contain either text or other elements
– <!ELEMENT price EMPTY>• Element-only: the corresponding element can only contain other elements, no
text.– <!ELEMENT person (name, address, tel)>
• Characters-only: The corresponding element can contain only text– <!ELEMENT bookinfo (#PCDATA) >
• Any: the corresponding element can include any of previous three types– <!ELEMENT other ANY>
• Mixed: the corresponding element must have both elements and text– <!ELEMENT mixedup (e1, e2) (#PCDATA)>
Elements Syntax
• Content model– ( ): substructure– , : strict ordering– | : choice– ?: optional– *: zero or more of a particular item can appear.– +: The plus-sign (+) denotes that one or more of
a particular item can appear.
Example<!ELEMENT DOCUMENT
(TITLE,AUTHOR+,SUMMARY*,NOTE?)>
<!ELEMENT User (Name, Email | Phone))>
<!ELEMENT Blah (A, (B?, C)*, D+, (E|F)>
Attributes Syntax
• A DTD defines attributes with the <!ATTLIST …> declaration.
• <!ATTLIST element attribute CDATA #required>– #REQUIRED-the attribute must appear with this
element.– #IMLIED- the attribute may appear with this element.– #FIXED default value – the attribute must always have
the default value. If it does not appear explicitly, it is assumed.
– Default value – the attribute may appear with the default or can have another value.
Attribute Types
• CDATA – Character data. Used if an attribute value is only plain text.
• <!ATTLIST Order order_num CDATA #REQUIRED>
• <order order_num = “123abc”> … </order>
Attribute Types
• ID – unique name within a document used to uniquely identify an element.
• <!ATTLIST Order order_num ID #REQUIRED>
• <Order order_num = “123abc”> .. </order>
• <order order_num = “456def”> …</order>
Attribute Types• IDREF- A reference to an element with an ID,
allowing for links to b created within documents.
• <!ATTLIST Order order_num ID #REQUIRED>• <!ATTLIST ClosedOrder order_ref IDREF
#REQUIRED>• <Order order_num = “123abc”>… </Order>• <ClosedOrder order_ref = “123abc”/>
Attribute Types
• IDREFS – Series of references to elements with corresponding Ids
• <!ELEMENT ClosedOrders EMPTY>• <!ATTLIST Order order_num ID #REQUIRED>• <!ATTLIST ClosedOrders order_ref IDREFS
#REQUIRED>• <Order order_num = “123abc”>… </Order>• <Order order_num = “456def”>… </Order>• <ClosedOrders order_ref = “123abc 456def”/>
Enumerated Values
• Enumerated values are defined with not type definition.
• A parenthetical sequence of legal values is given instead
• Do not use quotation marks and remember the values are case sensitive
• <!ATTLIST JU:Dessert type (regular | lowfat | sugar-free) #REQUIRED>
• <JU:Dessert type = “lowfat”>Cheescake</JU:Dessert>
NOTATION
• NOTATION- Identify the format of external data items that we wish to use with our XML document
• <!NOTATION gif SYSTM “imageviewer.exe”>
Entities
• Entities allow you t declare content and reference it anytime you need it within a document
• Entities are like an alias to some content
• Predefined entities <, >, &, &apos, "e
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE simple [
<!ELEMENT DOCUMENT (#PCDATA)>
<!ATTLIST DOCUMENT
trackNum CDATA #REQUIRED
secLevel (unclassified|classified) "unclassified">
<!ENTITY Description "This is a very simple
sample document.">
]>
<DOCUMENT trackNum="1234">This is an entity inside
an element:&Description; </DOCUMENT>
Problems With DTD
• DTD use a non-XML syntax.
• There is no way to type information like numbers
• DTDs do not allow merging of documents
• XML Schema