XML Validation I DTDs Robin Burke ECT 360
XML Validation I DTDs Robin Burke ECT 360 Winter 2004
Outline History ¢ Grammars / Regular expressions ¢ DTDs ¢ elements l attributes l entities l ¢ Declarations
Validation ¢ Why bother?
The idea ¢ Language consists of terminals l ¢ a, b, c Set of productions l beginning with non-terminals • A, B, C l rules specifying how to generate sequences of terminals
Example A a. B ¢ A a. BA ¢B b ¢ generates strings ¢ l ababab etc.
Grammar ¢ Can be used to efficiently parse a language basis of all modern programming language parsing since Algol-60 l Java Language Specification is completely in EBNF grammar l
Grammar ¢ XML grammar-based syntax l adheres to EBNF l ¢ SGML had a more complex language definition syntax l HTML is defined the SGML way l
Regular expressions ¢ ¢ Language for expressing patterns Basic components l l l l pattern elements optional element = ? repetition (1 or more) = + repetition (0 or more) = * choice = | grouping = ( ) sequence = ,
Examples ¢ (a, b)* l ¢ all strings "ab" "abab" etc. (a | b | c)+, q, (b, c)* aaqb l bqcccc l
Note ¢ Regular expressions are different in different applications Perl l Javascript l XML Schemas l ¢ DTDs only support l ? +*|, ()
EBNF ¢ EBNF is more compact version of BNF l ¢ ¢ ¢ A a. BA turns into l ¢ it uses regular expressions to simplify grammar expression A a. B(A)? only one production per non-terminal allowed
DTDs Use EBNF to specify structure of XML documents ¢ Plus ¢ attributes l entities l ¢ Syntax holdover from SGML l Ugly l
DTD Syntax <!ELEMENT element-name content_model> ¢ Content model contains the RHS of the production rule ¢ Example ¢ <!ELEMENT name (first. Name, last. Name)>
DTD Syntax cont'd ¢ Not XML <! begins a declaration l No "content" l Empty elements not indicated with /> l
Simple content models ¢ Content can be any text l ¢ #PCDATA Content can be anything at all (useful for debugging) l ANY l ¢ Element has no content l EMPTY
Example <grades> <grade> <student>Jane Doe</student> <assigned-grade>A</assigned-grade> </grade> <student>John Doe</student> <assigned-grade>A-</assigned-grade> </grades>
Example <grades> <grade> <student>Jane Doe</student> <assigned-grade>A</assigned-grade> </grade> <student>John Doe</student> <assigned-grade>A-</assigned-grade> </grade> <student>Wayne Doe</student> <assigned-grade>I</assigned-grade> <reason>Alien abduction</reason> </grades>
DTD?
Mixed content ¢ Legal to have a content model with text and element data <story category="national" byline="Karen Wheatley"> <headline>President Meets with Congress</headline> The President meet with Congressional leaders today in effort to jump-start faltering budget negotiations. Sources described the mood of the meeting as "cordial". <full_text ref="news 801" /> <image src="img 2071. jpg" /> <image src="img 2072. jpg" /> <image src="img 2073. jpg" /> </story>
Mixed content, cont'd <!ELEMENT story (headline, #PCDATA, full-story, image*)> ¢ Mixed content makes handling XML complex ¢ l necessary for many applications
Recursion ¢ Unlike grammars l ¢ recursive formulation ≠ repetition Difference between <!ELEMENT students (student+)> l <!ELEMENT students (student, students? )> l
Restriction ¢ The grammar cannot be ambiguous A (a, b)| (a, c) l this makes the parser implementation difficult l ¢ Usually easy to make non-ambiguous l A a, (b | c)
Attribute lists ¢ Declared separately from elements l ¢ can be anywhere in the DTD Specification includes name of the element l name of the attribute l attribute type l default l
Attribute types ¢ ¢ ¢ Character data l CDATA l different from XML CDATA section! Enumerated l (yes|no) ID l must be unique in the document IDREF l must refer to an id in the document NMTOKEN l a restriction of CDATA to single "word" Also IDREFS and NMTOKENS
Default declaration #REQUIRED ¢ #IMPLIED ¢ l ¢ Value l ¢ means optional this becomes the default #FIXED l value provided
Examples <!ATTLIST img src CDATA #REQUIRED alt CDATA #REQUIRED align (left|right|center) "left" id ID #IMPLIED > <!ATTLIST timestamp time-zone NMTOKEN #IMPLIED>
Entities ¢ Like macros content to be inserted l indicated with &name; l ¢ Predefined general entities & < l essential part of XML l ¢ User-defined general entities l &disclaimer;
Entities, cont'd ¢ Parameter entities can also be used to simplify DTD creation l or to combine DTDs l indicated with a % l ¢ More on this next week
Defining general entities <!ENTITY name content> ¢ Example <!ENTITY disclaimer "This is a work of fiction. Any resemblance to persons living or dead is unintentional. ">
In-class exercise ¢ Business cards
Next week ¢ More DTDs Entities l Modularization and parameterization l pg. 129 -148 l
Lab
- Slides: 32