xp new perspectives on xml, 2nd edition tutorial 3 1 tutorial 3 validating an xml document

62
XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

Upload: candace-lamb

Post on 22-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

1

TUTORIAL 3

VALIDATING AN XML DOCUMENT

Page 2: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

2

CREATING A VALID DOCUMENT

• You validate documents to make certain necessary elements are never omitted.

• For example, each customer order should include a customer name, address, and phone number.

Page 3: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

3

CREATING A VALID DOCUMENT

• Some elements and attributes may be optional, for example an e-mail address.

• An XML document can be validated using either

– DTD (Document Type Definition)• Older, simplier language for describing how to render HTML

and XML documents

– Schema• Newer, more complex language for describing how to render

XML documents

Page 4: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

4

CUSTOMER INFORMATION COLLECTED BY KRISTEN

This figure shows customer information collected by Kristen

Could thisinformation be stored in a relational database?

Page 5: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

5

THE STRUCTURE OF KRISTEN’S DOCUMENT

This figure shows the overall structure of Kristen’s document

? = zero or one time = exactly one time+ = one or more times* = zero or more times

“Red” indicates correction

Page 6: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

6

DECLARING A DTD

• A DTD can be used to:– Ensure all required elements are present in the

document– Prevent undefined elements from being used– Enforce a specific data structure– Specify the use of attributes and define their possible

values– Define default values for attributes– Describe how the parser should access non-XML or

non-textual content

Page 7: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

7

DECLARING A DTD

• There can only be one DTD per XML document.

• A DTD is a collection of rules or declarations that define the content and structure of the document.

• A document type declaration attaches those rules to the document’s content.

Page 8: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

8

DECLARING A DTD

• You create a DTD by first entering a document type declaration into your XML document.

• DTD in this tutorial will refer to document type definition and not the declaration.

• While there can only be one DTD, it can be divided into two parts: an internal subset and an external subset.

Page 9: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

9

DECLARING A DTD

• An internal subset is declarations placed in the same file as the document content.

• An external subset is located in a separate file.

Page 10: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

10

DECLARING A DTD

• A DOCTYPE declaration can indicate both an external and an internal subset. The syntax is:

<!DOCTYPE root SYSTEM “URI” [ declarations ]> or <!DOCTYPE root PUBLIC “id” “URL” [ declarations ]>

Page 11: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

11

DECLARING A DTD

• The DOCTYPE declaration for an internal subset is:

<!DOCTYPE root

[

declarations

]>

• Where root is the name of the document’s root element, and declarations are the statements that comprise the DTD.

Placed in the same file as the document content

Page 12: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

12

DECLARING A DTD

• The DOCTYPE declaration for external subsets can take two forms:

– SYSTEM location <!DOCTYPE root SYSTEM “uri”>

• root = document’s root element,

• uri = location and filename of the external subset.

Placed in an external file that is accessed from the

XML document

Page 13: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

13

DECLARING A DTD

– PUBLIC location.<!DOCTYPE root PUBLIC “id” “uri”>

• root = document’s root element,

• id = public identifier (a unique name that can be recognized by the parser . The public identifier acts as like a name space. )

• uri = location and filename of the external subset.

• Use the PUBLIC location form when the DTD is placed in several locations or the DID is built into the XML parser itself.

• Unless your application requires a public identifier, you should use the SYSTEM location form.

Page 14: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

14

DECLARING A DTD

• If you place the DTD within the document, it is easier to compare the DTD to the document’s content.

• However, the real power of XML comes from an external DTD that can be shared among many documents written by different authors.

Page 15: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

15

DECLARING A DTD

• If a document contains both an internal and an external subset, the internal subset takes precedence over the external subset if there is a conflict between the two.

• This way, the external subset would define basic rules for all the documents, and the internal subset would define those rules specific to each document.

Page 16: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

16

COMBINING AN EXTERNAL AND INTERNAL DTD SUBSET

This figure shows how to combine an external and an internal DTD subset

Page 17: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

17

WRITING THE DOCUMENT TYPE DECLARATION

This figure shows how to insert an internal DTD subset

Page 18: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

18

DECLARING DOCUMENT ELEMENTS

• Every element used in the document must be declared in the DTD for the document to be valid.

• An element type declaration specifies the name of the element and indicates what kind of content the element can contain.

Page 19: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

19

DECLARING DOCUMENT ELEMENTS

• The element declaration syntax is:

<!ELEMENT element content-model>

element = element name (case sensitive)

content-model = type of content the element contains.

Note that DTD is not an XML language

Page 20: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

20

DECLARING DOCUMENT ELEMENTS

• DTDs define five different types of element content:– Any elements. No restrictions on the element’s content.– Empty elements. The element cannot store any content.

– #PCDATA. The element can only contain parsed character data.

– Elements. The element can only contain child elements.

