Presentation is loading. Please wait.

Presentation is loading. Please wait.

McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Understanding How XML Works Ellen Pearlman Eileen Mullin Programming the.

Similar presentations


Presentation on theme: "McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Understanding How XML Works Ellen Pearlman Eileen Mullin Programming the."— Presentation transcript:

1 McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Understanding How XML Works Ellen Pearlman Eileen Mullin Programming the Web Using XML

2 3-2 Learning Objectives 1.Understanding the overall structure of an XML document 2.Familiarity with basic EBNF Characters 3.Knowing the difference between well formed and valid XML 4.Making a simple XML file 5.Looking at a simple DTD 6.Looking at elements, attributes, entities, and comments 7.Deciding between elements and attributes

3 3-3 Steps to a Basic XML Document Write the statement in an editor such as Notepad for PC Users or for Mac users, BBEdit. Save the document with a “.xml” extension. Have the.xml document read by a XML compliant browser. Display the.xml document to the user.

4 3-4 Information Sent Out to Different Devices

5 3-5 Basic Markup: EBNF Characters EBNF stands for Extended Backus-Naur Form Notation and is a syntaxic metalanguage that defines all the components of an XML document. < is the beginning of a tag > is the end of a tag / shows that it is an end tag starts and ends a processing statement (PI) ! alerts that a reserved word keyword will follow [ ] Shows the start and end of a range.

6 3-6 Basic Markup: EBNF Characters (2) [ shows the beginning of an internal DTD ]> Shows the end of an internal DTD Shows the start and end of a CDATA section | is a choice and allows any order of entry, shows a specific order of entry # used in front of PCDATA and to determine default attributes & starts a parsed general entity ; ends an entity ? Shows a content piece can happen up to once + Shows a content piece must happen once or more * Shows a content piece can happen for an unlimited number of times

7 3-7 Well-Formed XML Documents An XML document is considered well-formed in its syntax when: –Each opening tag has a corresponding closing tag and tags must nest correctly –All cases match. –There must be at least one element –There can only be one root element and all other elements must be contained within it –Elements and their associated tags must nest correctly

8 3-8 Rules for Element Names Element names must follow some simple rules: –They start with either a letter or an underscore –They are allowed to have letters, digits, periods (.), underscores (_) or hyphens (-) –No whitespace –They cannot begin with anything that says "xml" either with or without quotes and must adhere to all XML naming conventions

9 3-9 A Well-formed XML Document That Uses a Schema

10 3-10 Valid XML Documents A valid XML document must, in addition to being well-formed, conform to rules defined in its DTD (Document Type Definition) or XML Schema. A DTD ensures that an XML document meets these standards by defining the grammar and vocabulary of the markup language. The DTD lists everything that a parser needs to know in order to display and process a XML document, as does the XML Schema.

11 3-11 A Valid XML File

12 3-12 Tagging a XML Document Every XML document must begin with a processing instruction (PI): Without this PI, the software programs and processors are not aware that they are dealing with an XML document. It is called the prologue and contains the version number but can also have other information such what character set it is using.

13 3-13 First XML Example <!DOCTYPE veryfirst [ ]> This is my very first XML document

14 3-14 VeryFirstxml in XML Spy

15 3-15 Character References Character references are used to insert special characters into an XML document. Character references come from the ISO/IEC character set, an international numbering system that references characters from all languages. They are used in case you want to actually insert a specific character into your code, and that character is reserved for use by the processor.

16 3-16 Character Reference Lookup

17 3-17 Understanding The Tree Structure Of A Document The descriptions are more or less on the same hierarchical level, in terms of a tree structure. Think of a tree as your family. There are great-grandparents, grandparents, parents and children. Therefore, parents have one level above and one level below. The root of this is Company, because without the company, there would be no product.

18 3-18 Sample PastaPrimavera DTD <!DOCTYPE Company [ ]> North America Northeast Spaghetti Sauce Alfredo Glass 12 oz. Pepper

19 3-19 PastaPrimavera shown in IE

20 3-20 PastaPrimavera in XML Spy

21 3-21 Creating A Root Element Every XML document must have one root element. The root element contains all the other elements, and attributes inside of it. The root element is the most important in the document, and usually takes a name that describes its function.

22 3-22 Comments Insert comments when you want to write notes about either the code to guide you through revisions or to leave instructions for others. Although comments are contained within an XML statement, they do nothing in terms of its programming instructions.

23 3-23 I Like Spaghetti Comment

24 3-24 Elements An element is the basic building block of an XML document. The best way to think of elements is that they are like the nouns of an XML statement. The declaration begins with an opening bracket (<) and exclamation point (!) followed by the keyword ELEMENT. An element name must start with a letter or underscore (_) character.

25 3-25 Empty Element Tags An empty element is a placeholder for content. It tells the parser that later on something is going to fill in the space. Why use an empty element? One example might be if you were designing a logo for a company, and it wasn’t ready yet. Empty elements are also used for line breaks and can be anywhere from a single character up to a large amount of XML.

26 3-26 #PCDATA Parsed Character Data (#PCDATA) is data that has text. That text is a string of characters that either contains markup, content or both. Parsed Character Data is sent through an XML processor and unparsed information involves itself with data that refers to an application other than XML. <!DOCTYPE myfirstpcdata [ ]> This is my first xml document

27 3-27 CDATA CDATA is very important for putting large blocks of text into a special section that the XML processor looks at only as pure text. This is because the character reference for the "<" symbol is "&#60"; and other character symbols are equally as cumbersome. It would be extremely odd to have hundreds of lines of text looking like "<. A CDATA statement looks like this. <![CDATA[ Text Information (for example "<") ]]>

28 3-28 Attributes (#!ATTLIST) Attributes give more information about an element and reside inside of the element's start tag, listed underneath the element with which it is affiliated. Attributes can further define the behavior of an element and can also allow it to have extended links through giving it an identifier. Attributes follow the same naming rules as elements and each attribute has a name and a value.

29 3-29 Attributes (2) Attributes break into basic value types; Tokenized, Enumerated, CDATA and Default. –Tokenized breaks down into a simple unit, for example a specific name. It imposes constraints on the values of the attribute. –Enumerated consists of one or more notations in which a choice of valid values is provided. –CDATA means Character Data; the value is not constrained.

30 3-30 Syntax for an Attribute The basic syntax for an attribute is; And in an XML document it would look like: Gift Box

31 3-31 Entities Entities are the basic storage units of any XML document. The actual data they store may be derived from a variety of sources, but the sources themselves are not entities. An entity can be declared once, but uses countless times and does not change the semantic markup of the actual document. Its real purpose is to streamline the code. This is the home of &PPV

32 3-32 How To Decide: Attribute vs. !ELEMENT The best way to decide in choosing an attribute instead of an element is to figure out their basic functions. –Actual data about anything is usually an element. –Anything that describes the data itself is probably an attribute. If the actual information needs to be displayed and read, then an element is best using child elements because it is more extensible. Attributes work best with identifiers or certain types of formatting.

33 3-33 The End


Download ppt "McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Understanding How XML Works Ellen Pearlman Eileen Mullin Programming the."

Similar presentations


Ads by Google