1 / 23

XML DTD

XML DTD. Week 4 Web site: http://fog.ccsf.edu/~hyip. XML Document Type Definition (DTD). There are two principle systems for writing schema: DTD and XML Schema A DTD , or Document Type Definition , is an older, but widely used system with a peculiar and limited syntax

scrandell
Download Presentation

XML DTD

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. XML DTD Week 4 Web site: http://fog.ccsf.edu/~hyip

  2. XML Document Type Definition (DTD) • There are two principle systems for writing schema: DTD and XML Schema • A DTD, or Document Type Definition, is an older, but widely used system with a peculiar and limited syntax • XML Schema is written in XML itself, and more powerful than a DTD

  3. Working with DTDs • A DTD (Document Type Definition) is a set of rules that defines a custom markup language in XML. • A DTD simply identifies elements and their attributes. • If an XML document does not adhere to the rules defined by the DTD, it is not considered valid for that particular custom language. • A DTD is a text-only document and is customarily saved with a .dtd extension. It is not an XML document itself and therefore, does not begin with the standard XML declaration.

  4. Working with DTDs (continue…) • A DTD defines rules for every element and attribute that can appear in an XML document. • You must declare the DTD in your XML document in order to use it. • Once this declaration is made, you can use various tools to validate the XML document against the DTD. • This is the reason for creating a DTD to insure that a given XML document is constructed in a specific way as defined by the DTD. • DTDs can be written and saved as separate files, or they can be written entirely inside an XML document. • A benefit of writing internal DTD is that there is only one file to manage. • A benefit of writing external DTD is that they can easily be used to validate many XML documents. They can be used by other people or companies who are generating XML documents to validate their documents before sending them to you, and vice versa.

  5. External DTD • Define the rules for the DTD in a external file, then save the file as text only using an extension of .dtd • There are two kinds of external dtd: • The DTD schema is only intended for internal private use. • The DTD schema is intended for external public usage.

  6. Declaring an External DTD (Internal Private Use) • Once you have created an external DTD, you need to refer to it within your XML document. • You do this using a document type declaration which declares the DTD. <?xml version="1.0" standalone ="no" ?> <!DOCTYPE root_name SYSTEM "external_file.dtd"> • NOTE: In the XML declaration at the top of the document, add standalone="no". This tells the XML parser that the document will rely on an external file. In this case, the one that contain the DTD.

  7. XML file with External DTD (Internal Private Use) <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- 2015-10-23 sample external DTD --> <!DOCTYPE customer SYSTEM "01_external_dtd_sample.dtd"> <customer> <name>John Smith</name> <phone>(415) 123-4567</phone> <email>jsmith@abc.com</email> </customer>

  8. External DTD file (Internal Private Use) <!ELEMENT customer (name, phone, email)> <!ELEMENT name (#PCDATA)> <!ELEMENT phone (#PCDATA)> <!ELEMENT email (#PCDATA)>

  9. Declaring and Creating an Internal DTD • For individual XML documents (one that you won’t be sharing), it is simplest to declare and create the DTD within the XML document itself. • The rules for creating a DTD, whether internal or external, are the same. • The difference between the two is the way the document type declaration is written; in other words, how the DTD is declared. • For internal DTD, it is at the top of your XML document, after the XML declaration: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE root_name [ Create your DTD by defining its elements and attributes. ]>

  10. Internal DTD sample <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!-- 2015-10-23 sample internal DTD --> <!DOCTYPE customer [ <!ELEMENT customer (name, phone, email)> <!ELEMENT name (#PCDATA)> <!ELEMENT phone (#PCDATA)> <!ELEMENT email (#PCDATA)> ]> <customer> <name>John Smith</name> <phone>(415) 123-4567</phone> <email>jsmith@abc.com</email> </customer>

  11. Combined External and Internal DTD (add one additional element) <?xml version="1.0"?> <!-- 2015-10-23 sample combined external/internal DTDs --> <!DOCTYPE customer SYSTEM "01_external_dtd_sample.dtd" [ <!ELEMENT address (#PCDATA)> ]> <customer> <name>John Smith</name> <phone>(415) 123-4567</phone> <email>jsmith@abc.com</email> <address>123 Street, San Francisco, CA 94123</address> </customer>

  12. Naming a Public External DTD (External Public Use) • If your DTD will be used by others (public), you should name your DTDs in a standard way; using a formal public identifier, or FPI. • An XML parser could then use the FPI to find the latest version of the DTD on a public web server. <!DOCTYPE root_name PUBLIC "id" "uri"> • For example XHTML: <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd">

  13. To name an external DTD id • Type -//, if your DTD is not a recognized standard (this is most common), or +//, if your DTD is an approved non-ISO standard, or ISO//, if your DTD is an approved ISO standard. • Then, type owner//, where owner identifies the person or organization who wrote and will maintain the DTD. • Next, type DTD description//, where description is a reference to the DTD and should contain a unique element, such as a version number. • Finally, type XX, where XX is the two-letter abbreviation for the language the DTD uses. -//myowner/DTD myowner description 2.0//EN +//myowner/DTD myowner description 2.0//EN ISO//myowner/DTD myowner description 2.0//EN

  14. XML file with External DTD (External Public Use) <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- 2015-10-23 sample external DTD --> <!DOCTYPE customer PUBLIC “-//myowner//DTD customer 2.0//EN" "http://www.myowner/dtd/ 01_external_dtd_sample.dtd"> <customer> <name>John Smith</name> <phone>(415) 123-4567</phone> <email>jsmith@abc.com</email> </customer>

  15. DTD Declaration • The schema rules for each element in an XML document are defined using <!ELEMENT> declarations in the DTD. • These rules specify which elements can be nested within other elements, and the allowable content for each element, using this syntax: <!ELEMENT element-name allowable-content> • There must be precisely one <!ELEMENT> declaration for each element and each declaration must contain the element name exactly as it appears in the XML document.

  16. DTD Declaration (continue…) • NOTE: A declaration that allows an element to contain ANY content defeats the purpose of creating a schema against which an XML document can be verified, so is best avoided.

  17. DTD Declaration (continue…) • To define an empty element: <!ELEMENT tag_name EMPTY> • To define an element that can contain anything: <!ELEMENT tag_name ANY> • To define an element that only contains text: <!ELEMENT tag_name (#PCDATA)> • To define an element to contain one child element: <!ELEMENT tag_name (child_name)> • (Refer to chapter 2, page 28-29)

  18. Specifying Element Sequence • An <!ELEMENT> declaration in a DTD schema can specify a rule that multiple different child elements must be nested within a parent element. • The tag name of each child element to be nested is specified in a comma-separated list, in the same sequential order that the elements should appear in the XML document. • The entire list must be contained within parentheses in the <!ELEMENT> declaration of the parent element. • To define an element with children: <!ELEMENT tag_name(child1, child2, child3)> • This rule defines a sequence of child elements that should each appear exactly once within one parent element.

  19. Controlling Element Occurrence • Child elements that are specified in an <!ELEMENT> declaration are normally allowed to occur exactly once within the parent element. • The number of allowable occurrences can be changed, however, by adding a special “occurrence indicator” symbol after the child element name in the declaration.

  20. Controlling Element Occurrence Sample <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE contacts [ <!ELEMENT contacts (title*, forename, surname)+ > <!ELEMENT title (#PCDATA)> <!ELEMENT forename (#PCDATA)> <!ELEMENT surname (#PCDATA)> ]> <contacts> <title>Mr</title> <forename>John</forename> <surname>Smith</surname> <forename>Sally</forename> <surname>James</surname> </contacts>

  21. Allowing Alternative Elements • An element may sometimes be required to allow a choice of child element – so allowable alternatives may be specified within its <!ELEMENT> declaration in a DTD schema. • The alternative child element names are separated by a “|” pipe character, which is often used to represent the boolean OR operator. • The entire alternative statement must be surrounded by parentheses – to indicate that a choice is allowable. • See chapter 2 page 34 – 35 sample

  22. Alternative Element Sample – xml file <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE doc SYSTEM "05_alternative_element.dtd"> <doc> <desc>This is a description for the xml file, and it requires an external dtd file. Description is only occurred once. </desc> <image> <src>img1.jpg</src> </image> <image> <alt>The existing image is not able to display instead this alternative text will be displayed.</alt> </image> </doc>

  23. Alternative Element Sample – dtd file <!ELEMENT doc (desc,image+)> <!ELEMENT image (src|alt)> <!ELEMENT desc (#PCDATA)> <!ELEMENT src (#PCDATA)> <!ELEMENT alt (#PCDATA)>

More Related