1 / 83

XML

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)

yael
Télécharger la présentation

XML

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

  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

  3. 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

  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.

  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.

  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.

  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

  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.

  9. 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

  10. Example Tags 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>

  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

  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

  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.

  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.

  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.

  16. An XML-based System XML Document XML Application XML Processor XML DTD

  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.

  18. Some XML Parsers and Validators • XML Validators: • A Java-based DTD and Schema Validator: • http://wwws.sun.com/software/xml/developers/multischema/ • To validate an XML file (example.xml) against its DTD (example.dtd): • java -jar C:\pathWhereYouInstalledValidator\msv-20020414\msv.jar example.dtd example.xml • To validate an XML file (example.xml) against its schema (example.xsd): • java -jar C:\pathWhereYouInstalledValidator\msv-20020414\msv.jar example.xsd example.xml • XML Cooktop: http://www.xmlcooktop.com/ • XML Spy: http://www.xmlspy.com/

  19. 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

  20. 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”>

  21. 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.

  22. 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

  23. 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 times A vertical bar (|) is used to separate elements in a list where any one of the listed elements can appear.

  24. 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 <, >, &.

  25. 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 />

  26. 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>

  27. Attributes • Elements can have attributes. This isanother 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”>

  28. Using Attributes • Attributes represent information about an element (meta information) and different from the content. • Attributes are often used with empty elements Suggestion: use attributes for identifiers of elements, and use elements for contents Note: More discussion on elements and attributes later.

  29. Attributes • Attribute specification : for each attribute • Name • Type of attribute • CDATA • ID (identifier) or IDREF (ID reference) or IDREFS (multiple IDREFs)

  30. 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

  31. 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” />

  32. 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.

  33. 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)>

  34. 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>

  35. 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 >

  36. BookOrders.xmlbased on BookOrder2.dtd <?xml version=“1.0” ?> <DOCTYPE BookOrder SYSTEM “url/BookOrder2.dtd”> <BookOrder CustomerFirstName=“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>

  37. 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

  38. Example1.dtd <!ELEMENT skills (skill)*> <!ELEMENT skill (name)> <!ATTLIST skill level CDATA #REQUIRED> <!ELEMENT name ( #PCDATA )> Example1.xml <?xml version = "1.0"?> <!-- Example1.xml --> <!DOCTYPE skills SYSTEM "example1.dtd"> <skills> <skill level="1"> <name > XML How to write DTD </name> </skill> </skills> Writing DTDs

  39. Validating Sun Validator • Invoking the Validator: • java -jar C:\pathName\msv.jar Example1.dtd Example2.xml Output: start parsing a grammar. validating Example1.xml the document is valid.

  40. ID Attribute Type • Attributes using the ID type serve as unique identifiers for a given instance of an element. • The value of an ID attribute must be a valid XML name, unique within a document and use the #IMPLIED or #REQUIRED default value. • #IMPLIED: No default value. The attribute is optional. • #REQUIRED: The attribute must appear in every element type. • #FIXED: Attributes may be optional, but when present are constrained to the given value. • There may only be one ID attribute for one element type.

  41. <!ELEMENT PurchaseOrders (Order)*> <!ELEMENT Order (Item+)> <!ATTLIST Order OrderId ID #REQUIRED > <!ELEMENT Item (ItemName,price)> <!ATTLIST Item ItemId ID #REQUIRED > <!ELEMENT ItemName (#PCDATA)> <!ELEMENT price (#PCDATA)> <?xml version = "1.0"?> <!-- Example3.xml --> <!DOCTYPE PurchaseOrder SYSTEM "example3.dtd"> <PurchaseOrders> <Order OrderId="od123"> <Item ItemId="I1"> <ItemName> Item1 </ItemName> <price> 20.00 </price> </Item> <Item ItemId="I2"> <ItemName> Item2 </ItemName> <price> 20.00 </price> </Item> </Order> <Order OrderId="od456"> <Item ItemId="I3"> <ItemName> Item3 </ItemName> <price> 20.00 </price> </Item> <Item ItemId="I4"> <ItemName> Item4 </ItemName> <price> 20.00 </price> </Item> </Order> </PurchaseOrders> Example

  42. <!ELEMENT PurchaseOrders (Order,Manufacturer)*> <!ELEMENT Order (Item+)> <!ATTLIST Order OrderId ID #REQUIRED > <!ELEMENT Item (ItemName)> <!ATTLIST Item ItemId ID #REQUIRED Manf IDREF #REQUIRED > <!ELEMENT ItemName (#PCDATA)> <!ELEMENT Manufacturer EMPTY> <!ATTLIST Manufacturer ManfId ID #REQUIRED > <?xml version = "1.0"?> <!– Example4.xml --> <!DOCTYPE PurchaseOrders SYSTEM "example4.dtd"> <PurchaseOrders> <Order OrderId="od123"> <Item ItemId="I1" Manf = "m444"> <ItemName> Item1 </ItemName> </Item> </Order> <Manufacturer ManfId = "m444"/> </PurchaseOrders> Example

  43. XML Schemas

  44. Useful Links • Schema tutorial links: • http://www.w3schools.com/schema/default.asp • http://www.xfront.com/ • http://www.w3.org/TR/xmlschema-0/

  45. Describing Metadata using Schemas • XML Schema Strategy - two documents are used : • a schema document specifies the properties (metadata) for a class of resources (objects); • each instance document provides specific values for the properties.

  46. XML Schema: Specifies the Properties for a Class of Resources <xsd:complexType name="Book"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:year"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> "For the class of Book resources, we identify five properties - Title, Author, Date, ISBN, and Publisher"

  47. XML Instance Document: Specifies Values for the Properties <Book> <Title>Cosmos</Title> <Author>Sagan</Author> <Date>1977</Date> <ISBN>0-111-34319-4</ISBN> <Publisher>Star Publishers Co.</Publisher> </Book> An XML document with a specific instance of a Book

  48. Why Schemas • Enhanced datatypes • 44+ versus 10 • Can create your own datatypes • Example: "This is a new type based on the string type and elements of this type must follow this pattern: ddd-dddd, where 'd' represents a digit". • Written in the same syntax as instance documents: less syntax to remember • Can extend or restrict a type (derive new type definitions on the basis of old ones) • Can express sets, i.e., can define the child elements to occur in any order • Can specify element content as being unique (keys on content) and uniqueness within a region • Can define multiple elements with the same name but different content • Can define substitutable elements - e.g., the "Book" element is substitutable for the "Publication" element.

  49. Schemas and DTDs • One difference between XML Schemas and DTDs is that the XML Schema vocabulary is associated with a name (namespace). Likewise, the new vocabulary that you define must be associated with a name (namespace). • With DTDs neither set of vocabulary is associated with a name (namespace).

  50. XML Schema Vocabulary • Schema element: All XML schemas have schema as the root element. • XML Schema namespace: The elements and datatypes that are used to construct schemas, like, schema, element, complexType, sequence and string come from the http://…/XMLSchema namespace

More Related