XML 1 XML is a method for putting structured data in a text file Structured data examples: Application specific: Word document, Excel sheet Usage specific: Patient journal, Invoices XML provides rules for creating text-based formats for structured data that are / have: Unambigous Extensible Internationalization support Platform independence 2 1
XML looks a bit like HTML but isn't HTML Similarities: XML uses tags ( <tag> ), just like HTML XML uses attributes ( name=value) BUT HTML assigns meaning to each tag XML uses tags to delimit data, meaning is given by the application context 3 XML is a family of technologies XML 1.0 define concept of tag, attribute etc XSL extensible Stylesheet Language XSLT - A transformation language for rearranging, adding or deleting tags & attributes Xpath, XSL Formatting Objects Xlink adding hyperlinks to an XML file XPointer pointing to parts of an XML document DOM Document Object Model, a standard set of function calls for manipulting XML files RDF, XML Namespaces, XFragments, CSS... 4 2
XML is new, but not that new XML started in 1996 Standard since February 1998 The idea of markup languages is not new: SGML 70 s & 80 s HTML began 90 Easier to use than SGML, but not less powerful The design used the experiences from the large HTML userbase. 5 Element Elementnamn <DATUM>24 Aug 2001</DATUM> Start-tag Innehåll Slut-tag Element är den grundläggande byggstenen I ett XMLdokument 6 3
Element forts. Element måste innehålla både start-tag och slut-tag (Undantag: Ett tomt element) Element kan innehålla: Nästlade element Textdata Entitetsreferenser eller teckenreferenser CDATA sektioner Processing instructions Kommentarer 7 Attribut I start- eller i slut-taggen kan man inkludera ett eller flera attribut. Attribut är namn värde par som associeras till elementet. Exempel: <DATUM CALENDAR= Julian >24 Aug 2001</DATUM> Man kan sålunda representera information på två sätt: Som innehåll I ett element eller som värde för ett attribut. 8 4
DTD DTD (Document Type Declaration) definierar typen of strukturen på dokumentet. DTD kan ses som grammatik som bygger upp ett applikationsspråk. DTD kan specificera både generella språk som exvis XHTML och strikta deklarationer motsvarande databasschema. XML Schema är ett altentativ till DTD 9 Wellformed vs. Valid Ett XML-dokument kan vara antingen felaktigt, Wellformed eller Valid. Wellformed: Endast ett rotelement (dokumentelement) Elementen måste vara korrekt nästlade Alla element (utom tomma) måste ha både start- och slut-tag Valid: Ett well-formed dokument som uppfyller en given DTD Felaktigt: Ett dokument som varken är wellformed el valid 10 5
XML Namespaces XML Namespaces is used to disambiguate elements that would otherwise have the same name URLs are used to create unique namespace prefixes The URL does not really point at anything, just used to create globally unique prefix 11 <h:html xmlns:xdc="http://www.xml.com/books" xmlns:h="http://www.w3.org/html/1998/html4"> <h:head><h:title>book Review</h:title></h:head> <h:body> <xdc:bookreview> <xdc:title>xml: A Primer</xdc:title> <h:table> <h:tr align="center"> <h:td>author</h:td><h:td>price</h:td> <h:td>pages</h:td><h:td>date</h:td></h:tr> <h:tr align="left"> <h:td><xdc:author>simon Laurent</xdc:author></h:td> <h:td><xdc:price>31.98</xdc:price></h:td> <h:td><xdc:pages>352</xdc:pages></h:td> <h:td><xdc:date>1998/01</xdc:date></h:td> </h:tr> </h:table> </xdc:bookreview> </h:body> </h:html> Namespace example (from www.xml.com) 12 6
XML Schema XML Schema is another way of describing and constraining the content of XML documents (i.e grammars for creating a markup language + restrictions for values) More powerful than DTD, but also more complicated XML Schemas are written using XML 13 Example of a notes XML document (Example from www.w3schools.com) <?xml version="1.0"?> <note> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>don't forget me this weekend!</body> </note> DTD for notes <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> 14 7
XML Schema for notes (Example from www.w3schools.com) <?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:element name="note"> <xs:complextype> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complextype> </xs:element> </xs:schema> 15 SOAP Simple Object Access Protocol Sending messages between applications Based on XML: platform & language independent Uses HTTP GET and POST methods to transfer data: easy traversal through firewalls etc. 16 8
SOAP request example (Example from www.w3schools.com) POST /InStock HTTP/1.1 Host: www.stock.org Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <soap:envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingstyle="http://www.w3.org/2001/12/soap-encoding"> <soap:body xmlns:m="http://www.stock.org/stock"> <m:getstockprice> <m:stockname>ibm</m:stockname> </m:getstockprice> </soap:body> </soap:envelope> 17 SOAP response example (Example from www.w3schools.com) HTTP/1.1 200 OK Content-Type: application/soap; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <soap:envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingstyle="http://www.w3.org/2001/12/soap-encoding"> <soap:body xmlns:m="http://www.stock.org/stock"> <m:getstockpriceresponse> <m:price>34.5</m:price> </m:getstockpriceresponse> </soap:body> </soap:envelope> 18 9