1 / 14

CITA 330 Section 2

CITA 330 Section 2. DTD. Defining XML Dialects. “Well-formedness” is the minimal requirement for an XML document; all XML parsers can check it

december
Download Presentation

CITA 330 Section 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CITA 330 Section 2 DTD

  2. Defining XML Dialects • “Well-formedness” is the minimal requirement for an XML document; all XML parsers can check it • Any useful XML document must follow the syntax rules of a specific XML dialect: which tags and attributes can be used, how elements can be ordered or nested … • Major mechanisms for defining XML dialects: • Document Type Definition (DTD) • XML Schema (XSD) • XML validating parsers can read an XML instance document and its syntax definition DTD/XSD file to validate whether the XML document conforms to the syntax constraints

  3. Document Type Definition • DTD is simpler than XSD and can specify less syntax constraints • DTD is part of XML specification • DTD syntax is not based on XML • Usually DTD is specified in a separate file so it can be referred to by many of its instance XML documents • Local DTD definitions, especially entity name definitions, can also be included at the top of an XML document to override some global definitions

  4. External DTD Example <!ELEMENT library (dvd+)> <!ELEMENT dvd (title, format, genre)> <!ELEMENT title (#PCDATA)> <!ELEMENT format (#PCDATA)> <!ELEMENT genre (#PCDATA)> <!ATTLIST dvd id CDATA #REQUIRED> • A “library” element contains one or more “dvd” elements • A “dvd” element contains one “title” element, one “format” element, and one “genre” elements, in the same order • The “title”, “format” and “genre” elements all have strings as their values • A “dvd” element has a required attribute “id” whose value is a string

  5. Declaring Elements • Empty elements • <!ELEMENT elementName (EMPTY)> • Example: <!ELEMENT br (EMPTY)> • Elements with text or generic data • <!ELEMENT elementName (#PCDATA)> • #PCDATA means that the element contains data that is going to be parsed by a parser for markups including entity references but not for nested elements • <!ELEMENT elementName (ANY)> • The keyword ANY declares an element with any content as its value, including text, entity references and nested elements. Any element nested in this element must also be declared

  6. Declaring Elements • Elements with children (sequences) • <!ELEMENT elementName (childElementNames)> • <!ELEMENT index (term, pages)> • <!ELEMENT footnote (message)> • Elements with zero or more nested element • <!ELEMENT elementName (childName*)> • <!ELEMENT footnote (message*)> • Elements with one or more nested element • <!ELEMENT elementName (childName+)> • <!ELEMENT footnote (message+)> • Elements with optional nested elements • <!ELEMENT elementName (childName?)> • <!ELEMENT footnote (message?)>

  7. Declaring Elements • Elements with alternative nested elements • <!ELEMENT section (data_section | application_section)> • Elements with mixed content • <!ELEMENT email (to+, from, header, attachment*, #PCDATA)> • An email element must contain in the same order at least one to child element, exactly one from child element, exactly one header element, zero or more attachment elements, and some other parsed character data as well

  8. Operators Used with Element Content • Comma (,) specifies a required sequence of child elements • Vertical line or pipe (|) specifies a list of candidate child elements • Question mark (?) specifies that the child element is optional • Plus Sign (+) specifies that at least one child element(s) is required • Asterisk (*) specifies the zero or more of the child element(s) may appear

  9. Declaring Attributes • Syntax: <!ATTLIST elementName attributeName attributeType defaultValue> • DTD • <!ELEMENT circle EMPTY> • <!ATTLIST circle radius CDATA "1"> • XML • <circle radius="10"></circle> • <circle /> (having default radius 1) • DTD • <!ATTLIST circle type (solid|outline) "solid"> • XML • <circle type="solid"/> • <circle type="outline"/>

  10. Declaring Attributes • Declaring an optional attribute without default value • DTD • <!ATTLIST circle radius CDATA #IMPLIED> • XML • <circle radius="10"></circle> • <circle /> (having no attribute radius ) • Declaring a mandatory attribute • DTD • <!ATTLIST circle radius CDATA #REQUIRED> • XML • <circle radius="10"></circle> • <circle /> (invalid element)

  11. attributeType

  12. Declaring Entity Names • Syntax: <!ENTITY entityName "entityValue"> • <!ENTITY euro "&#8364;"> • <!ENTITY cs "Computer Science"> • Example usage of entity names • XML code: A &cs; book costs me &euro;52. • View: A Computer Science book costs me €52.

  13. Associating DTD Declarations to XML Documents • Including DTD declarations in an XML document • <!DOCTYPE rootElementTag [DTD-Declarations]> <!DOCTYPE library [ <!ELEMENT library (dvd+)> <!ELEMENT dvd (title, format, genre)> <!ELEMENT title (#PCDATA)> <!ELEMENT format (#PCDATA)> <!ELEMENT genre (#PCDATA)> <!ATTLIST dvd id CDATA #REQUIRED> ]> <library> <dvd id="1"> <title>Gone with the Wind</title> <format>Movie</format> <genre>Classic</genre> </dvd> </library>

  14. Associating DTD Declarations to XML Documents • Referencing an external DTD file from an XML document • <!DOCTYPE rootElementTag SYSTEM DTD-URL> <!DOCTYPE library SYSTEM "library.dtd"> <library> <dvd id="1"> <title>Gone with the Wind</title> <format>Movie</format> <genre>Classic</genre> </dvd> </library>

More Related