XML Programming
description
Transcript of XML Programming
![Page 1: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/1.jpg)
XML ProgrammingXML Programming
IntroductionIntroduction
XML, Schema, etc.XML, Schema, etc.
![Page 2: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/2.jpg)
What is XMLWhat is XML
Extensible Markup LanguageExtensible Markup Language ( (XMLXML) ) a general-purpose specification for creating custom
markup languages. It is classified as an extensible language because it allows its users to define their own elements. Its primary purpose is to facilitate the sharing of structured data across different information systems, particularly via the Internet, and it is used both to encode documents and to serialize data. In the latter context, it is comparable with other text-based serialization languages such as JSON and YAML.
W3C recommendation, open standard
![Page 3: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/3.jpg)
XML in messagingXML in messaging
Most modern languages have method of Most modern languages have method of representing structured data.representing structured data.
Typical flow of events in applicationTypical flow of events in application
Read data(file, db, socket)
Marshalobjects
Manipulate inprogram
Unmarshal (file, db, socket)
•Many language-specific technologies to reduce these steps: RMI, object serialization in any language, CORBA (actually somewhat language neutral), MPI, etc.
•XML provides a very appealing alternative that hits the sweet spot for many applications
![Page 4: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/4.jpg)
User-defined types in programming User-defined types in programming languageslanguages
One view of XML is as a text-based, programming-One view of XML is as a text-based, programming-language-neutral way of representing structured language-neutral way of representing structured
information.information. Compare:Compare:
struct Student{ char* name; char* ssn; int age; float gpa;}
class Student{ public String name; public String ssn; public int age; public float gpa;}
C Java Fortrantype Student character(len=*) :: name character(len=*) :: ssn integer :: age real :: gpaend type Student
![Page 5: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/5.jpg)
Sample XML SchemaSample XML Schema
• In XML, (a common) datatype description is called an XML schema.• DTD and Relax NG are other common alternatives• Below uses schema just for illustration purposes• Note that schema itself is written in XML
<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="student"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="ssn" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="gpa" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>
Ignore thisFor now
![Page 6: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/6.jpg)
Alternative schemaAlternative schema
•In this example studentType is defined separately rather than anonymously
<xs:schema> <xs:element name="student" type="studentType“/> <xs:complexType name="studentType"> <xs:sequence>
<xs:element name="name" type="xs:string"/><xs:element name="ssn" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="gpa" type="xs:decimal"/>
</xs:sequence> </xs:complexType></xs:schema>
new type defined separately
![Page 7: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/7.jpg)
Alternative: DTDAlternative: DTD• Can also use a DTD (Document Type Descriptor), but this is much simpler than a schema but also much less powerful (notice the lack of types)
<!DOCTYPE Student [ <! – Each XML file is stored in a document whose name is the same as the root node -- > <! ELEMENT Student (name,ssn,age,gpa)> <! – Student has four attributes -- > <!ELEMENT name (#PCDATA)> <! – name is parsed character data -- > <!ELEMENT ssn (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT gpa (#PCDATA)>]>
![Page 8: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/8.jpg)
Another alternative: Relax NGAnother alternative: Relax NG
Gaining in popularityGaining in popularity
Can be very simple to write and at same Can be very simple to write and at same time has many more features than DTDtime has many more features than DTD
Still much less common than SchemaStill much less common than Schema
![Page 9: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/9.jpg)
Creating instances of typesCreating instances of types
In programming languages, we instantiate objects:
struct Student s1, s2;s1.name = “Andrew”s1.ssn=“123-45-6789”;
Student s = new Student();s1.name = “Andrew”;s1.ssn=“123-45-6789”;.type(Student) :: s1s1%name = ‘Andrew’.
C
Java
Fortran
![Page 10: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/10.jpg)
Creating XML documentsCreating XML documents
XML is XML is notnot a programming language! a programming language!
In XML we make a Student “object” in an xml file In XML we make a Student “object” in an xml file (Student.xml):(Student.xml):
<Student><Student>
<name>Andrew</name><name>Andrew</name>
<ssn>123-45-6789</ssn><ssn>123-45-6789</ssn>
<age>39</age><age>39</age>
<gpa>2.0</gpa><gpa>2.0</gpa>
</Student> </Student>
Think of this as like a serialized object.Think of this as like a serialized object.
![Page 11: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/11.jpg)
XML and SchemaXML and Schema
Note that there are two parts to what we didNote that there are two parts to what we did Defining the “structure” layoutDefining the “structure” layout Defining an “instance” of the structureDefining an “instance” of the structure
The first is done with an appropriate Schema or The first is done with an appropriate Schema or DTD.DTD.The second is the XML partThe second is the XML partBoth can go in the same file, or an XML file can Both can go in the same file, or an XML file can refer to an external Schema or DTD (typical)refer to an external Schema or DTD (typical)From this point on we use only SchemaFrom this point on we use only SchemaExercise 1Exercise 1
![Page 12: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/12.jpg)
??
Question: What can we do with such a file?Question: What can we do with such a file?
Some answers:Some answers:
Write corresponding Schema to define its contentWrite corresponding Schema to define its content
Write XSL transformation to displayWrite XSL transformation to display
Parse into a programming languageParse into a programming language
![Page 13: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/13.jpg)
Exercise 1Exercise 1
![Page 14: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/14.jpg)
Exercise 1 SolutionExercise 1 Solution<?xml version="1.0" encoding="UTF-8"?><cars> <car> <make>dodge</make> <model>ram</model> <color>red</color> <year>2004</year> <mileage>22000</mileage> </car>
<car> <make>Ford</make> <model>Pinto</model> <color>white</color> <year>1980</year> <mileage>100000</mileage> </car>
</cars>
![Page 15: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/15.jpg)
Some sample XML Some sample XML documentsdocuments
![Page 16: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/16.jpg)
Order / WhitespaceOrder / Whitespace
Note that element order is important, but whitespace in element data is not. This is the same as far as the xml parser is concerned:
<Article ><Headline>Direct Marketer Offended by Term 'Junk Mail' </Headline><authors>
<author> Joe Garden</author><author> Tim Harrod</author>
</authors><abstract>Dan Spengler, CEO of the direct-mail-marketing firm Mailbox of
Savings, took umbrage Monday at the use of the term <it>junk mail</it></abstract><body type="url" > http://www.theonion.com/archive/3-11-01.html </body>
</Article>
![Page 17: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/17.jpg)
Molecule ExampleMolecule Example
XML is extremely useful for standardizing data sharing within XML is extremely useful for standardizing data sharing within specialized domains. Below is a part of the specialized domains. Below is a part of the Chemical Chemical Markup Language Markup Language describing a water molecule and its describing a water molecule and its constituentsconstituents
<?xml version "1.0" ?><?xml version "1.0" ?><CML><CML>
<MOL TITLE="Water" ><MOL TITLE="Water" ><ATOMS> <ATOMS>
<ARRAY BUILTIN="ELSYM" > H O H</ARRAY><ARRAY BUILTIN="ELSYM" > H O H</ARRAY></ATOMS></ATOMS><BONDS><BONDS>
<ARRAY BUILTIN="ATID1" >1 2</ARRAY><ARRAY BUILTIN="ATID1" >1 2</ARRAY><ARRAY BUILTIN="ATID2" >2 3</ARRAY><ARRAY BUILTIN="ATID2" >2 3</ARRAY><ARRAY BUILTIN="ORDER" >1 1</ARRAY><ARRAY BUILTIN="ORDER" >1 1</ARRAY>
</BONDS></BONDS></MOL></MOL>
</CML></CML>
![Page 18: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/18.jpg)
Rooms exampleRooms example
A typical example showing a few more XML features:A typical example showing a few more XML features:
<?xml version="1.0" ?><?xml version="1.0" ?> <rooms> <rooms>
<room name="<room name="RedRed">"> <capacity><capacity>1010</capacity> </capacity> <equipmentList><equipmentList>
<equipment><equipment>ProjectorProjector</equipment> </equipment>
</equipmentList></equipmentList> </room></room><room name="<room name="GreenGreen">">
<capacity><capacity>55</capacity> </capacity> <equipmentList /> <equipmentList /> <features><features> <feature><feature>No RoofNo Roof</feature> </feature> </features></features>
</room></room> </rooms></rooms>
![Page 19: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/19.jpg)
SuggestionSuggestion
Try building each of those documents in Try building each of those documents in an XML builder tool (XMLSpy, Oxygen, an XML builder tool (XMLSpy, Oxygen, etc.) or at least an XML-aware editor.etc.) or at least an XML-aware editor.
Note: it is not required to create a schema Note: it is not required to create a schema to do this. Just create new XML document to do this. Just create new XML document and start building.and start building.
![Page 20: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/20.jpg)
Dissecting an XML Dissecting an XML DocumentDocument
![Page 21: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/21.jpg)
Things that can appear in an XML documentThings that can appear in an XML document
ELEMENTSELEMENTS: : simplesimple, , complexcomplex, , emptyempty, or , or mixedmixed content model; content model; attributes. attributes.
The The XML declarationXML declaration
Processing instructions(PIsProcessing instructions(PIs) ) <? …?><? …?> Most common is Most common is <?xml-stylesheet …?><?xml-stylesheet …?> <?xml-stylesheet type=“text/css” <?xml-stylesheet type=“text/css” href=“mys.css”?>href=“mys.css”?>
CommentsComments <!-- <!-- comment textcomment text --> -->
![Page 22: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/22.jpg)
Begin TagsEnd Tags
Tags
Attributes
<?xml version "1.0"<?xml version "1.0" ?>?>
<<CMLCML><><MOL TITLE="Water" MOL TITLE="Water" > <> <ATOMSATOMS>> <<ARRAY BUILTIN="ELSYM" ARRAY BUILTIN="ELSYM" >> H O H H O H</</ARRAYARRAY>></</ATOMSATOMS>><<BONDSBONDS>><<ARRAY BUILTIN="ATID1" >1 2ARRAY BUILTIN="ATID1" >1 2</</ARRAYARRAY>><<ARRAY BUILTIN="ATID2" >2 3ARRAY BUILTIN="ATID2" >2 3</</ARRAYARRAY>><<ARRAY BUILTIN="ORDER" >1 1ARRAY BUILTIN="ORDER" >1 1</</ARRAYARRAY>></</BONDSBONDS>></</MOLMOL>></</CMLCML>>
Parts of an XML documentParts of an XML documentDeclaration
AttributeValues
An XML element is everything from (including) the element's start tag to (including) the element's end tag.
![Page 23: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/23.jpg)
XML and TreesXML and TreesTags give the structure of a Tags give the structure of a document. They divide the document. They divide the document up into document up into Elements, Elements, starting at the top most starting at the top most element, theelement, the root element. root element. The The stuff inside an element is its stuff inside an element is its content – content cancontent – content caninclude other elements along include other elements along with ‘character data’with ‘character data’
CML
MOL
ATOMS BONDS
ARRAY ARRAY ARRAY ARRAY
HOH 12 23 11
Root element
CDATA sections
![Page 24: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/24.jpg)
XML and XML and TreesTrees
<?xml version "1.0"<?xml version "1.0" ?>?><<CMLCML>>
<<MOL TITLE="Water" MOL TITLE="Water" >><<ATOMSATOMS>>
<<ARRAY BUILTIN="ELSYM" ARRAY BUILTIN="ELSYM" >> H O H H O H</</ARRAYARRAY>></</ATOMSATOMS>><<BONDSBONDS>>
<<ARRAY BUILTIN="ATID1" >1 2ARRAY BUILTIN="ATID1" >1 2</</ARRAYARRAY>><<ARRAY BUILTIN="ATID2" >2 3ARRAY BUILTIN="ATID2" >2 3</</ARRAYARRAY>><<ARRAY BUILTIN="ORDER" >1 1ARRAY BUILTIN="ORDER" >1 1</</ARRAYARRAY>>
</</BONDSBONDS>></</MOLMOL>>
</</CMLCML>>
CML
MOL
ATOMS BONDS
ARRAY ARRAY ARRAY ARRAY
HOH 12 23 11
Root element
Data sections
![Page 25: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/25.jpg)
XML and TreesXML and Trees
rooms
room
capacity equipmentlistequipmentlist
equipment
capacity
room
features
feature10
projector
5
No Roof
![Page 26: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/26.jpg)
More detail on elementsMore detail on elements
![Page 27: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/27.jpg)
Element relationshipsElement relationships
<book> <title>My First XML</title> <prod id="33-657" media="paper"></prod> <chapter>Introduction to XML <para>What is HTML</para> <para>What is XML</para> </chapter> <chapter>XML Syntax <para>Elements must have a closing tag</para> <para>Elements must be properly nested</para> </chapter> </book>
•Book is the root element.
•Title, prod, and chapter are child elements of book.
•Book is the parent element of title, prod, and chapter.
•Title, prod, and chapter are siblings (or sister elements) because they have the same parent.
![Page 28: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/28.jpg)
Well formed XMLWell formed XML
![Page 29: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/29.jpg)
Well-formed vs ValidWell-formed vs Valid
An XML document is said to be An XML document is said to be well-well-formedformed if it obeys basic semantic and if it obeys basic semantic and syntactic constraints.syntactic constraints.
This is different from a This is different from a validvalid XML XML document, which (as we will see in more document, which (as we will see in more depth) properly matches a schema.depth) properly matches a schema.
![Page 30: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/30.jpg)
Rules for Well-Formed XMLRules for Well-Formed XML
An XML document is considered well-formed if it obeys the An XML document is considered well-formed if it obeys the following rules:following rules:
There must be one element that contains all others (root element)There must be one element that contains all others (root element)
All tags must be balanced All tags must be balanced <BOOK>...</BOOK><BOOK>...</BOOK> <BOOK /><BOOK />
Tags must be nested properly:Tags must be nested properly: <BOOK> <LINE> This is OK </LINE> </BOOK><BOOK> <LINE> This is OK </LINE> </BOOK> <LINE> <BOOK> This is </LINE> definitely NOT </BOOK> <LINE> <BOOK> This is </LINE> definitely NOT </BOOK>
OKOK
Element text is case-sensitive soElement text is case-sensitive so <P>This is not ok, even though we do it all the time <P>This is not ok, even though we do it all the time
in HTML!</p>in HTML!</p>
![Page 31: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/31.jpg)
More Rules for Well-Formed XMLMore Rules for Well-Formed XML
The attributes in a tag must be in quotesThe attributes in a tag must be in quotes < ITEM CATEGORY=“Home and Garden” Name=“hoe-matic < ITEM CATEGORY=“Home and Garden” Name=“hoe-matic
t500”>t500”>
Comments are allowedComments are allowed <!–- They are done just as in HTML… --><!–- They are done just as in HTML… -->
Must begin withMust begin with <?xml version=‘1.0’ ?><?xml version=‘1.0’ ?>
Special characters must be escaped: the most common are Special characters must be escaped: the most common are < < " ' > &" ' > &
<formula> x < y+2x </formula><formula> x < y+2x </formula><cd title="" mmusic"><cd title="" mmusic">
![Page 32: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/32.jpg)
Naming RulesNaming Rules
Naming rules for XML elementsNaming rules for XML elements Names may contain letters, numbers, and other characters Names must not start with a number or punctuation character Names must not start with the letters xml (or XML or Xml ..) Names cannot contain spaces
Any name can be used, no words are reserved, but the idea is to make names descriptive. Names with an underscore separator are typical
Examples: <first_name>, <date_of_birth>, etc.
![Page 33: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/33.jpg)
XML ToolsXML Tools
XML can be created with any text editorXML can be created with any text editor
Normally we use an XML-friendly editorNormally we use an XML-friendly editor e.g. XMLSpye.g. XMLSpy nXML emacs extensionsnXML emacs extensions MSXML on WindowsMSXML on Windows OxygenOxygen Etc etc.Etc etc.
To check and validate XML, use either these tools and/or To check and validate XML, use either these tools and/or xmllint on Unix systems.xmllint on Unix systems.
![Page 34: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/34.jpg)
Another ViewAnother View
XML-as-data is one way to introduce XMLXML-as-data is one way to introduce XML
Another is as a Another is as a markup language markup language similar to html.similar to html.
One typically says that html has a fixed tag set, whereas One typically says that html has a fixed tag set, whereas XML allows the definition of arbitrary tagsXML allows the definition of arbitrary tags
This analogy is particularly useful when the goal is to use This analogy is particularly useful when the goal is to use XML for text presentation -- that is, when most of our XML for text presentation -- that is, when most of our data fields contain textdata fields contain text
Note that mixed element/text fields are permissible in XMLNote that mixed element/text fields are permissible in XML
![Page 35: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/35.jpg)
Article exampleArticle example
<Article > <Headline>Direct Marketer Offended by Term 'Junk Mail' </Headline> <authors> <author> Joe Garden</author> <author> Tim Harrod</author> </authors> <abstract>Dan Spengler, CEO of the direct-mail-marketing firm Mailbox of Savings, took umbrage Monday at the use of the term <it>junk mail</it>. </abstract> <body type="url" > http://www.theonion.com/archive/3-11-01.html </body>
</Article>
![Page 36: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/36.jpg)
More uses of XMLMore uses of XML
There is more!There is more!
A very popular use of XML is as a base syntax for A very popular use of XML is as a base syntax for programming languages (the elements become program programming languages (the elements become program control structures)control structures)
XSLT, BPEL, ant, etc. are good examplesXSLT, BPEL, ant, etc. are good examples XML is ubiqitous and must have a deep understanding to be XML is ubiqitous and must have a deep understanding to be
efficient and productiveefficient and productive
Many other current and potential uses -- up to the Many other current and potential uses -- up to the creativity of the programmercreativity of the programmer
![Page 37: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/37.jpg)
XML SchemaXML Schema
There are many details to cover of schema There are many details to cover of schema specification. It is extremely rich, flexible, specification. It is extremely rich, flexible, and somewhat complexand somewhat complex
We will do this in detail next lectureWe will do this in detail next lecture
Now we begin with a brief introductionNow we begin with a brief introduction
![Page 38: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/38.jpg)
XML Schema
XML itself does not restrict what elements existing in a document.
In a given application, you want to fix a vocabulary -- what elements make sense, what their types are, etc.
Use a Schema to define an XML dialect MusicXML, ChemXML, VoiceXML, ADXML, etc.
Restrict documents to those tags.
Schema can be used to validate a document -- ie to see if it obeys the rules of the dialect.
![Page 39: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/39.jpg)
Schema determine …What sort of elements can appear in the document.
What elements MUST appear
Which elements can appear as part of another element
What attributes can appear or must appear
What kind of values can/must be in an attribute.
![Page 40: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/40.jpg)
<?xml version="1.0" encoding="UTF-8"?><library> <book id="b0836217462" available="true"> <isbn> 0836217462 </isbn> <title lang="en"> Being a Dog is a Full-Time Job </title> <author id="CMS"> <name> Charles Schulz </name> <born> 1922-11-26 </born> <dead> 2000-02-12 </dead> </author> <character id="PP"> <name> Peppermint Patty </name> <born> 1966-08-22 </born> <qualification> bold,brash, and tomboyish </qualification> </character> <character id="Snoopy"> <name> Snoopy</name> <born>1950-10-04</born> <qualification>extroverted beagle</qualification> </character> <character id="Schroeder"> <name>Schroeder</name> <born>1951-05-30</born> <qualification>brought classical music to the Peanuts Strip</qualification> </character> <character id="Lucy"> <name>Lucy</name> <born>1952-03-03</born> <qualification>bossy, crabby, and selfish</qualification> </character> </book></library>
• We start with sample XML document and reverse engineer a schema as a simple example
First identify the elements:author, book, born, character,dead, isbn, library, name,qualification, title
Next categorize by contentmodelEmpty: contains nothingSimple: only text nodesComplex: only sub-elementsMixed: text nodes + sub-elements
Note: content model independentof comments, attributes, or processing instructions!
![Page 41: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/41.jpg)
Content modelsContent models
Simple content model: name, born, title, dead, isbn, qualification
Complex content model: libarary, character, book, author
![Page 42: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/42.jpg)
Content TypesContent Types
We further distinguish between complex and simple content Types: Simple Type: An element with only text nodes
and no child elements or attributes Complex Type: All other cases
We also say (and require) that all attributes themselves have simple type
![Page 43: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/43.jpg)
Content TypesContent Types
Simple content type: name, born, dead, isbn, qualification
Complex content type: library, character, book, author, title
![Page 44: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/44.jpg)
Exercise2 answerExercise2 answer
• In the previous example <book>
• book has element content, because it contains other elements.
• Chapter has mixed content because it contains both textand other elements.
• Para has simple content (or text content) because it
contains only text.
• Prod has empty content, because it carries no information
![Page 45: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/45.jpg)
Building the schemaBuilding the schema
Schema are XML documentsSchema are XML documents
They must contain a schema root element as suchThey must contain a schema root element as such <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified">
... ... </xs:schema>
We will discuss details in a bit -- note that yellow part can be excluded for now.
![Page 46: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/46.jpg)
Flat schema for libraryFlat schema for library
Start by defining all of the simple types (including attributes):
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema> <xs:element name=“name” type=“xs:string”/> <xs:element name=“qualification” type=“xs:string”/> <xs:element name=“born” type=“xs:date”/> <xs:element name=“dead” type=“xs:date”/> <xs:element name=“isbn” type=“xs:string”/> <xs:attribute name=“id” type=“xs:ID”/> <xs:attribute name=“available” type=“xs:boolean”/> <xs:attribute name=“lang” type=“xs:language/> …/…</xs:schema>
![Page 47: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/47.jpg)
Complex types with simple content
Now to complex types with simple content:
<title lang=“en”> Being a Dog is …</title>
<xs:element name=“title”> <xs:complexType> <xs:simpleContent> <xs:extension base=“xs:string”> <xs:attribute ref=“lang”/> </xs:extension> </xs:simpleContent> </xs:complexType></xs:element>
“the element named title has a complextype which is a simple content obtainedby extending the predefined datatypexs:string by adding the attribute definedin this schema and having the name lang.”
![Page 48: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/48.jpg)
Complex TypesComplex Types
All other types are complex types with complex content. For example:
<xs:element name=“library”> <xs:complexType> <xs:sequence> <xs:element ref=“book” maxOccurs=“unbounded”/> </xs:sequence> </xs:complexType></xs:element>
<xs:element name=“author”> <xs:complexType> <xs:sequence> <xs:element ref=“name”/> <xs:element ref=“born”/> <xs:element ref=“dead” minOccurs=0/> </xs:sequence> <xs:attribute ref=“id”/> </xs:complexType></xs:element>
![Page 49: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/49.jpg)
<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="name" type="xs:string"/> <xs:element name="qualification" type="xs:string"/> <xs:element name="born" type="xs:date"> </xs:element> <xs:element name="dead" type="xs:date"> </xs:element> <xs:element name="isbn" type="xs:string"> </xs:element> <xs:attribute name="id" type="xs:ID"> </xs:attribute> <xs:attribute name="available" type="xs:boolean"> </xs:attribute> <xs:attribute name="lang" type="xs:language"> </xs:attribute> <xs:element name="title"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute ref="lang"> </xs:attribute> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="library"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" ref="book"> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="author"> <xs:complexType> <xs:sequence> <xs:element ref="name"> </xs:element> <xs:element ref="born"> </xs:element> <xs:element ref="dead" minOccurs="0"> </xs:element> </xs:sequence> <xs:attribute ref="id"> </xs:attribute> </xs:complexType> </xs:element>
<xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element ref="isbn"> </xs:element> <xs:element ref="title"> </xs:element> <xs:element ref="author" minOccurs="0" maxOccurs="unbounded”/> <xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute ref="available"> </xs:attribute> <xs:attribute ref="id"> </xs:attribute> </xs:complexType> </xs:element> <xs:element name="character"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="born"/> <xs:element ref="qualification"/> </xs:sequence> <xs:attribute ref="id"> </xs:attribute> </xs:complexType> </xs:element>
</xs:schema>
![Page 50: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/50.jpg)
<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="library"> <xs:complexType> <xs:sequence> <xs:element name="book" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="isbn" type="xs:integer"> </xs:element> <xs:element name="title"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="lang" type="xs:language" > </xs:attribute> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="author" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"> </xs:element> <xs:element name="born" type="xs:date"> </xs:element> <xs:element name="dead" type="xs:date"> </xs:element> </xs:sequence> <xs:attribute name="id" type="xs:ID"> </xs:attribute> </xs:complexType> </xs:element> <xs:element name="character" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"> </xs:element> <xs:element name="born" type="xs:date"> </xs:element> <xs:element name="qualification" type="xs:string" > </xs:element> </xs:sequence> <xs:attribute name="id" type="xs:ID"> </xs:attribute> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute type="xs:ID" name="id"> </xs:attribute> <xs:attribute name="available" type="xs:boolean"> </xs:attribute> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element></xs:schema>
Same schema but with everythingdefined locally!
![Page 51: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/51.jpg)
Next LectureNext Lecture
Even with this simple example there are Even with this simple example there are many design issues to discussmany design issues to discuss When is a flat layout betterWhen is a flat layout better When is a nested layout betterWhen is a nested layout better What are scoping rulesWhat are scoping rules When to use ref= vs. defining new typeWhen to use ref= vs. defining new type
Schema in depth is topic of next lectureSchema in depth is topic of next lecture
![Page 52: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/52.jpg)
““Traditional” Traditional” programming modelsprogramming models
![Page 53: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/53.jpg)
Distributed programming models:Distributed programming models:Classic Web-basedClassic Web-based
Easy to deploy but slow, not great user experienceEasy to deploy but slow, not great user experience
htmlbrowser
WebServer
http
DynamicallyGenerated
html
Many programming models•JSP•ASP•Servlets•PHP•CGI (python, perl, C)•Cold Fusion
html
plus optionallyJavaScript to jazz up html
database
Lacks full supportof apps server -- notransactions, rpc, etc.
![Page 54: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/54.jpg)
Distributed programming modelsDistributed programming modelsTypical Web-basedTypical Web-based
Better user experience. Heavier, less portable, requires Better user experience. Heavier, less portable, requires socket programming to stream to server.socket programming to stream to server.
WebServer
http
DynamicallyGenerated
html
html + applet
databaseapplet
html
socket
![Page 55: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/55.jpg)
Direct ConnectionsDirect Connections
Direct socket and rpc-styleDirect socket and rpc-style
Application client
App1sockets
App2
App3
ports
Application client
App1
Remote ProceduresApp2
App3N
DS
Examples: Java’s rmi, CORBA, DCOM
![Page 56: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/56.jpg)
Application ServersApplication Servers
![Page 57: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/57.jpg)
RPC-style Web serviceRPC-style Web service
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
![Page 58: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/58.jpg)
General role of XMLGeneral role of XML
Most modern languages have method of Most modern languages have method of representing structured data.representing structured data.
Typical flow of events in applicationTypical flow of events in application
Read data(file, db, socket)
Marshalobjects
Manipulate inprogram
Unmarshal (file, db, socket)
•Many language-specific technologies to reduce these steps: RMI, object serialization in any language, CORBA (actually somewhat language neutral), MPI, etc.
•XML provides a very appealing alternative that hits the sweet spot for many applications
![Page 59: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/59.jpg)
Simple XML-based architectureSimple XML-based architecture
webbrowser
WebServer
http
“hand-rolled”XML
XML
pyth
on C
GI
“hand-rolled”XML
File system
![Page 60: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/60.jpg)
Motivating Example1Motivating Example1
Mortgage CalculatorMortgage Calculator
![Page 61: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/61.jpg)
Example: mortgage calculatorExample: mortgage calculator
Start very simple to motivate role of XMLStart very simple to motivate role of XML
All we need is a web server that supports All we need is a web server that supports a server-side programming modela server-side programming model
We will build up this example to include We will build up this example to include first web service and then SOA concepts first web service and then SOA concepts (as we gain experience)(as we gain experience)
![Page 62: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/62.jpg)
Functional requirementsFunctional requirements
Design a simple application which does the Design a simple application which does the following:following: Accepts user inputAccepts user input
Loan amountLoan amountLoan termLoan termInterest rateInterest rateExtras (assessments + taxes)Extras (assessments + taxes)
Returns per-month table ofReturns per-month table oftotal paymenttotal paymentinterestinterestprincipalprincipalsome other fun stuffsome other fun stuff
![Page 63: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/63.jpg)
Design requirementsDesign requirements
Must beMust be Clean simple interface (easy)Clean simple interface (easy) Remotely accessible with securityRemotely accessible with security Portable to different machine architecturesPortable to different machine architectures Not require heavyweight or sophisticated Not require heavyweight or sophisticated
installation on the part of the userinstallation on the part of the user Sufficiently fast not to be embarrassing given Sufficiently fast not to be embarrassing given
10 hits/minute maximum usage10 hits/minute maximum usage
![Page 64: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/64.jpg)
Some possible architecturesSome possible architectures
Things I tried Things I tried what are (dis) advantages of each?what are (dis) advantages of each?
Web serverWeb server Server-side scripting with pure (dynamic) html Server-side scripting with pure (dynamic) html Server-side scripting with html+javascriptServer-side scripting with html+javascript Server-side scripting with html+appletServer-side scripting with html+applet
Direct connectionDirect connection Raw socketsRaw sockets Distributed objectsDistributed objects
![Page 65: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/65.jpg)
Initial choiceInitial choice
Front-end: html formFront-end: html form
Back end: python cgiBack end: python cgi Python generates web page dynamically after making Python generates web page dynamically after making
calculationscalculations No use of higher-level web generation libraries at this pointNo use of higher-level web generation libraries at this point
What are advantages/disadvantages of this architecture?What are advantages/disadvantages of this architecture?
Run application: Run application: http://people.cs.uchicago.edu/~asiegel/courses/ace104/lesson1/examples/mortgage/http://people.cs.uchicago.edu/~asiegel/courses/ace104/lesson1/examples/mortgage/
![Page 66: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/66.jpg)
![Page 67: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/67.jpg)
![Page 68: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/68.jpg)
DisadvantagesDisadvantages
Two obvious disadvantages are:Two obvious disadvantages are: Formatted web content in print statements low-level, Formatted web content in print statements low-level,
ugly error proneugly error prone Data is not decoupled from formatting. What if we want Data is not decoupled from formatting. What if we want
to switch to an application client? What if we want to to switch to an application client? What if we want to allow further processing by the client?allow further processing by the client?
Several strategies can help with both of these Several strategies can help with both of these (higher-level htmlgen libraries, server-side (higher-level htmlgen libraries, server-side scripting model, beans, etc.) and XMLscripting model, beans, etc.) and XML
We will look at how XML fits in We will look at how XML fits in
![Page 69: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/69.jpg)
Key QuestionsKey Questions
What does browser do with XML?What does browser do with XML? Can it display?Can it display? Does it even understand XML?Does it even understand XML?
If not, what good is this?If not, what good is this?
Do we have to hand roll our programming Do we have to hand roll our programming language objects from XML?language objects from XML?
![Page 70: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/70.jpg)
Some answersSome answers
Regarding first point, try this with your web Regarding first point, try this with your web browserbrowser Note that XML is displayed/formatted nicely, but not Note that XML is displayed/formatted nicely, but not
nearly to the same level of utility as the html tablenearly to the same level of utility as the html table To transform to html, we must associate a To transform to html, we must associate a
separate separate .xsl.xsl file (e.g.) with the XML file. We will study file (e.g.) with the XML file. We will study XSL soon.XSL soon.
Regarding XML-language conversion, we will Regarding XML-language conversion, we will study language binding for various high-level study language binding for various high-level ways of doing this. For now, we will hand-roll ways of doing this. For now, we will hand-roll ourselves!ourselves!
![Page 71: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/71.jpg)
Lottery applicationLottery application
![Page 72: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/72.jpg)
Functional requirementsFunctional requirements
Given a list of student members of a Given a list of student members of a dormitory, perform an ordered randomized dormitory, perform an ordered randomized sort of the students to determine a room sort of the students to determine a room draft order.draft order.
![Page 73: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/73.jpg)
Functional requirements, cont.Functional requirements, cont.
Students are defined byStudents are defined by Last nameLast name First nameFirst name SenioritySeniority
Quarters in the HouseQuarters in the HouseQuarters in the CollegeQuarters in the College
The sort keys areThe sort keys are1.1. Quarters in HouseQuarters in House2.2. Quarters in CollegeQuarters in College3.3. RandomRandom
![Page 74: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/74.jpg)
Software requirementsSoftware requirements
Secure loginSecure login House nameHouse name PasswordPassword
Remotely accessible to single small companyRemotely accessible to single small company Several hits per hour maximumSeveral hits per hour maximum
All Windows UsersAll Windows Users
What I triedWhat I tried Excel embedded in IE6Excel embedded in IE6 Web-based with java servlets, XML, XSLTWeb-based with java servlets, XML, XSLT
![Page 75: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/75.jpg)
Sketch of XML/Servlet solutionSketch of XML/Servlet solution
XMLLoginInfo
XMLStudent
Data
filesystem
WebServer
login
lottery
WebClient
XML
XSLT
![Page 76: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/76.jpg)
Exercise 1Exercise 1
![Page 77: XML Programming](https://reader035.fdocuments.net/reader035/viewer/2022081418/568145aa550346895db2a1d5/html5/thumbnails/77.jpg)
An exercise1 solutionAn exercise1 solutionTravel Service
InternalRules
Engine
Security manager
WebClient html
WebClient
xml xml?
html
http
Complexity of marshaling/unmarshaling XMLHow to represent state
Ease of integration -- no proprietary issuesAbility to use Schema for validation
http