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:

{small lecturenumber - hepage :} Modifying XML programmatically{small lecturenumber - hepage :} Creating new nodes{small lecturenumber - hepage :} Adding nodes{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} Defining XML documents{small lecturenumber - hepage :} Data Interchange{small lecturenumber - hepage :} DTDs vs Schema{small lecturenumber - hepage :} DTDs{small lecturenumber - hepage :} DTD example{small lecturenumber - hepage :} Weaknesses of DTDs{small lecturenumber - hepage :} XML Schema{small lecturenumber - hepage :} An example{small lecturenumber - hepage :} Using a schema{small lecturenumber - hepage :} Schema datatypes{small lecturenumber - hepage :} Complex types{small lecturenumber - hepage :} Complex types{small lecturenumber - hepage :} Value Restrictions{small lecturenumber - hepage :} Value Restrictions{small lecturenumber - hepage :} Value Restrictions{small lecturenumber - hepage :} Groupings{small lecturenumber - hepage :} Referencing Schema{small lecturenumber - hepage :} Namespaces{small lecturenumber - hepage :} SummaryDistributed Software DevelopmentXML SchemaChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p. 1/??7-2: Modifying XML programmatically•Last week we saw how to use the DOM parser to read an XMLdocument.•The DOM parser can also be used to create and modify nodes.Department of Computer Science — University of San Francisco – p. 2/??7-3: Creating new nodes•The top-level Node in an XML document is of class Document•It contains a set of factory methods that allow you to create newnodes.◦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 Francisco7-4: Adding 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/??7-5: 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/??7-6: 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 Francisco7-7: Defining XML documents•Recall that, unlike HTML, an XML author can declare any tagshe 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 with others,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/??7-8: Data Interchange•A challenge in exchanging data between heterogenoussystems is ensuring that all participants agree on the meaningand representation of the data.◦Is author a sub-element of book, or the other way around?◦Do all books have to have an ISBN tag, or is it optional?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/??7-9: DTDs vs Schema•There are (at least) two different mechanisms for specifying thelegal 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 Francisco7-10: DTDs•A Document Type Definition is information about the legalstructure of an XML document.•A DTD allows you to specify the set of allowable elements (thevocabulary), how they fit together (the grammar), and the legalvalues that can be assigned to them (their semantics).Department of Computer Science — University of San Francisco – p. 10/??7-11: 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/??7-12: Weaknesses of DTDs•DTDs are still used, often for backward compatibility.•Weaknesses:◦Not XML - require a different parser.◦Can’t constrain ranges◦Overly restrictive about order.Department of Computer Science — University of San Francisco7-13: 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 XML documents.◦Allow you to specify order, data types, number ofoccurrences, etc.Department of Computer Science — University of San Francisco – p. 13/??7-14: An example(see external example)Department of Computer Science — University of San Francisco – p. 14/??7-15: Using a schema•We can then use the schema to validate an XML document.•This lets us programmatically ensure that the document iswell-formed.◦Helps with data integration, testing output, verifying receiveddata.•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


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?