Download presentation
Presentation is loading. Please wait.
Published byDamian McLaughlin Modified over 8 years ago
1
10.1 Chapter 10: XML Sections 10.1-10.3.1 Problems 10.1, 10.2, 10.7 Find an example of using XML in a field of interest to you and describe it to the class. Define your own DTD for a PowerPoint presentation why would you do this? has it been done before?
2
10.2 XML (Extensible Markup Language) Defined by the WWW Consortium (W3C) Derived from SGML (Standard Generalized Markup Language) Goal: replace HTML for publishing documents on the Web Originally intended as a document markup language, not for databases Discovery: XML is great for exchanging data, not just documents Tags make data (relatively) self-documenting. E.g. what's this: A-101 Downtown 500 A-101 Johnson
3
10.3Motivation Suppose a bank wants to send a transaction to another bank Importance of data interchange in a networked world Each application area has its own standards for representing info. Examples: banking: funds transfer order processing (especially inter-company orders) scientific data Chemistry: ChemML, … Genetics: BSML (Bio-Sequence Markup Language), … Earlier formats based on plain text with line headers indicating the meaning of fields similar in concept to email headers does not support nesting tied too closely to low level document structure (lines, spaces, etc)
4
10.4Mechanics Each XML based standard defines valid elements, using XML type specification languages to specify the syntax DTD (Document Type Descriptors) XML Schema plus textual descriptions of the semantics New tags can be defined as required Many tools for parsing, browsing and querying XML documents/data Structure Tag: label for a section of data Element: section of data beginning with and ending with matching Elements must be properly nested proper: … ….... Improper: … …. Every document must have a single top-level element
5
10.5 Nested Elements Hayes Main Harrison A-102 Perryridge 400 …..
6
10.6 Motivation for Nesting Nested data is common in modeling relational databases example: elements representing customer-id, customer name, and address contained within an order element with multiple orders, customer name and address are stored redundantly Normalization often replaces nested structures, storing customer name and address information separately But nesting is often appropriate when transferring data since the external application may not have direct access to related data Perryridge an account another account another one
7
10.7 Structure of XML Data Elements can have attributes A-102 Perryridge 400 Distinction between subelement and attribute Attributes are part of markup, while subelement contents are part of the document contents Same information can be represented in two ways – …. – A-101 …
8
10.8Namespaces XML data has to be exchanged between organizations Same tags may have different meanings in different organizations Specifying a unique string as an element name avoids confusion Avoid using long unique names with XML Namespaces …http://www.FirstBank.com Downtown Brooklyn … (Xmins:FB... means FB is an abbreviation for http://www.FirstBank.com)http://www.FirstBank.com
9
10.9 XML Document Schema Database schemas constrain what information can be stored, and the data types of stored values XML schemas are very important for XML data exchange to allow a site to interpret data received from another site Two mechanisms for specifying XML schema Document Type Definition (DTD) what elements can occur what attributes can/must an element have what subelements can/must occur inside each element, and how many times XML Schema newer, increasing in use
10
10.10 Element Specification in DTD DTD syntax DTD does not constrain data types: all values are strings Subelements can be specified as names of elements, or #PCDATA (parsed character data), i.e., character strings EMPTY (no subelements) or ANY (anything can be a subelement) Example
11
10.11 More on Subelement Specification Subelement specification may use regular expressions Notation: – “|” - alternatives – “+” - 1 or more occurrences – “*” - 0 or more occurrences rfp.dtdrfp.dtd (request for promotion into a CM* system) <!DOCTYPE rfp [ <!ELEMENT promotion (req_nbr, is_done?, cm_tag+, src+, obsolete*, dev_comment, test_comment)> <!-- A promotion is a set of working files to be checked into RCS files, and a set of CM tags to be given to the checked-in versions. --> * Configuration Management
12
10.12 Bank DTD <<!DOCTYPE bank [ ]> A-401 Downtown 500... more accounts Mary Erin Newark...more customers...depositors
13
10.13 Attributes in DTD For each attribute name type CDATA (characters) ID (a unique identifier) IDREF (the ID value of an element in the same document) IDREFS (0 or more IDREFs) Examples * <!ATTLIST customer customer-id ID # REQUIRED accounts IDREFS # REQUIRED > *checking is the default
14
10.14 Bank DTD with ID and IDREF Attribute Types <!DOCTYPE bank-2[ <!ATTLIST account account-number ID # REQUIRED owners IDREFS # REQUIRED> <!ATTLIST customer customer-id ID # REQUIRED accounts IDREFS # REQUIRED> … declarations for branch_name, balance, customer-name, customer-street... ]> Downtown 500... more accounts Mary Erin Newark...more customers
15
10.15 bank-2 Downtown 500 Joe Monroe Madison Mary Erin Newark
16
10.16 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 (unlike in the document- layout environment from which XML evolved) (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
17
10.17 XML Schema (skip) XML schema: a more sophisticated schema language Supports typing values e.g. integer, string, etc constraints on min/max values user defined types is itself specified in XML syntax, unlike DTDs more standard representation, but verbose many more features list types, uniqueness and foreign key constraints, inheritance.. But: significantly more complicated than DTDs not yet widely used
18
10.18 XML Schema Version of Bank DTD (skip) http://www.w3.org/2001/XMLSchema ….. definitions of customer and depositor ….
19
10.19Examples MusicXML: http://www.recordare.com/xml/helloworld.htmlhttp://www.recordare.com/xml/helloworld.html RSS feeds: http://www.ibm.com/developerworks/library/w-rss.htmlhttp://www.ibm.com/developerworks/library/w-rss.html Meat and Poultry http://www.mpxml.org/about/:http://www.mpxml.org/about/
20
10.20 10.4 Querying and Transforming XML Data Tools for querying XML data translating information from anXML schema to another XML or other format XPath a simple query language consisting of path expressions XQuery an XML query language with a rich erset of features XSLT a simple language translating from XML to XML and XML to HTML
21
10.21XPath Used to address (select) parts of documents with path expressions Path expression: a sequence of steps separated by “/” like file names in a directory hierarchy Result of path expression: set of values that along with their containing elements/attributes match the specified path /bank-2/customer/customer_name evaluated on the bank-2 data in slide 15 returnsbank-2 data Joe Mary /bank-2/customer/customer_name/text( ) returns the same names without the enclosing tags “/” denotes root of the XML document (above the top-level tag) Query and transformations are based on a tree model of XML data
22
10.22XPath Selection predicates in [ ] /bank-2/account[balance > 400] returns account elements with a balance value greater than 400 /bank-2/account[balance] returns account elements containing a balance subelement Attributes are accessed using “@” /bank-2/account[balance > 400]/@account_number returns the account numbers of accounts with balance > 400 count() at the end of a path counts the number of elements in the set generated by the path /bank-2/account[count(./customer) > 2] returns accounts with > 2 customers
23
10.23 More XPath Features “|” used for union /bank-2/account/id(@owner) | /bank-2/loan/id(@borrower) gives customers with either accounts or loans “//” can be used to skip multiple levels of nodes a short from for specifying “all descendants” /bank-2//customer_name finds any customer_name element anywhere under the /bank-2 element, regardless of the element in which it is contained. “..” specifies the parent
24
10.24XQuery A general purpose query language for XML data Currently being standardized by W3C Uses flowr (for … let … where … order by …result … ) syntax for SQL from //uses XPath expressions where SQL where order by SQL order by return SQL select let allows temporary variables, and has no equivalent in SQL E.g. to return customers with negative balnaces sorted by name for $c in /bank/customer where $c/balance < 0 order by $c/customer_name return { $c/* }
25
10.25XQuery Find all accounts with balance > 400, with each result enclosed in an.. tag for $x in /bank-2/account let $acctno := $x/@account_number where $x/balance > 400 return { $acctno } Items in the return clause are XML text unless enclosed in {}, in which case they are evaluated Let clause not really needed here: for $x in /bank-2/account[balance>400] return { $x/@account_number }
26
10.26Joins Joins are similar to SQL for $a in /bank/account, $c in /bank/customer, $d in /bank/depositor where $a/account_number = $d/account_number and $c/customer_name = $d/customer_name return { $c $a }... lots more
27
10.27XSLT A stylesheet specifies document formatting e.g. an HTML css may specify font colors and sizes for headings,... stored outside the document separate content and presentation XSL XML Stylesheet Language originally designed for generating HTML from XML XSLT a general-purpose transformation language can translate XML to XML, and XML to HTML XSLT transformations are expressed using rules called templates combine selection using XPath with construction of results strung together to produce a document
28
10.28 Creating XML Output E.g. to wrap results in new XML elements. match specifies a pattern in XPath xsl:value-of selects (outputs) specified values thwarts default behavour of outputing all elements that don't match any template. Output: Joe Mary
29
10.29XSLT Example of templates strung together: Using an xsl:sort directive inside a template causes all elements matching the template to be sorted
30
10.30 10.6 Storing XML Data XML data can be stored in non-relational data stores flat files –natural for XML –has problems discussed in Chapter 1 (no concurrency, no recovery, …) XML database –database built specifically for storing XML data –no commercial-grade systems relational databases data is translated into relational form advantage: mature database systems disadvantages: overhead of translating data and queries
31
10.31 Storing XML in Relational Databases Alternatives: String Representation Tree Representation Map to relations
32
10.32 Tree Representation Model XML data as tree, store using relations nodes(id, type, label, value) child (child_id, parent_id) Nodes: each element/attribute is given a unique identifier, type indicates element/attribute, label: the tag name of the element/name of attribute, value: the text value of the element/attribute Child: captures parent-child relationships in the tree, can add an extra attribute to child to record ordering of children Benefit: can store any XML data, even without DTD Drawbacks: data is broken up into too many pieces, increasing space overheads queries require a large number of joins, which can be slow bank (id:1) customer (id:2) account (id: 5) customer_name (id: 3) account_number (id: 7)
33
10.33 10.7.3 Web Services Simple Object Access Protocol (SOAP): an XML-based protocol for applications to exchange information over HTTP invokes procedures across applications with distinct databases HTTP: the protocol for sending messages over the internet see example on next slide Web service: a site providing a collection of SOAP procedures described using the Web Services Description Language (WSDL) directories of Web services are described using the Universal Description, Discovery, and Integration (UDDI) standard Stephanie's CS 330 Project
34
10.34 HTTP Messages E.g. a resource request for a web page: GET /hi.htm HTTP/1.1 Host: xx.wells.edu Accept text/plain The response: HTTP/1.1 200 OK Content-Type: text/plain Content-Length: 42 Hello there (HTTP/1.1 200 OK is the header attached to a successfully returned page)
35
10.35PowerPoint? Chapter 10: XML What is XML? XML: Extensible Markup Language...
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.