cs 157b: database management systems ii february 18 class meeting department of computer science san...
TRANSCRIPT
CS 157B: Database Management Systems IIFebruary 18 Class Meeting
Department of Computer ScienceSan Jose State University
Spring 2013Instructor: Ron Mak
www.cs.sjsu.edu/~mak
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
2
XML Schema
XML Schema Definition (XSD) Specify the structure of XML data.
Application-specific. Replacement for DTD.
An XML document is well-formed if it has correct XML syntax.
An XML document is valid if its contents conform to its schema. The schema specifies what elements and attributes
a valid document must have, and in what order. The schema specifies the data type and format of the content.
_
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
3
XML Schema Types
Simple type An XML element with only text content. No attributes and no child elements.
Complex types Four complex types:
Complex type Attributes Children Text content
Text only yes - yes
Element only yes yes -
Empty element yes - -
Mixed content yes yes yes
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
4
Simple Type Definition
Example:
Some basic types: xs:string xs:integer xs:decimal xs:boolean
false or true (or: 0 or 1) xs:date
format YYYY-MM-DD
<xs:element name="age" type="xs:integer"/>
xs:time format hh:mm:ss
xs:dateTime format YYYY-MM-ddThh:mm:ss
xs:anyURINote the
T
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
5
Simple Type Definition
Predefined value Example:
If the element is present and has content, the content must match the predefined value.
If the element is present but empty,it gets the predefined value.
If the element is not present, it has no value. Default value
Example:
If the element is empty or not present,it gets the default value.
If the element is present and has content,the content that’s there is used as the value.
<xs:element name="age" type="xs:integer" fixed="25"/>
<xs:element name="age" type="xs:integer" default="25"/>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
6
Derived Simple Types
Derive a custom simple type from a base type. Example:
This is an anonymous custom type. The type itself has no name. It applies only to the element (birthday) for which it is defined.
_
<xs:element name="birthday" <xs:simpleType> <xs:restriction base="xs:date"> <xs:minInclusive value="1900-01-01"> <xs:maxInclusive value="1999-12-31"> </xs:restriction> </xs:simpleType></xs:element>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
7
Derived Simple Types
Named custom type. The type has a name. Multiple elements can refer to the type by name. Example:
<xs:simpleType name="birthday_type"> <xs:restriction base="xs:date"> <xs:minInclusive value="1900-01-01"/> <xs:maxInclusive value="1999-12-31"/> </xs:restriction></xs:simpleType>
<xs:element name="birthday" type="birthday_type"/>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
8
Derived Simple Types
A set of acceptable content values. Example:
<xs:simpleType name="gender_type"> <xs:restriction base="xs:string"> <xs:enumeration value="male"/> <xs:enumeration value="female"/> </xs:restriction></xs:simpleType>
<xs:element name="gender" type="gender_type"/>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
9
Derived Simple Types
A regular expression pattern. Example:
<xs:simpleType name="product_code"> <xs:restriction base="xs:string"> <xs:pattern value="X_\d{3}"/> </xs:restriction></xs:simpleType>
Perl-style regular
expression.
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
10
Derived Simple Types
A union type. Example:
<xs:simpleType name="isbn10"> <xs:restriction base="xs:string"> <xs:pattern value="\d-\d{3}-\d{5}-\d"/> </xs:restriction></xs:simpleType>
<xs:simpleType name="isbn13"> <xs:restriction base="xs:string"> <xs:pattern value="\d{3}-\d-\d{3}-\d{5}-\d"/> </xs:restriction></xs:simpleType>
<xs:element name="book"> <xs:simpleType> <xs:union memberTypes="isbn10 isbn13"/> </xs:simpleType></xs:element>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
11
Derived Simple Types
A list type. Example:
Therefore, in the XML document, the “holidays” element can have a list of date values:
<xs:simpleType name="date_list_type"> <xs:list itemType="xs:date"/></xs:simpleType>
<xs:element name="holidays" type="date_list_type"/>
<holidays> 2013-01-01 2013-07-04 2013-12-25</holidays>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
12
Sample XML Document
<?xml version="1.0" encoding="UTF-8"?> <catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="catalog.xsd"> <journal title="XML" publisher="IBM developerWorks"> <article level="Intermediate" date="February-2003"> <title>Design XML Schemas Using UML</title> <author>Ayesha Malik</author> </article> </journal> <journal title="Java Technology" publisher="IBM developerWorks"> <article level="Advanced" date="January-2004"> <title>Design service-oriented architecture frameworks with J2EE technology </title> <author>Naveen Balani</author> </article> <article level="Advanced" date="October-2003"> <title>Advance DAO Programming</title> <author>Sean Sullivan </author> </article> </journal> </catalog>
Schema reference
XML document adapted from the bookPro XML Development with Java Technology,by Ajay Vohra and Deepak Vohra, Apress, 2006
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
13
Sample XML Schema: catalog.xsd
<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="article_type"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> </xs:sequence> <xs:attribute name="date" type="xs:string"/> <xs:attribute name="level" type="xs:string"/> </xs:complexType> <xs:element name="article" type="article_type"/> ...</xs:schema> XML schema adapted from the book
Pro XML Development with Java Technology,by Ajay Vohra and Deepak Vohra, Apress, 2006
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
14
Sample XML Schema: catalog.xsd
<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> ... <xs:element name="article" type="article_type"/> <xs:complexType name="journal_type"> <xs:sequence> <xs:element ref="article" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="title" type="xs:string"/> <xs:attribute name="publisher" type="xs:string"/> </xs:complexType>
<xs:element name="journal" type="journal_type"/> ...</xs:schema>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
15
Sample XML Schema: catalog.xsd
<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> ... <xs:element name="journal" type="journal_type"/> <xs:complexType name="catalog_type"> <xs:sequence> <xs:element ref="journal" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:element name="catalog" type="catalog_type"/></xs:schema>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
16
Complex Types
Four complex types:
Complex type Attributes Children Text content
Text only yes - yes
Element only yes yes -
Empty element yes - -
Mixed content yes yes yes
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
17
Complex Types: Element Only
Attributes and child elements, no text content. Example:
<xs:sequence> imposes an order on child elements. <xs:all> for child elements in any order. <xs:attribute> elements come last.
_
<xs:complexType name="article_type"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> </xs:sequence> <xs:attribute name="date" type="xs:string"/> <xs:attribute name="level" type="xs:string"/></xs:complexType>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
18
Complex Types: Element Only
Choice of child elements Example:
Either a social security number, or a username followed by a password.
<xs:complexType> <xs:sequence> <xs:choice> <xs:element name="ssn" type="xs:string"/>
<xs:sequence> <xs:element name="username" type="xs:string"/> <xs:element name="password" type="xs:string"/> </xs:sequence> </xs:choice> </xs:sequence></xs:complexType>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
19
Complex Types: Text Only
Attributes and text content, no child elements. Example:
In this example, an element of type “year_type” must have content that is a positive integer. It must also have an “era” attribute whose value is a string.
Use <xs:restriction> instead of <xs:extension> to add restrictions (such as maximum string length) to the base type._
<xs:complexType name="year_type"> <xs:simpleContent> <xs:extension base="xs:positiveInteger"> <xs:attribute name="era" type="xs:string"/> </xs:extension> </xs:simpleContent></xs:complexType>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
20
Complex Types: Empty Element
Attributes only, no text content and no child elements. Example:
<xs:complexType name="id_type"> <xs:attribute name="userid" type="xs:positiveInteger"/> <xs:attribute name="passkey" type="xs:string"/></xs:complexType>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
21
Complex Types: Mixed Content
Attributes, child elements, and text content. Example:
The elements are interspersed among the text content. Instead of <xs:sequence>, you can also use
<xs:all> or <xs:choice>._
<xs:complexType name="miscellaneous" mixed="true"> <xs:sequence> <xs:element ...> ... </element> <xs:element ...> ... </element> </xs:sequence>
<xs:attribute ... /> <xs:attribute ... /></xs:complexType>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
22
Attributes
Attributes are simple type elements. Example:
Attributes are optional by default. Add use="required" to the attribute definition
to make the attribute mandatory. Add use="prohibited" to prohibit the attribute.
_
<xs:attribute name="ssn"> <xs:simpleType> <xs:restriction base=xs:string> <xs:pattern value="\d{3}-\d{2}-\d{3}"/> </xs:restriction> </xs:simpleType></xs:attribute>
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
23
Validating XML Documents
Install NetBeans XML tools plugins: Schema editor
https://blogs.oracle.com/geertjan/entry/xml_schema_editor_in_netbeans
Query windowhttp://plugins.netbeans.org/plugin/15704/query-xml
Validate an XML document using Java’s DOM and SAX parsers._
Demos
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
24
Object-XML Mapping
JAXB 2.0 package. Automatic object-XML mapping. Object bindings: One Java object per XML element. Similar to object-relational mapping with Hibernate.
Use the xjc compiler to generate Java classes for complex types in the XML schema. Complete with Java annotations. Requires an XML schema.
At run time: Marshalling
Generate XML elements from a Java object tree. Unmarshalling
Generate a Java object tree from a XML elements.
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
25
Complete XML Schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="article_type"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> </xs:sequence> <xs:attribute name="date" type="xs:string"/> <xs:attribute name="level" type="xs:string"/> </xs:complexType> <xs:element name="article" type="article_type"/> <xs:complexType name="journal_type"> <xs:sequence> <xs:element ref="article" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="title" type="xs:string"/> <xs:attribute name="publisher" type="xs:string"/> </xs:complexType> <xs:element name="journal" type="journal_type"/> <xs:complexType name="catalog_type"> <xs:sequence> <xs:element ref="journal" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:element name="catalog" type="catalog_type"/></xs:schema>
XML schema adapted from the bookPro XML Development with Java Technology,
by Ajay Vohra and Deepak Vohra, Apress, 2006
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
26
xjc Compiler
Should be part of your standard Java installation. Command line:
<source directory> is where you want the generated Java classes to go. (It’s the source directory for the Java compiler.)
Example:
The xjc compiler generates Java classesfor complex types in the XML schema. The classes will be properly annotated for XML mapping.
_
xjc –d <source directory> -p <package name> <schema file>
xjc -d src -p jaxbdemo.generated catalog.xsd
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
27
Unmarshalling an XML Document
Create an unmarshaller. Put the generated Java classes into package
jaxbdemo.generated for this example.
Unmarshal the XML document. Get a Catalog object.
Get the list of Journal objects.
JAXBContext jaxbContext = JAXBContext.newInstance("jaxbdemo.generated");Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
JAXBElement<CatalogType> catalogElement = (JAXBElement<CatalogType>) unMarshaller.unmarshal(xmlDocument);CatalogType catalog = catalogElement.getValue();
List<JournalType> journalList = catalog.getJournal();
Demo
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
28
Marshalling an XML Document
Create a marshaller and an object factory. We'll use the Java classes in package jaxbdemo.generated
for this example.
Create a Catalog and Journal objects. Get references to the lists of Journal and Article objects. Add the Journal object to the Journal list.
JAXBContext jaxbContext = JAXBContext.newInstance("jaxbdemo.generated");Marshaller marshaller = jaxbContext.createMarshaller();ObjectFactory factory = new ObjectFactory();
CatalogType catalog = factory.createCatalogType();List<JournalType> journalList = catalog.getJournal();
JournalType journal = factory.createJournalType();List<ArticleType> articleList = journal.getArticle();
journalList.add(journal);
Demo
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
29
Marshalling an XML Document
Create an Article object. Add it to the Article list.
Marshal the Java objects to XML elements. Write the XML document to standard out.
ArticleType article = factory.createArticleType();article.setTitle("Service Oriented Architecture Frameworks");article.setAuthor("Naveen Balani");article.setLevel("Intermediate");article.setDate("January-2004");articleList.add(article);
JAXBElement<CatalogType> catalogElement = factory.createCatalog(catalog);marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);marshaller.marshal(catalogElement, System.out);
Department of Computer ScienceSpring 2013: February 18
CS 157B: Database Management Systems II© R. Mak
30
Project #2
Due: Friday, March 1. Experience:
Writing XML schemas Validating XML documents Marshalling and unmarshalling XML data XQuery Altova XMLSpy tool
Download the Altova XMLSpy 2013 tool from:http://www.altova.com/download-trial.html# Evaluation good for 30 days.
_