DOC PREVIEW
USF CS 682 - XML Schema

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Modifying XML programmatically Creating new nodes Adding nodes Example Example Defining XML documents Data Interchange DTDs vs Schema DTDs DTD example Weaknesses of DTDs XML Schema An example Using a schema Schema datatypes Complex types Complex types Value Restrictions Value Restrictions Value Restrictions Groupings Referencing Schema Namespaces SummaryDistributed SoftwareDevelopmentXML SchemaChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p. 1/??Modifying XML programmatically•Last week we saw how to use the DOM parser to read anXML document.•The DOM parser can also be used to create and modifynodes.Department of Computer Science — University of San Francisco – p. 2/??Creating new nodes•The top-level Node in an XML document is of classDocument•It contains a set of factory methods that allow you to createnew nodes.•doc = minidom.parse(’cdcat.xml’)•doc.createElement(’songs’)•doc.createTextNode(’Tomorrow Never Knows’)•doc.createAttribute(’encoding’)•After creating a node, it can be added to the tree.Department of Computer Science — University of San FranciscoAdding nodes•We then insert nodes by attaching them to existing nodes.•node.appendChild(newNode)•node.insertBefore(newNode, childAfter)•node.replaceChild(newNode, oldNode)•We can also remove nodes:•node.removeChild(nodename)Department of Computer Science — University of San Francisco – p. 4/??Exampledef changeRating(artist, newrating) :cds = doc.getElementsByTagName(’cd’)for item in cds :a = item.getElementsByTagName(’artist’)if a[0].firstChild.data.strip() == artist.strip() :r = item.getElementsByTagName(’rating’)r[0].replaceChild(doc.createTextNode(newrating), r[0].firstChild)return docDepartment of Computer Science — University of San Francisco – p. 5/??Exampledef addLabel(artist, label) :cds = doc.getElementsByTagName(’cd’)print cdsfor item in cds :print itema = item.getElementsByTagName(’artist’)if a[0].firstChild.data.strip() == artist.strip() :n = doc.createElement(’Label’)n.appendChild(doc.createTextNode(label))item.appendChild(n)print cds[1].toxml()return docDepartment of Computer Science — University of San FranciscoDefining XML documents•Recall that, unlike HTML, an XML author can declare anytags he or she wants.•If you’re just making your own simple documents, an ad hocapproach can work fine.•If you’re building more complex applications, need toincorporate legacy data, or need to exchange data withothers, this may not be suitable.•XML Schema are a way to formally define legal XMLdocuments.Department of Computer Science — University of San Francisco – p. 7/??Data Interchange•A challenge in exchanging data between heterogenoussystems is ensuring that all participants agree on themeaning and representation of the data.•Is author a sub-element of book, or the other wayaround?•Do all books have to have an ISBN tag, or is itoptional? What is the format of a valid ISBN number?•Must price be a float?•Is there an order that elements must occur in?•XML allows users of data to validate this data against aschema.Department of Computer Science — University of San Francisco – p. 8/??DTDs vs Schema•There are (at least) two different mechanisms for specifyingthe legal structure of an XML document.•DTDs•XML Schema•DTDs are an older technology•Less flexible, but still found in many documents.•XML Schema are a newer, W3C-backed standard.Department of Computer Science — University of San FranciscoDTDs•A Document Type Definition is information about the legalstructure of an XML document.•A DTD allows you to specify the set of allowable elements(the vocabulary), how they fit together (the grammar), andthe legal values that can be assigned to them (theirsemantics).Department of Computer Science — University of San Francisco – p. 10/??DTD example<!-- BTW, here’s how to add a comment. This is our book DTD --!><!ELEMENT book (author+ , subtitle?, title, price+, publisher+, isbn,(volumes | description)*)><!ELEMENT author (#PCDATA)><!ELEMENT title (#PCDATA)><!ELEMENT volumes volume+><!ELEMENT volume (#PCDATA)><!ELEMENT publisher (#PCDATA)><!ELEMENT isbn (#PCDATA)><!ATTLIST bookgenre (fantasy | sci-fi | mystery | horror) ‘‘fantasy’’id ID #REQUIRED>Department of Computer Science — University of San Francisco – p. 11/??Weaknesses of DTDs•DTDs are still used, often for backward compatibility.•Weaknesses:•Not XML - require a different parser.•Can’t constrain range s•Overly restrictive about order.Department of Computer Science — University of San Francisco –XML Schema•XML Schema are one of several proposed techniques fordescribing how elements can be arranged.•DTDs are the other common way to do this.•Schemata are more flexible and expressive than DTDs•Backed by W3C•Essentially an XML document that describes XMLdocuments.•Allow you to specify order, data types, number ofoccurrences, etc.Department of Computer Science — University of San Francisco – p. 13/??An example(see external example)Department of Computer Science — University of San Francisco – p. 14/??Using a schema•We can then use the schema to validate an XML document.•This lets us programmatically ensure that the do cument iswell-formed.•Helps with data integration, testing output, verifyingreceived data.•Schemata also serve as a form of documentation•Can also be used to provide application-level and parsingguidance.Department of Computer Science — University of San Francisco –Schema datatypes•Schema let us specify what data types an element can have:•xs:string - any text•xs:token - tokens separated by whitespace•decimal - float•xs:integer - integer•xs:ID - - provides a unique identifier•xs:boolean - ’true’ or ’false’•xs:dateTime - 2004-11-03T11:03:00-10:00Department of Computer Science — University of San Francisco – p. 16/??Complex types•Many interesting XML elements are not just simple datatypes, but are are compositions of simple types.•For example, let’s say we want a date element that lookslike this:<date><month> 12 </month><day> 13 </day><year> 1972 </year></date>Department of Computer Science — University of San Francisco – p. 17/??Complex types•A Schema for this would look like:<xs:element name=’’date’’><xs:complexType><xs:all><xs:element


View Full Document

USF CS 682 - XML Schema

Documents in this Course
Load more
Download XML Schema
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view XML Schema and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view XML Schema 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?