XML and Web Application Programming
Total Page:16
File Type:pdf, Size:1020Kb
XMLXML andand WebWeb ApplicationApplication ProgrammingProgramming Copyright © 2007 Anders Møller <[email protected]> OverviewOverview SchemaSchema languageslanguages forfor XMLXML XMLXML inin programmingprogramming languageslanguages WebWeb applicationapplication frameworksframeworks 2 PartPart II Schema languages for XML – what is the essence of DTD / XML Schema / RELAX NG ? • expressiveness of schema languages • formal models, variations of regular tree grammars • foundation for Part II 3 PartPart II Schema languages for XML – what is the essence of DTD / ““WW33CC XXMMLL SScchheemmaa isis oonnee sspepecc wwhheerere yyoouurr XML Schema / RELAX NG ? eeyyeess wwililll ssttiillll ttwwiittcchh aanndd yyoouurr hheeaadd ssttiillll ggoo bubuzzzzZZZZZZ eevveenn wwhheenn yyoouu rreeadad itit ffoorr tthhee tteenntthh ttimime•e. expressiveness. EExxcceeptpt tthhaatt yofyoo uschemau wwililll nn olanguageso lolonngegerr bebe ssuurprrpr• formalisiseedd by bymodels, ssuurprrpr variationsisiseess..”” of regular tree grammars • foundation for Part II ––MMiicchahaeell KKaayy,, eeddiittoorr ooff WW33CC’’ss XXSLSLTT 22..00 4 PartPart IIII XML in programming languages – how can we integrate XML processing in programming languages? • XML schemas as types in programming languages • research projects: –XDuce –XACT 5 PartPart IIIIII Web application frameworks – why is Web programming so complicated? • Java Servlets and JSP • JWIG • Google Web Toolkit 6 PartPart IIIIII »»WWooWebww ! !T Thhaannkk ss applicationf oforr r reeleleaassiningg a annooththeerr J Jaavv aaframeworks W Weebb a apppplilcicaatitoionn d deevveeloloppmmeennt t frfraamme–eww whyoorrkk. .T Thh eeis 3 31 1,9Web,91188 f rfraamm eprogrammingewwoorrkkss c cuurrreenntltyly a avvaailialabb lelsoe d doon n'complicated?t' to offeferreennoouugghh c chhooicicee.. YYoouu k knnooww, ,I I w waass j ujusst ts saayyiningg t oto m myy f rfrieienndd t hthee o oththeerr d daayy t hthaat tw wee r reeaalllyly d doo n noot th haavvee eennoouugghh J Jaavvaa w weebb d deevveeloloppmmeennt tf rfraammeewwoorrkkss. ." "WWhhyy,"," I I s saaidid t oto h himim, ," "iti'ts's b beeeenn t hthrreeee ddaayyss s si•ninc Javacee I I r reea adServletsd a abboouut ta a n nee wwand w weebb d JSPdeevveeloloppmmeennt tf rfraammeewwoorrkk o onn T TSSSS ! !IIss t hthee p paaccee oof fi ninnnoo•vvaatitoionn s slolowwiningg i nin t hthee J Jaavvaa w woorrldld?? A Arree w wee t hthatat f afarr a alolonngg t hthee r rooaadd t oto oobbssoolelesscceennJWIGccee ? ? C Canan d deeaathth b bee f afarr a awwaayy ? ?"" BBuut tn noow•w GoogleI I f efeeel lb beettet erWebr. .[ [...].] w whToolkitheenn I I s saaww t hthee A AJJAAXX w woorrdd I I k knneeww t hthaat tt hthee r ricichhnneessss a anndd aaggilielenneessss o of ft hthee e enndd--uusseerr e exxppeerrieiennccee w woouuldld b bee e exxcceeeeddeedd o onnlyly b byy t hthee u ussaabbiliiltiyty, , ssccaaleleababiliiltiyty, ,a anndd, ,m moosst ti mimppoorrtatannt to of fa alll,l ,t etesstatabbiliiltiyty o of fa apppplilcicaatitoionnss p prroodduucceedd b byy t hthee [[<<uusseelelessss t hthiningg n naammee>>]] . YYoouu a arree s soommee w wilidld a anndd c crraazzyy g guuyyss, ,a anndd I I, ,f oforr o onnee, ,a amm g gooiningg t oto n noommininaatete [ [<<uusseelelessss ththiningg n naammee>>]] f oforr t hthee W Weebb A Apppplilcicaatitoionn D Deevveeloloppmmeennt tFr Fraammeewwoorrkk o of ft hthee W Weeeekk. .N Noo, , II'm'm s seerrioiouuss ! !Y Yoouu'v'vee e eaarrnneedd i ti t! !S Statanndd u upp a anndd t atakkee a a b booww ! !« « ––oonn t heserverside.com aass a a c oommmeent tt oto a a totoool aannnnoouunnccemmennt theserverside.com c m n l e e t 7 ObjectivesObjectives These topics illustrate a few areas in (XML∪WWW) ∩ Programming Languages At different stages: • XML and schemas: now well-understood, essential to other areas of XML/WWW • XML programming: many proposals, (still) no “silver bullet” • Web programming: new frameworks appear frequently, needs consolidation Lots of remaining research opportunities! 8 PrerequisitesPrerequisites I assume that you have a basic knowledge of • Regular languages •Java (and ML) • WWW and XML/HTML 9 AA tourtour ofof schemaschema languageslanguages forfor XML:XML: DTDDTD,, XMLXML SchemaSchema,, andand RELAXRELAX NGNG OverviewOverview Motivation DTD XML Schema RELAX NG – focus on language constructs that are essential to expressiveness 11 Example:Example: XMLXML forfor businessbusiness cardscards <card><card> <name>John<name>John Doe</name>Doe</name> <title>CEO,<title>CEO, WidgetWidget Inc.</title>Inc.</title> <email>[email protected]</email><email>[email protected]</email> the contents <phone>(202)<phone>(202) 555-1414</phone>555-1414</phone> of the card <logo<logo uri="widget.gif"/>uri="widget.gif"/> element </card></card> cardcard XML document namename titletitle ... JohnJohn DoeDoe CEO,CEO, ...... XML tree An XML-based language is a set of XML documents (with some semantics) 12 SchemasSchemas forfor XMLXML Validation of XML documents: •A schema describes the syntax of an XML-based language •A schema language (DTD / XML Schema / RELAX NG / ...) is a formal notation for specifying schemas – like BNF for programming language syntax •A schema processor checks validity of a document, relative to a schema 13 ThisThis presentationpresentation isis basedbased on...on... Anders Møller and Michael Schwartzbach, An Introduction to XML and Web Technologies, Addison-Wesley, 2006 Makoto Murata, Dongwon Lee, Murali Mani, and Kohsuke Kawaguchi, Taxonomy of XML schema languages using formal language theory, ACM Trans. Internet Techn. 5(4): 660- 704, 2005 14 XMLXML inin programmingprogramming languageslanguages Many recent programming languages and APIs are designed for processing XML data schema ~ type Type checking? Type inference?? We need formal models of schemas and schema languages! 15 OverviewOverview Motivation DTD XML Schema RELAX NG 16 DTDDTD –– DocumentDocument TypeType DefinitionDefinition Introduced with XML 1.0 Focuses on elements and attributes Little control of attribute values and chardata No support for namespaces Widely used (still!) 17 ElementElement declarationsdeclarations <!ELEMENT<!ELEMENT element-nameelement-name content-model content-model >> where content-model is generally a restricted regular expression over element names and #PCDATA Observations: the content model of a particular element only depends on its name! content models must be deterministic limited control over chardata 18 AttributeAttribute declarationsdeclarations <!ATTLIST<!ATTLIST element-nameelement-name attribute-definitions attribute-definitions >> Each attribute definition consists of • an attribute name • an attribute type (CDATA, enum, ID, IDREF, ...) •a default declaration (#REQUIRED, #IMPLIED, ...) Observations: the requirements for a particular attribute only depends on its name and the name of the element! limited control over attribute values 19 ExampleExample <!ELEMENT<!ELEMENT cardcard (name(name,title,email,phone?,logo?)>,title,email,phone?,logo?)> <!ELEMENT<!ELEMENT namename (#PCDATA)>(#PCDATA)> <!ELEMENT<!ELEMENT titletitle (#PCDATA)>(#PCDATA)> <!ELEMENT<!ELEMENT emailemail (#PCDATA)>(#PCDATA)> <!ELEMENT<!ELEMENT phonephone (#PCDATA)>(#PCDATA)> <!ELEMENT<!ELEMENT logologo EMPTY>EMPTY> <!ATTLIST<!ATTLIST logologo uriuri CDATACDATA #REQUIRED>#REQUIRED> 20 GeneralGeneral observationsobservations DTD can only express “local” properties cardcard on XML trees! namename titletitle ... An XML document JohnJohn DoeDoe CEO,CEO, ...... can be validated by checking the element nodes in any order 21 OverviewOverview Motivation DTD XML Schema RELAX NG 22 XMLXML SchemaSchema W3C’s design goal: make “a better DTD”... Successes: • Namespace support • Modularization • Data types • Type derivation mechanism Critique: • lots... it’s an easy prey ☺ 23 TypesTypes andand declarationsdeclarations Simple type definition: defines a family of Unicode text strings (i.e. no markup) Complex type definition: defines a content and attribute model Element declaration: associates an element name with a simple or complex type Attribute declaration: associates an attribute name with a simple type 24 ExampleExample (1/2)(1/2) <schema<schema xmlns="http:/xmlns="http://www.w3.org/2001/XMLSchema"/www.w3.org/2001/XMLSchema" xmlns:b="http://businesscard.org"xmlns:b="http://businesscard.org" targetNamespace="httptargetNamespace="http://businesscard.org">://businesscard.org"> <element<element name="card"name="card" type="b:card_type"/>type="b:card_type"/> <element<element name="name"name="name" type="string"/>type="string"/> <element<element name="title"name="title" type="string"/>type="string"/> <element<element name="email"name="email" type="string"/>type="string"/> <element<element name="phone"name="phone" type="string"/>type="string"/> <element<element name="logo"name="logo" type="b:logo_type"/>type="b:logo_type"/> <attribute<attribute name="uri"name="uri" type="anyURI"/>type="anyURI"/> 25 ExampleExample (2/2)(2/2) <complexType<complexType name="card_type">name="card_type"> <sequence><sequence> <element<element ref="b:name"/>ref="b:name"/> <element<element ref="b:title"/>ref="b:title"/> <element<element ref="b:email"/>ref="b:email"/> <element<element ref="b:phone"ref="b:phone" minOccurs="0"/>minOccurs="0"/> <element<element ref="b:logo"ref="b:logo" minOccurs="0"/>minOccurs="0"/> </sequence></sequence> </complexType></complexType> <complexType<complexType name="logo_type">name="logo_type"> <attribute<attribute ref="b:uri"ref="b:uri" use="required"/>use="required"/>