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 / Schema Felaktigt: Ett dokument som varken är wellformed el valid 10 XML Namespaces XML Namespaces används för att särskillja element som annars skulle ha samma namn URLs används för att skapa unika prefix till namnrymderna URLen pekar normalt inte på något som kan hämtas, utan används för att kunna skapa ett globalt unikt prefix 11 1
<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 exempel (from www.xml.com) 12 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 2
Example of a notes XML document <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 XML Schema for notes <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 3
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 SOAP request example POST /InStock HTTP/1.1 Host: www.stock.org Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <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 4
SOAP response example HTTP/1.1 200 OK Content-Type: application/soap; charset=utf-8 Content-Length: nnn <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 5