160 likes | 295 Views
CIS 451: XML DTDs. Dr. Ralph D. Westfall February, 2009. Additional XML Issues. what is a "well-formed" XML document? what is a "valid" XML document? what is a DTD? how do you create DTDs?. Well-Formed XML Document. <textbooks> <book>Great Gatsby</book> </textbooks>
E N D
CIS 451: XML DTDs Dr. Ralph D. Westfall February, 2009
Additional XML Issues • what is a "well-formed" XML document? • what is a "valid" XML document? • what is a DTD? • how do you create DTDs?
Well-Formed XML Document • <textbooks> • <book>Great Gatsby</book> • </textbooks> • a uniquely named root element • one or more elements • elements are properly nested • inner tags always closed before outer ones
Well-Formed Document - 2 • complies with XML rules • for example: has "legal" characters • regular alphanumeric characters • &#_ _ _ ; special characters identified by decimal #s (like &60; = < in HTML) • &#x_ _ _ _ _ _ ; hexadecimal #s (0-f) • other files pulled into document must also be well-formed (e.g., stylesheets)
Valid XML Document • must be a well-formed document • must also comply with "rules" in a data type definition (DTD) • DTD contains specifications ("rules") created by user • user creates both rules and XML document tags • if you don't like the rules, change them!!
Creating a DTD • separate file (use .dtd extension, not .xml) • DTD can be in same file, but separate is much better • all elements defined by <!ELEMENT … > • DTD starts with a root element, which contains other elements • remaining elements either contain more elements, or specifications for type of data
Creating a DTD - Start • <?xml version="1.0"?><!--slsrcpt.xml.doc--> • make sure that you: • put <?xml etc. at top of .dtd file • don't put <!DOCTYPE etc. in .dtd file • error if <!DOCTYPE is in both .xml and .dtd files • put <!DOCTYPE in .xml file instead, e.g.<!DOCTYPE books SYSTEM "books.dtd"> • identifies DOCTYPE (root element) and pulls in .dtd file (like an include or a reference to an external stylesheet)
Creating a DTD - Root • <!ELEMENT textbooks (book+)> • textbooks is "root" element • textbooks element contains at least one book element • + means one or more • (like in regular expressions) • similar spelling is NOT necessary e.g., could have texts element as root
Creating a DTD - Children • <!ELEMENT book (title, ISBN, authors, description?, price+)> • book element contains 5 elements • description element can be absent • ? means optional (once or not at all) • (just like in regular expressions) • how many times does price appear?
Creating a DTD - Data • <!ELEMENT title (#PCDATA)> • title element contains PC (parsed character) data • e.g., the .xml file has following line <title>ASP.NET 1.0</title> • the text between the tags is the data
Creating a DTD - Empty • <!ELEMENT price EMPTY> • price is an "empty" element (no data) • empty element can have attributes (next page) • can also have an effect (cf. HTML <br/> effect) • 2 ways to close elements (empty or not) • <price></price> (always have to close) • <price /> (abbreviated like in ASP form)
Creating a DTD - Attributes • <!ATTLIST price • US CDATA #REQUIRED> • identifies 1 or more attributes, as follows: • element nameprice • attribute nameUS • typeCDATA(character data) • default#REQUIRED (other options) • better if attribute list is just below the corresponding element (but not required)
Creating a DTD - Declarations • element declaration symbols ( ) group of items(title, ISBN, authors) , comma means exact order (a, b, c) a b c | pipe means any order (a | b | c) c b a ok ? once or not at all * zero to many times + one to many times (no symbol) means exactly one time
Put DTD Link into XML File • 2nd line of file • replace [name of] with actual filename <?xml version="1.0"?> <!DOCTYPE syllabus SYSTEM "[name of].dtd"> <?xml:stylesheet href="[name of].css" type="text/css"?>
Exercise • modify the slsrcpt.xml.doc file (or your file) • create .dtd file to match this file's pattern • can use syllabus2.dtd.doc as a sample pattern • put a link to slsrcpt.dtd file in slsrcpt.xml • test with a VBScriptfile (or use xmlint.exe) • xmlint [file].xml (in DOS) • put xmlint.exe and slsrcpt.xml in same directory • now correct your errors