xml

36
XML

Upload: joylyn

Post on 23-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

XML. XML- Some Links. XML Tutorials – Some Links http://www.zvon.org/index.php?nav_id=tutorials&mime=html http://www.w3schools.com/ http://java.sun.com/xml/tutorial_intro.html. Introduction. XML: Extensible Markup Language Defined by the WWW Consortium (W3C) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: XML

XML

Page 2: XML

2

XML- Some Links

• XML Tutorials – Some Links• http://www.zvon.org/index.php?

nav_id=tutorials&mime=html• http://www.w3schools.com/• http://java.sun.com/xml/tutorial_intro.html

Page 3: XML

Introduction• XML: Extensible Markup Language• Defined by the WWW Consortium (W3C)• Originally intended as a document markup language

but not a database language– Derived from SGML (Standard Generalized

Markup Language), but simpler to use than SGML

– Extensible, unlike HTML• Users can add new tags, and separately specify

how the tag should be handled for display

Page 4: XML

4

XML: Application Areas

• XML can be effectively used in the following areas:• Use XML to describe meta-content regarding

documents or on-line resources.– Meta-content is information about a document’s

content – title, author, file size, creation date, keywords and so on.

– Meta-content can be used for example, for searching, information filtering and document management.

– A meta-content description that is external to a file (and not inside an HTML file), improves search performance.

Page 5: XML

5

XML: Application Areas

• Use XML to publish and exchange database contents– Many applications extract data from backend

database systems, transform the results using the <table> tag of HTML and display it on the screen.

– If data is delivered as an XML document that preserves the original information, such as column names and data types, the information can be used by the client for purposes other than displaying it on the screen.

Page 6: XML

6

XML: Application Areas• Use XML as a messaging format for communication

between application programs.– Messaging is an exchange of messages between

organizations or between application systems within an organization.

– Traditionally, messaging among companies has traditionally been done by Electronic Data Exchange (EDI). But many small companies cannot participate in EDI because of the cost of building and operating an EDI system. The other problem is agreeing on a standard message format to use. Among the skills needed to build an EDI system is a good understanding of the X12 or EDIFACT message format.

– It is easier to learn XML syntax for messaging.

Page 7: XML

7

• Data interchange is critical in today’s networked world– Examples:

• Banking: funds transfer• Order processing (especially inter-company orders)• Scientific data

– Chemistry: ChemML, …– Genetics: BSML (Bio-Sequence Markup Language), …

– Paper flow of information between organizations is being replaced by electronic flow of information

• Each application area has its own set of standards for representing information

• XML has become the basis for all new generation data interchange formats

Page 8: XML

8

Why Use XML?• Simplicity

– XML’s character-based format is simpler and human-readable than binary formats.

– XML messages can be easily created, read and modified using common text editors.

– XML’s ability to represent tree-structured data• Richness Of Data structures

– Can express complex data structures using a rooted tree structure. For many applications, a tree structure is general and powerful enough to express fairly complex data.

• International Character Handling– XML’s ability to handle international character set is

especially handy for Web applications and business transactions that may cross national boundaries.

– XML 1.0 Recommendation is defined based on the ISO-10646 (Unicode) character set.

Page 9: XML

XML

• The ability to specify new tags, and to create nested tag structures made XML a great way to exchange data, not just documents.

– Much of the use of XML has been in data exchange applications, not as a replacement for HTML

Page 10: XML

10

ExampleTags make data (relatively) self-documenting

Example:

<?xml version="1.0"?><!-- Books.xml --><books><book> <title>Cosmos</title>

<author>Carl Sagan </author><isbn> 1234 </isbn>

<price>70.00</price> </book><book>

<title>War And Peace</title><author>Leo Tolstoy</author><isbn> 4222</isbn>

<price>50.00</price> </book><book> </book>

</books>

Page 11: XML

11

Structure Of an XML Document

• Tag: label for a section of data• Element: section of data beginning with <tagname> and

ending with matching </tagname>• Elements must be properly nested

– Proper nesting• <Order> … <LineItem> …. </LineItem> </Order>

– Improper nesting • <Order> … <LineItem> …. </Order> </LineItem>

– Formally: every start tag must have a unique matching end tag, that is in the context of the same parent element.

• Every document must have a single top-level element

Page 12: XML

12

Nesting Of Data• Nesting of data is useful in data transfer

– Example: elements representing customer-id, customer name, and address nested within an order element

• Nesting is not supported, or discouraged, in relational databases– With multiple orders, customer name and address are

stored redundantly– normalization replaces nested structures in each order by

foreign key into table storing customer name and address information