– Mixed. The element contains both a text string and child elements.

• Examples follow…

Page 21: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

21

TYPES OF ELEMENT CONTENT

• ANY content: The declared element can store any type of content. The syntax is:

<!ELEMENT element ANY>

For example:

<!ELEMENT products ANY>

Is satisfied by any of the following:

<products>SLR 100 digital Comera </products>

<products />

<products>

<name>SLR100 </name>

<type> Digital CAMera </type>

</products>

Page 22: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XPTYPES OF ELEMENT CONTENT

• EMPTY content: This is reserved for elements that store no content. The syntax is:

<!ELEMENT element EMPTY>

For example:

<!ELEMENT img EMPLY>

Is satisfied by following:

<img />

New Perspectives on XML, 2nd EditionTutorial 3

22

Page 23: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

23

TYPES OF ELEMENT CONTENT

• Parsed Character Data content: These elements can only contain parsed character data. The syntax is:

<!ELEMENT element (#PCDATA)

• The keyword #PCDATA stands for “parsed-character data” and is any well-formed text string.

For example:

<!ELEMENT name (#PCDATA>

Is satisfied by the following:

<name> Lea Ziegler <name>

Page 24: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

24

TYPES OF ELEMENT CONTENT

• ELEMENT content.: The syntax for declaring that elements contain only child elements is:

<!ELEMENT element (children)>

• Where children is a list of child elements.

• For example:<!ELEMENT customer (phone)>

is NOT satisfied by the following:

<customer>

<name>Lea Ziegler</name>

<phone>555-2819</phone>

</customer>

Page 25: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

25

TYPES OF ELEMENT CONTENT

• The declaration <!ELEMENT customer (phone)> indicates the customer element can only have one child, named phone. You cannot repeat the same child element more than once with this declaration.

Page 26: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

26

ELEMENT SEQUENCES AND CHOICES

• A sequence is a list of elements that follow a defined order. The syntax is:

<!ELEMENT element (child1, child2, …)>

• The order of the child elements must match the order defined in the element declaration. A sequence can be applied to the same child element.

Page 27: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

27

ELEMENT SEQUENCES AND CHOICES

• Thus,

<!ELEMENT customer (name, phone, email)>

• indicates the customer element should contain three child elements for each customer.

Page 28: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

28

ELEMENT SEQUENCES AND CHOICES

• Choice is the other way to list child elements and presents a set of possible child elements. The syntax is:

<!ELEMENT element (child1 | child2 | …)>

• where child1, child2, etc. are the possible child elements of the parent element.

Page 29: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

29

ELEMENT SEQUENCES AND CHOICES

• For example,

<!ELEMENT customer (name | company)>

• This allows the customer element to contain either the name element or the company element. However, you cannot have both the customer and the name child elements since the choice model allows only one of the child elements.

Page 30: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

30

MODIFYING SYMBOLS

• Modifying symbols are symbols appended to the content model to indicate the number of occurrences of each element. There are three modifying symbols:

– a question mark (?), allow zero or one of the item.

– a plus sign (+), allow one or more of the item.

– an asterisk (*), allow zero or more of the item.

Page 31: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

31

MODIFYING SYMBOLS

• For example, <!ELEMENT customers (customer+)> would allow the document to contain one or more customer elements to be placed within the customers element.

• Modifying symbols can be applied within sequences or choices. They can also modify entire element sequences or choices by placing the character immediately following the closing parenthesis of the sequence or choice.

Page 32: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

32

MIXED CONTENT

• Mixed content elements contain both character data and child elements. The syntax is:

<!ELEMENT element (#PCDATA | child1 | child2 | …)*>

• This form applies the * modifying symbol to a choice of character data or elements. Therefore, the parent element can contain character data or any number of the specified child elements, or it can contain no content at all.

Page 33: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

33

MIXED CONTENT

• Because you cannot constrain the order in which the child elements appear or control the number of occurrences for each element, it is better not to work with mixed content if you want a tightly structured document.

Page 34: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

34

DECLARING ELEMENT ATTRIBUTES

• For a document to be valid, all the attributes associated with elements must also be declared. To enforce attribution properties, you must add an attribute-list declaration to the document’s DTD.

Page 35: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

35

ELEMENT ATTRIBUTES IN KRISTEN’S DOCUMENT

This figure shows element attributes in Kristen's document

Page 36: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

36

DECLARING ELEMENT ATTRIBUTES

• The attribute-list declaration :– Lists the names of all attributes associated with

a specific element– Specifies the data type of the attribute– Indicates whether the attribute is required or

optional– Provides a default value for the attribute, if

necessary

Page 37: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

37

DECLARING ELEMENT ATTRIBUTES

• The syntax to declare a list of attributes is: <!ATTLIST element attribute1 type1 default1

attribute2 type2 default2 attribute3 type3 default3…>

element = name of the element associated with the attributes

attribute = name of an attributetype = attribute’s data typedefault = whether the attribute is required or implied,

and whether it has a fixed or default value.

Page 38: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

38

DECLARING ELEMENT ATTRIBUTES

• Attribute-list declaration can be placed anywhere within the document type declaration, although it is easier if they are located adjacent to the declaration for the element with which they are associated.

Page 39: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

39

WORKING WITH ATTRIBUTE TYPES

• While all attribute types are text strings, you can control the type of text used with the attribute. There are three general categories of attribute values:– CDATA– enumerated– Tokenized

• CDATA types are the simplest form and can contain any character except those reserved by XML.

• Enumerated types are attributes that are limited to a set of possible values.

Page 40: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

• CDATA format:

<!ATTLIST element attribute CDATA default>

Example:

<!ATTLIST item itemPrice CDATA …>

Permits the following in the XML document

<item itemprice=“29.95”> … <item>

New Perspectives on XML, 2nd EditionTutorial 3

40

Page 41: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

41

WORKING WITH ATTRIBUTE TYPES

• Enumerated types are attributes that are limited to a set of possible values:

attribute (value1 | value2 | value3 | …)

• For example:

customer custType (home | business )>

• restricts CustType to either “home” or “business”

Page 42: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

42

WORKING WITH ATTRIBUTE TYPES

• notation (another kind of enumerated attribute)

– It associates the value of the attribute with a <!NOTATION> declaration located elsewhere in the DTD.

– The notation provides information to the XML parser about how to handle non-XML data.

– More about this later

Page 43: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

43

WORKING WITH ATTRIBUTE TYPES

• Tokenized types = text strings that follow certain rules for the format and content. The syntax is:

attribute token

• There are seven tokenized types.

• The ID token is used with attributes that require unique values. For example, if a customer ID needs to be unique, you may use the ID token:

customer custID ID

• This ensures each customer will have a unique ID:

<customer custID=“Cust021”> … </customer>

<customer custID=“Cust022”> … </customer>

Page 44: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XPWORKING WITH ATTRIBUTE TYPES

• IDREF token must have a value equal to the value of an Id attribute located somewhere in the same document

• Like a “foreign key” in relational databases

• General format;

<!ATTLIST element attribute IDREF default>

• Example

<!ATTIST customer forCustomer IDREF …>

• The document must contain an customer element whose ID value matches the value of forCustomer For example

<customer ID=“OR3413”> … <customer>

<order forCustomer = “OR3413”> … </order>

New Perspectives on XML, 2nd EditionTutorial 3

44

Page 45: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XPWORKING WITH ATTRIBUTE TYPES

• NMTOKEN (name token) is used with character data whose value must be valid XML names

• More about this later…

New Perspectives on XML, 2nd EditionTutorial 3

45

Page 46: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

46

ATTRIBUTE TYPES

This figure shows the attribute types

Page 47: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

47

ATTRIBUTE DEFAULTS

• Default has four possible defaults:– #REQUIRED: the attribute must appear with every occurrence of

the element.<!ATTLIST customer custID ID #REQUIRED>

– #IMPLIED: The attribute is optional.<!ATTLIST customer custID ID #IMPLIED>

– An optional default value: A validated XML parser will supply the default value if one is not specified

<!ATTLIST item quantity CDATA “1”>

– #FIXED: The attribute is optional but if one is specified, it must match the default.

<!ATTLIST customer rating CDATA “1” #FIXED>

“Red” indicates correction

“Red” indicates correction

Page 48: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

48

INSERTING ATTRIBUTE-LIST DECLARATIONS

This figure the revised contents of the Orders.xml file

attribute declaration

Page 49: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

49

WORKING WITH ENTITIES

• General entity = entity that references content to be used within an XML document. An entity be refer to:

– a text string

– a DTD

– an element or attribute declaration

– an external file containing character or binary data

• Parsed entity = referenes text that can be interpreted or parsed

• Unparsed entity = references content that can not be parsed, e.g., graphic image

I use an “entity” like a “macro” from some programming languages.

Page 50: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XPIntroducing Entities

• Built in entities:

– &amp; for the & character

– &lt; for the < character

– &gt; for the > character

– &apos; for the ‘ character

– &quot; for the ” charcter

New Perspectives on XML, 2nd EditionTutorial 3

50

Page 51: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

51

UNPARSED ENTITIES

• You need to create an unparsed entity in order to reference binary data such as images or video clips, or character data that is not well formed. The unparsed entity includes instructions for how the unparsed entity should be treated.

• A notation is declared that identifies a resource to handle the unparsed data.

<!NOTATION notation SYSTEM “uri”>

Page 52: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

52

UNPARSED ENTITIES

• For example, to create a notation named “jpeg” that points to an application paint.exe:

<!NOTATION jpeg SYSTEM “paint.exe”>

• Once the notation has been declared, you then declare an unparsed entity that instructs the XML parser to associate the data to the notation.

<!ENTITY entity SYSTEM “uri” NDATA notation>

Page 53: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

53

UNPARSED ENTITIES

• For example, to create an unparsed entity named DCT5ZIMG that references the graphic image file dct5z.jpg:

<!ENTITY DCT5ZIMG SYSTEM “dct5z.jpg” NDATA jpeg>

• Here, the notation is the jpeg notation that points to the paint.exe file. This declaration does not tell the paint.exe application to run the file but simply identifies for the XML parser what resource is able to handle the unparsed data.

Page 54: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

54

GENERAL PARSED ENTITIES

• General entities are declared in the DTD of a document. The syntax is:

<!ENTITY entity “value”>

entity = the name assigned to the entity

value = the general entity’s value.

• For example, an entity named “DCT5Z” can be created to store a product description:

• <!ENTITY DCT5Z (“Topan Digital Camera 5 Mpx - zoom”>

• After an entity is declared, it can be referenced anywhere within the document, for example;

<item>&DCT5Z;</item>

• This is interpreted as

<item>Tapan Digital Camera 5 Mpx - zoom</item>

Entity value

Entity nameEntity value

Entity name

Page 55: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

55

PARAMETER ENTITIES

• Parameter entities are used to store the content of a DTD.• For internal parameter entities, the syntax is:

<!ENTITY % entity “value”>entity = the name of the parameter entity value= a text string of the entity’s value.

• For external parameter entities, the syntax is: <!ENTITY % entity SYSTEM “uri”>

• uri = location of the external file containing DTD content.

Page 56: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

56

PARAMETER ENTITIES

• Parameter entity references can only be placed where a declaration would normally occur, in

– Internal DTD

– External DTD

• An external parameter entity can allow XML to use more than one DTD per document by combining declarations from multiple DTDs.

Page 57: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XP

New Perspectives on XML, 2nd EditionTutorial 3

57

USING PARAMETER ENTITIES TO COMBINE MULTIPLE DTDS

This figure shows how to combine multiple DTDs using parameter entities

Page 58: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XPVALIDATING STANDARD VOCABULARIES

• Most popular XML vocabularies have existing DTDs associated with them

• To validate a document, you must access an external DTD located on a Web serer

• See Figure 3-27 on page XML130 for examples

• (You can find most of these on the W3C web page)

New Perspectives on XML, 2nd EditionTutorial 3

58

Page 59: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XPValidating XHTML 1.0

<?xml version=“1.0” encoding = “UTF-8” standalong=“no” ?>

<!DOCTPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”

http://www.w3.orgTR/xhtml1/DTD/xhtml1-strict.dtd>/

• <html>

• …

• </html>

New Perspectives on XML, 2nd EditionTutorial 3

59

Page 60: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XPValidate an XML file with a DTD

• XML Spy

– http://www.altova.com/download-xml-validator.html?gclid=CNTaw52c3LYCFaU5Qgod0RgApw

• W3C schools

– http://www.w3schools.com/xml/xml_validator.asp

New Perspectives on XML, 2nd EditionTutorial 3

60

Page 61: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XPIn class exercise

<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE recipe [

<!ELEMENT recipe (title, ingredient+, preparation+)>

<!ELEMENT title (#PCDATA)>

<!ELEMENT ingredient (#PCDATA)>

<!ELEMENT preparation (#PCDATA)>

]>

<recipe>

<title>Peanut Butter Sandwich</title>

<ingredient>1 teaspoon peanut butter </ingredient>

<ingredient>1 teaspoon jelly</ingredient>

<ingredient>2 slices bread </ingredient>

<preparation>Step 1: Spread peanut butter on one slice of bread </preparation>

<preparation>Step 2: Spread jelly on the other slice of bread </preparation>

<preparation>Step 3: Place slices of bread together with peanut butter and jelly in the middle </preparation>

</recipe>

New Perspectives on XML, 2nd EditionTutorial 3

61

Insert new element.Insert new attribute.Change cardinality.…and validate

Page 62: XP New Perspectives on XML, 2nd Edition Tutorial 3 1 TUTORIAL 3 VALIDATING AN XML DOCUMENT

XPTutorial 3 Case Problem 1

• The XML file may have errors.

• Use a validator to verify that edltxt.xml is well-formed.

• Make the declarations in the internal DTD

• Use a validator to verify that edltxt.xml is valid

• Add a reference to a CSS that you construct

• Post the results to your web site. Remember to add your name to the upper left hand cornor.

• Send an e-mail to [email protected] with the following subject heading: Tutorial 3 Case Problem 1 by <your name> before 11:59 pm Wednesday May 8

New Perspectives on XML, 2nd EditionTutorial 3

62