Download presentation
Presentation is loading. Please wait.
Published byOliver Shepherd Modified over 9 years ago
1
1 Advanced Topics XML and Databases
2
2 XML u Overview u Structure of XML Data –XML Document Type Definition DTD –Namespaces –XML Schema u Query and Transformation –XPath –XSLT –XQuery
3
3 XML Overview u eXtensible Markup Language xML u Hyper-Text Markup Language (HTML) for document presentation and Standard Generalized Markup Language SGML for document management. u XML can handle structured data typical of DBMS. u XML is flexible and can handle semi-structured data that cannot be handled by relational DBMS. u XML is the de facto representation to exchange data between applications on the Web.
4
4 XML Overview u Markup Language –separation of content and markup; –meaning of the markup; –E.g., HTML shows document markup for presentation; –Tags – Database System Concepts –HTML has a specific set of tags; –XML is extensible and applications can specify tags as needed.
5
5 XML Overview u Comparison with DBMS –Focus is on the EXCHANGE of data between applications. –Storage and management of XML is more complex than for relational DBMS since XML is semi- structured. –Tagged XML means that the message is self- documenting. No need for catalog, etc. –Format of XML is not rigid and an application can ignore any fields. –Versatile since most browsers are XML enabled and most DBMS vendors support XML data.
6
6 Structure of XML Data u XML document; single root, e.g., bank in Figure 10.1 u Element: bank is the root element and document also contains customer, account and depositor elements. u Elements in the XML document must be properly nested, i.e., matching start and end tag within parent. u is properly nested. u is not properly nested. u Figure 10.2 – Combine unstructured data (text) and semi-structured data. This is one of the strengths of XML data exchange.
7
7 Structure of XML Data u Nested data in XML can be considered similar to the output of a join from multiple tables or an unnormalized (nested) relational table. u Figure 10.3 shows account elements nested within customer elements. –Advantage is that there is no need to join customer and account. –Shipping address is stored with each shipment. –Disadvantage is that if customer and account is a many-to-many relationship then the account information will be replicated with all the disadvantages of replicated information.
8
8 Structure of XML Data u Element u Subelement – or u Attribute –Figure 10.4 –Attribute is of type string; it cannot be repeated within an element and cannot have sub-elements. –account is an element; acct-type is an attribute; account-number and branch-name and balance are subelements of element account.
9
9 XML Namespace u Namespace allows organizations to specify globally unique names for element tags. u Each tag or attribute is associated with a URI and this combination of URI and tag (attribute) is unique. u Namespace can be declared in the root element. u http://www.FirstBank.com …. …. …
10
10 XML DTD u XML documents do not have to conform to any schema or set of pre-defined tags. u However, in most cases, applications require that data conforms to some pre-defined tags. u XML DTD –Allowed list of elements and subelements within elements. –Does not identify data types and other constraints. –| (or) + (1 or more) ? (0 or more)
11
11 XML DTD u Figure 10.6 DTD Example –bank element consists of one or more account or customer or depositor elements (in that order). –account element has subelements account-number, branch-number, balance, etc. –elements account-number, branch-name, etc. are of type #PCDATA (text or string). –empty – element has no contents. –any – element can have any subelements. –attrributes must have a type declaration and a default value.
12
12 XML DTD u ID and IDREF and IDREFS Figure 10.7 u ID –An attribute of type ID for an element provides a unique (global) identifier or key for that element. –An element can at most have one such attribute of type ID. –<!ATTLIST account account-number ID #REQUIRED u An attribute of type IDREF is a reference to an element; its value MUST BE the unique ID value of some element in the document. u IDREFS is a set of ID values. u ID and IDREF and IDREFS capture primary key and foreign key functionality of the relational data model. u Figure 10.8 Example of XML document with ID and IDREFS. u IDREF must point to an ID but there is no type checking so it can point to the ID of an account or the ID of a customer or the ID of a branch!
13
13 XML Schema – Figure 10.9 u XML Schema is closer in spirit to relational schemas. u It is closely associated with namespaces, e.g., xmlns:xsd=http://www.w3.org/2001/XMLSchema>http://www.w3.org/2001/XMLSchema u Supports uniqueness of primary keys and constraints on foreign keys. u element has name and type u complexType (account or customer or depositor) is a sequence of subelements. u complexType BankType is a sequence of references to elements of type account or customer or depositor. –More well defined than XML DTD since IDREF could refer to an element irrespective of whether it was an account or a customer. u minOccurs and maxOccurs are multiplicity constraints.
14
14 Query and Transformation of XML u 3 kinds of query languages –XPath is the building block of path expressions. –XSLT is a transformation language. »Originally designed to convert to HTML. »XSLT can transform one XML document to another so it is also a query language. »Most widely supported. –XQuery is more like an object query language. u Tree model of XML data –Root –Nodes are either elements or attributes. –Element nodes can have children which are subelements or attributes of that element.
15
15 Query and Transformation of XML u Path expression –Sequence of /xx/yy/zz where / refers to the root. –Result is a set of values from the XML document. –/bank-2/customer/customer-name on Figure 10.8 returns Joe and Lisa and Mary –/bank-2/customer/customer-name/text() would return only the values and not the tagged elements. –/bank-2/account/@account-number also returns the set of account numbers. @ cannot be applied to IDREFS. u Selection – /bank-2/account[balance > 400] – /bank-2/account[balance > 400]/@account-number u Count –/bank-2/account/[customer/count() > 2] u Skip intermediate elements –/bank-2//name
16
16 BMGTG402 Namespace u …. …
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.