– Nesting is supported in object-relational databases• But nesting is appropriate when transferring data

– External application does not have direct access to data referenced by a foreign key

Page 13: XML

13

XML For Text and XML For Data

• XML For Text (Document-centric): is used for marking up documents. The order of the information in the text is important to understand its meaning – Cannot reorder the segments of text.

• XML For Data (Data-centric): Data can be represented in a number of different ways and still have the same meaning.

Page 14: XML

14

Creating XML Documents• Structure Of XML Documents

• Well-Formed XML Documents

– Obey the rules defined in the XML standard.

– XML elements must have a closing tag

– If there is no element content (usually is the case when the element has an attribute) an abbreviation can be used.

– <Item Type=“Book”/>

– Items must nest completely (cannot be partially nested)

– XML Documents can have only one root element.

Page 15: XML

15

Creating XML Documents• Structure Of XML Documents• Valid XML Documents:• A valid XML document obeys the rules defined in

the Document Type Definition (DTD).• The structure of an XML document can be pre-

defined with a DTD.• An application can then compare the definition of a

DTD with an actual XML document and check whether the document conforms to the rules defined in the DTD. If so, it is a valid XML document.

Page 16: XML

16

An XML-based System

XML Document

XML DTD

XML Processor XML Application

Page 17: XML

17

Some XML Processors

• msxml• XP• nsgmls• saxon• Well-formed XML Documents:

– An XML processor can build a tree structure.• Valid XML Documents

– Obey the rules specified in the DTD– msxml parser is a validating processor.

Page 18: XML

18

XML Document Type Definitions (DTD)

• A DTD defines the structure of an XML document.• The type of an XML document can be specified using

a DTD• DTD constrains structure of XML data

– What elements can occur– What attributes can/must an element have– What sub-elements can/must occur inside each

element, and how many times.• DTD does not constrain data types

– All values represented as strings in XML

Page 19: XML

19

XML Document Type Definitions (DTD)

• A DTD defines the structure of an XML document.• Internal DTD: A DTD can be placed inside an XML

document, with a declaration shown below:<!DOCTYPE document_name [ document_type_definition ]>

• External DTD: A DTD can be stored in an external file and the location of the file is given in the declaration:

<!DOCTYPE document_name SYSTEM “url_of_dtd”>

Page 20: XML

20

Anatomy Of an XML Document And Its Definition in DTD

• Structuring Elements: Elements are the items that make up the structure of the document.

• Elements can contain either data content or a mixture of data content and other elements.

Page 21: XML

21

• Element-only Content: Elements may only contain other elements. This is the way elements are nested within one another.

• Example:A DTD entry:<!ELEMENT BookOrder (Customer, Order_LineItem+)>An XML entry:<BookOrder>

<Customer /><Order_LineItem /><Order_LineItem />

</BookOrder>A plus (+) means an element can occur one or more times

Page 22: XML

22

• Mixed Content: Elements may contain zero or more instances of a list of elements, along with any amount of text in any position.

• Example:A DTD entry:<!ELEMENT BookOrder (#PCDATA | Customer,

Order_LineItem)*>An XML entry:<BookOrder>

This is a Book Order For <Customer> Mary Jones </Customer>

</BookOrder>A plus (*) means an element can occur zero or more timesA vertical bar (|) is used to separate elements in a list where any one of the listed

elements can appear.

Page 23: XML

23

• Text-OnlyContent: Elements may contain text strings.

• Example:A DTD entry:

<!ELEMENT Customer (#PCDATA )>

An XML entry:

<Customer> Mary Jones </Customer>

#PCData (parsed character data) indicates that element’ contents contain text characters and cannot contain any characters that are part of the mark up, such as delimiter <, >, &.

Page 24: XML

24

• Empty Content: Elements cannot contain anything at all. Any data can be expressed as attributes.

• Example:A DTD entry:

<!ELEMENT Customer EMPTY>

An XML entry:

<Customer />

Page 25: XML

25

• ANY Content: Elements may contain any type of content - element or text.

• Example:A DTD entry:

<!ELEMENT Customer ANY>

Two XML entries:

<Customer> Mary Jones </Customer>

Or

<Customer>

<Customer> Mary Jones </Customer>

</Customer>

Page 26: XML

26

Attributes

• Elements can have attributes. This is another way of representing data points is to use attributes attached to elements.

• An element can be broken down into smaller chunks of data (Example: name into firstName and lastName) and the data is attached to the element itself.

• Attributes are specified by name=value pairs inside the starting tag of an element

• An element may have several attributes, but each attribute name can only occur once.

• Example: • <Order order_type=“purchase” priority=“1”>

Page 27: XML

27

Using Attributes

• Attributes represent information about an element (meta information) and different from the content.

• Attributes are often used with empty elementsSuggestion: use attributes for identifiers of elements, and use

elements for contents

Note: More discussion on elements and attributes later.

Page 28: XML

28

Attributes

• Attribute specification : for each attribute – Name– Type of attribute

• CDATA• ID (identifier) or IDREF (ID reference) or

IDREFS (multiple IDREFs)

Page 29: XML

29

Attributes

• An element can have at most one attribute of type ID• The ID attribute value of each element in an XML document

must be distinct

– Thus the ID attribute value is an object identifier• An attribute of type IDREF must contain the ID value of an

element in the same document• An attribute of type IDREFS contains a set of (0 or more) ID

values. Each ID value must contain the ID value of an element in the same document

Page 30: XML

30

Using Attributes• Example:A DTD entry:

<!ELEMENT Customer EMPTY>

<!ATTLIST Customer ------ element name in which the attributes appear

FirstName CDATA #REQUIRED ------ the attribute must contain some value

LastName CDATA #REQUIRED>

An XML entry:

<Customer

FirstName=“Mary”

LastName=“Jones” />

Page 31: XML

31

Using Attributes

CDATA section contains content that is treated as plain text. No parsing actions are performed on the content and can also contain the special characters that are not permitted in #PCDATA.

• #REQUIRED: Attribute must appear in the element.

• #FIXED “default”: Attribute value is a constant and must

appear as such in an XML document.• #IMPLIED: If the attribute does not appear in the element, then

an application using the XML document may supply any value.

Page 32: XML

32

Elements vs Attributes• Representing BookOrder using Elements• BookOrder1.dtd<!ELEMENT BookOrder

(CustomerFirstName,CustomerLastName,CustomerCity,CustomerZip OrderLineItem+)>

<!ELEMENT CustomerFirstName (#PCDATA)>

<!ELEMENT CustomerLastName (#PCDATA)>

<!ELEMENT CustomerCity (#PCDATA)>

<!ELEMENT CustomerZip (#PCDATA)>

<!ELEMENT OrderLineItem (itemNo,quantity,price)>

<!ELEMENT itemNo (#PCDATA)>

<!ELEMENT quantity (#PCDATA)>

<!ELEMENT price (#PCDATA)>

Page 33: XML

33

BookOrders.xmlbased on BookOrder1.dtd

<?xml version=“1.0” ?><DOCTYPE BookOrder SYSTEM “url/BookOrder1.dtd”><BookOrder>

<CustomerFirstName> Mary </CustomerFirstName> <CustomerLastName> Jones </CustomerLastName> <CustomerCity> San Jose </CustomerCity> <CustomerZip> 95053 </CustomerZip> <OrderLineItem>

<itemNo> 1234 </itemNo> <quantity> 10</quantity><price>100.00</price>

</OrderLineItem><OrderLineItem>

<itemNo> 1444 </itemNo> <quantity> 5</quantity><price>50.00</price>

</OrderLineItem></BookOrder>

Page 34: XML

34

Elements Vs Attributes

• BookOrder2.dtd using attributes<!ELEMENT BookOrder (OrderLineItem+)>

<!ATTLIST BookOrder

CustomerFirstName CDATA #REQUIRED

CustomerLastName CDATA #REQUIRED

CustomerCity CDATA #REQUIRED

CustomerZip CDATA #REQUIRED >

<!ELEMENT OrderLineItem EMPTY >

<!ATTLIST OrderLineItem

itemNo CDATA #REQUIRED

quantity CDATA #REQUIRED

price CDATA #REQUIRED >

Page 35: XML

35

BookOrders.xmlbased on BookOrder2.dtd

<?xml version=“1.0” ?><DOCTYPE BookOrder SYSTEM “url/BookOrder2.dtd”>

<BookOrderCustomerFirstName=“Mary”CustomerLastName=“Jones” CustomerCity=“San Jose” CustomerZip=“95053” > <OrderLineItem

itemNo=“1234” quantity=”10”price=“100.00”/>

<OrderLineItem>itemNo=“1444” quantity=”5”price=“50.00”/>

</BookOrder>

Page 36: XML

36

Limitations of DTDs

• No typing of text elements and attributes– All values are strings, no integers, reals, etc.

• Difficult to specify unordered sets of subelements– Order is usually irrelevant in databases– (A | B)* allows specification of an unordered set, but

• Cannot ensure that each of A and B occurs only once• IDs and IDREFs are untyped

– The owners attribute of an account may contain a reference to another account, which is meaningless

• owners attribute should ideally be constrained to refer to customer elements