Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fast Train Computer School

Similar presentations


Presentation on theme: "Fast Train Computer School"— Presentation transcript:

1 Fast Train Computer School
XML and Web Services XML and Web Services Lesson 1 Jin X. Liu, Ph.D. Fast Train Computer School Redistribution of this notes is not allowed

2 About This Course This course focuses on the protocols on which the Web service platform is based. The Building section, which discusses the implementation of Web services using the latest tools. The topics in this course cover: Fundamental XML technologies Basic Web service standards Advanced Web services specifications. Case Studies XML and Web Services Jin X. Liu

3 XML and Web Services Reference Books Web Services Enhancements: Understanding the WSE for .NET Enterprise Applications;Bill Evjen,ISBN: NET Web Services: Architecture and Implementation;Keith Ballinger;ISBN: Real World XML Web Services: For VB and VB .NET Developers;Yasser Shohoud;ISBN: Building XML Web Services for the Microsoft.NET Platform;Author Scott Short Pages464 XML and Web Services Jin X. Liu Redistribution of this notes is not allowed

4 XML Fundamentals The core Extensible Markup Language (XML) technologies Web services are built on XML Concepts: XML Information Set XML 1.0 XML namespaces XML schema. XML technologies will allow you to: Read Web Services Description Language (WSDL) definitions Interpret SOAP message traces Accomplish a myriad of other tasks you may encounter when building Web services.    XML and Web Services Jin X. Liu

5 Where Is XML From? The Extensible Markup Language (XML) was originally envisioned as a language for defining new document formats for the World Wide Web. XML is derived from the Standard Generalized Markup Language (SGML), and can be considered to be a meta-language: a language for defining markup languages. SGML and XML are text-based formats that provide mechanisms for describing document structures using markup tags (words surrounded by '<' and '>'). There are some similarities between HTML and XML, due to that they are both derived from SGML. XML and Web Services Jin X. Liu

6 Why XML? Pre-existing formats such as comma separated value (CSV) files work well for tabular data and handle semi-structured data poorly RTF are too specialized for semi-structured text documents. XML is useful for both: Describing new document formats for the Web Describing structured data, such as information typically contained in spreadsheets, program configuration files, and network protocols. XML can easily represent both: Tabular data (relational data from a database or spreadsheets) Semi-structured data (Web page or business document). XML and Web Services Jin X. Liu

7 Benefit of XML In addition to represent both structured and semi-structured data, XML has a number of characteristics that have caused it to be widely adopted as a data representation format. XML is extensible, platform-independent, and supports internationalization by being fully Unicode compliant. XML is a text-based format. One can read and edit XML documents using standard text editor. XML, unlike HTML, does not have a fixed vocabulary. One can define vocabularies specific to particular applications or industries using XML. Applications that process or consume XML formats are more resistant to changes in the structure of the XML being provided to them than applications that use other formats, as long as such changes are additive. XML and Web Services Jin X. Liu

8 Benefit of XML… XML is not tied to any programming language, operating system or software vendor. Platform independence makes XML very useful as a means for achieving interoperability between different programming platforms and operating systems. Business documents, databases and inter-business communication are all examples of information sources that are moving or have moved to using XML as a representation format. Microsoft products such as Microsoft Office, Microsoft SQL Server and the Microsoft .NET Framework enable end users and developers to produce and consume documents, network messages and other data as XML. XML and Web Services Jin X. Liu

9 XML and HTML Compared Both HTML and XML documents are made up of elements Each element consists of a "start tag" and an "end tag“, and the information between the two tags Example: <order> Fry Chicken </order>) Elements can be annotated with attributes that contain metadata about the element and its contents. However, there are significant differences between HTML and XML XML is case sensitive while HTML is not. Example: In XML the start tags <Table> and <table> are different In HTML they are the same XML must be well-formed. Example: All attribute values must be in quotes like <name, id = “23”> Steve Smith </name> All elements must have a start tag and end tag <p> this is a paragraph </p> explicitly indicate that they are empty elements <p/> XML and Web Services Jin X. Liu

10 XML and HTML Compared… The most significant difference between HTML and XML is: HTML has predefined elements and attributes whose behavior is well specified XML does not. Instead, document authors can create their own XML vocabularies that are specific to their application or business needs. XML vocabularies currently exist for a large number of industries and applications: Financial filings (XBRL) and financial services (FpML) Web documents (XHTML) Network protocols (SOAP) The separation of content from presentation enabled by XML vocabularies allows for greater reuse of information and content repurposing. XML and Web Services Jin X. Liu

11 XML and Web Services A Sample XML Document Below is a sample XML document that represents a customer order for a music store <?xml version="1.0" encoding="iso " ?> <?xml-stylesheet href="orders.xsl"?> <order id="ord123456"> <customer id="cust0921"> <first-name>Dare</first-name> <last-name>Obasanjo</last-name> <address> <street>One Microsoft Way</street> <city>Redmond</city> <state>WA</state> <zip>98052</zip> </address> </customer> <items> <compact-disc> <price>16.95</price> <artist>Nelly</artist> <title>Nellyville</title> </compact-disc> <price>17.55</price> <artist>Baby D</artist> <title>Lil Chopper Toy</title> </items> <!-- Always go the extra mile for the customer --> <special-instructions xmlns:html=" <html:p> If customer is not available at the address then attempt leave package at one of the following locations listed in order of which should be attempted first <html:ol> <html:li>Next Door</html:li> <html:li>Front Desk</html:li> <html:li>On Doorstep</html:li> </html:ol> <html:b>Note</html:b> Remember to leave a note detailing where to pick up the package. </html:p> </special-instructions> </order> <?xml version="1.0" encoding="iso " ?> <?xml-stylesheet href="orders.xsl"?> <order id="ord123456"> <customer id="cust0921"> <first-name>Dare</first-name> <last-name>Obasanjo</last-name> <address> <street>One Microsoft Way</street> <city>Redmond</city> <state>WA</state> <zip>98052</zip> </address> </customer> <items> <compact-disc> <price>16.95</price> <artist>Nelly</artist> <title>Nellyville</title> </compact-disc> <price>17.55</price> <artist>Baby D</artist> <title>Lil Chopper Toy</title> </items> <!-- Always go the extra mile for the customer --> <special-instructions xmlns:html=" <html:p> If customer is not available at the address then attempt leave package at one of the following locations listed in order of which should be attempted first <html:ol> <html:li>Next Door</html:li> <html:li>Front Desk</html:li> <html:li>On Doorstep</html:li> </html:ol> <html:b>Note</html:b> Remember to leave a note detailing where to pick up the package. </html:p> </special-instructions> </order> XML and Web Services Jin X. Liu Redistribution of this notes is not allowed

12 The Above XML Document…
XML and Web Services The Above XML Document… The document begins with the optional XML declaration that specifies the version of XML and character encoding used by the document The second line is the xml-stylesheet processing instruction, used to bind a style sheet containing formatting instructions to the XML document. Applications used for displaying XML documents. (e.g. Web browser) would use the information in the processing instruction to locate the style sheet that contains special instructions for displaying document. Being based on Unicode makes XML suitable for sharing information across global networks (e.g.WWW). XML and Web Services Jin X. Liu Redistribution of this notes is not allowed

13 The XML Infoset The XML Infoset is a set of definitions used by XML technologies to formally describe what parts of an XML document they operate upon. SOAP 1.2 XML Schema XQuery. The XML Infoset is a tree-based hierarchical representation of an XML document. An XML document's information set consists of a number of information items, which are abstract representations of the components of an XML document. XML and Web Services Jin X. Liu

14 The XML Infoset… These Information items are defined in the infoset:
the document its elements Attributes processing instructions Comments Characters Notations Namespaces unparsed entities unexpanded entity references the document type declaration XML and Web Services Jin X. Liu

15 The XML Infoset… The XML Infoset is an official attempt to define what should be considered to be significant information in an XML document. For example, the infoset does not distinguish between the two forms of empty element. <test></test> <test/> are equivalent according to the XML Infoset. The quotation marks used for attributes are not considered significant <test attr='value'/> <test attr="value"/> are equivalent according to the XML Infoset. The XML Information Set describes the concept of synthetic infosets. Infosets that are created by other means besides parsing a textual XML document. Synthetic infosets pave the way for processing non-XML data using XML technologies as long as this data can be mapped to an XML Infoset. An example of processing a synthetic infoset is the ObjectXPathNavigator which enables one to query objects in the .NET Framework using XPath or transform them using XSLT. XML and Web Services Jin X. Liu

16 Schema Languages An XML schema language:
To describe the structure of an XML document To describe content of an XML document Example: a schema can be used to specify a document that consists of one or more compact-disc elements which each contain a price, title, and artist element as children. During document interchange, an XML schema describes the contract between the producer and consumer of XML. XML and Web Services Jin X. Liu

17 A schema language-XSD A schema language that currently rules the roost is the W3C XML Schema Definition Language typically abbreviated as XSD. XSD is unique among XML schema languages. XSD introduces the concept of a Post Schema Validation Infoset (PSVI). A conformant XSD processor accepts an XML Infoset as input and transforms it into a Post Schema Validation Infoset (PSVI) upon validation. One important contribution of PSVI is type annotations. Elements and attributes become strongly typed and have datatype information associated with them. Such strongly-typed XML can now be: Mapped to objects using .NET Framework's XmlSerializer Mapped to relational tables using SQLXML and the .NET Framework's DataSet Processed using XML query languages with strong typing, such as XPath 2.0 and XQuery. XML and Web Services Jin X. Liu

18 Sample Schema Describing Items in The XML Document on Slide 11
XML and Web Services Sample Schema Describing Items in The XML Document on Slide 11 <xs:schema xmlns:xs=" <xs:element name="items"> <xs:complexType> <xs:sequence> <xs:element ref="compact-disc" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="compact-disc"> <xs:element name="price" type="xs:decimal" /> <xs:element name="artist" type="xs:string" /> <xs:element name="title" type="xs:string" /> </xs:schema> <xs:schema xmlns:xs=" <xs:element name="items"> <xs:complexType> <xs:sequence> <xs:element ref="compact-disc" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="compact-disc"> <xs:element name="price" type="xs:decimal" /> <xs:element name="artist" type="xs:string" /> <xs:element name="title" type="xs:string" /> </xs:schema> XML and Web Services Jin X. Liu Redistribution of this notes is not allowed

19 Tree Model-Based APIs An API exposes an XML document as a tree of nodes, which is loaded in memory all at once. The most popular tree model API for XML is the Document Object Model (DOM). The DOM enables: programmatic reading of an XML document Manipulation of an XML document modification of an XML document XML and Web Services Jin X. Liu

20 Example of using the XmlDocument class in the
Example of using the XmlDocument class in the .NET Framework to obtain the artist name and title of the first compact-disc in an items element using System; using System.Xml; public class Test { public static void Main(string[] args) XmlDocument doc = new XmlDocument(); doc.Load("test.xml"); XmlElement firstCD = (XmlElement) doc.DocumentElement.FirstChild; XmlElement artist = (XmlElement) firstCD.GetElementsByTagName("artist")[0]; XmlElement title = (XmlElement) firstCD.GetElementsByTagName("title")[0]; Console.WriteLine("Artist={0}, Title={1}", artist.InnerText, title.InnerText); } XML and Web Services Jin X. Liu

21 Cursor-Based APIs An XML cursor API is a lens that moves across the XML document focusing on distinct aspects of document as directed. The XPathNavigator class in the .NET Framework is an example of an XML cursor API. The advantage of XML cursor APIs over tree model APIs: It is not required for the entire XML document to be in-memory This opens the door for optimizations on the part of producer of the XML The document is produced on an "as needed" basis. XML and Web Services Jin X. Liu

22 XML and Web Services Example of using XPathNavigator class in the .NET Framework to obtain the artist name and title of the first compact-disc in an items element using System; using System.Xml; using System.Xml.XPath; public class Test { public static void Main(string[] args) XmlDocument doc = new XmlDocument(); doc.Load("test.xml"); XPathNavigator nav = doc.CreateNavigator(); nav.MoveToFirstChild(); //move from root node to document element (items) nav.MoveToFirstChild(); //move from items element to first compact-disc element //move from compact-disc element to artist element nav.MoveToFirstChild(); nav.MoveToNext(); string artist = nav.Value; //move from artist element to title element string title = nav.Value; Console.WriteLine("Artist={0}, Title={1}", artist, title); } using System; using System.Xml; using System.Xml.XPath; public class Test { public static void Main(string[] args) XmlDocument doc = new XmlDocument(); doc.Load("test.xml"); XPathNavigator nav = doc.CreateNavigator(); nav.MoveToFirstChild(); //move from root node to document element (items) nav.MoveToFirstChild(); //move from items element to first compact-disc element //move from compact-disc element to artist element nav.MoveToFirstChild(); nav.MoveToNext(); string artist = nav.Value; //move from artist element to title element string title = nav.Value; Console.WriteLine("Artist={0}, Title={1}", artist, title); } XML and Web Services Jin X. Liu Redistribution of this notes is not allowed

23 Streaming APIs A streaming API for processing XML enables one to process an XML document without storing much more than the context of the current node being processed in memory. Such APIs make it possible to process large XML files without incurring an unbearably large memory footprint. There are two main classes of streaming APIs for XML processing: push-based XML parsers and pull-based XML parsers. Push-based parsers such as SAX work by moving across an XML stream then "pushing" events to registered event handlers (callback methods) when XML nodes are encountered. Pull-based parsers such as the XmlReader class in the .NET Framework act as forward-only cursors over an XML stream. XML and Web Services Jin X. Liu

24 XML and Web Services Example of using the XmlReader class in the .NET Framework to obtain the artist name and title of the first compact-disc in an items element using System; using System.Xml; public class Test { public static void Main(string[] args) string artist = null, title = null; XmlTextReader reader = new XmlTextReader("test.xml"); reader.MoveToContent(); //move from root node to element (items) // keep reading until we get to the first <artist> element while(reader.Read()) if((reader.NodeType == XmlNodeType.Element) && reader.Name.Equals("artist")) artist = reader.ReadElementString(); title = reader.ReadElementString(); break; } Console.WriteLine("Artist={0}, Title={1}", artist, title); using System; using System.Xml; public class Test { public static void Main(string[] args) string artist = null, title = null; XmlTextReader reader = new XmlTextReader("test.xml"); reader.MoveToContent(); //move from root node to element (items) // keep reading until we get to the first <artist> element while(reader.Read()) if((reader.NodeType == XmlNodeType.Element) && reader.Name.Equals("artist")) artist = reader.ReadElementString(); title = reader.ReadElementString(); break; } Console.WriteLine("Artist={0}, Title={1}", artist, title); XML and Web Services Jin X. Liu Redistribution of this notes is not allowed

25 XML Query In some cases, attempting to extract information from an XML document using an API may be too cumbersome because the criteria for finding the data are non-trivial because the API fails to expose certain aspects of an XML document that are amenable to certain queries. XML query languages such as XPath 1.0 and the forthcoming XQuery provide rich mechanisms for extracting information from XML infosets. XML and Web Services Jin X. Liu

26 Example showing how to use XPath to obtain the artist name and title of the first compact-disc in an items element using System; using System.Xml.XPath; public class Test { public static void Main(string[] args) XPathDocument doc = new XPathDocument("test.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator iterator = nav.Select("/items/compact-disc[1]/artist | /items/compact-disc[1]/title"); iterator.MoveNext(); Console.WriteLine("Artist={0}", iterator.Current); Console.WriteLine("Title={0}", iterator.Current); } XML and Web Services Jin X. Liu

27 XML Transformation There is often a need to transform XML documents from one vocabulary to another. Sometimes this is so they can be rendered in a print-friendly format or in a Web browser; it may be to convert documents received from an external entity to a format with which one is more familiar. XSLT is the premiere XML transformation language. A transformation expressed in XSLT describes rules for transforming a source tree into a result tree. The transformation is achieved by associating patterns with templates. A pattern is an XPath expression, and can be thought of as a regular expression that matches parts of an XML source tree as opposed to matching parts of a string. A pattern is matched against elements in the source tree. On successful matches, a template is instantiated to create part of the result tree. In constructing the result tree, elements from the source tree can be filtered and reordered, and arbitrary structure can be added. XML and Web Services Jin X. Liu

28 XSLT style sheet converts an items element into an XHTML Web page containing a table of compact disc information. <xsl:stylesheet xmlns:xsl=" version="1.0" xmlns=" <xsl:output method="xml" indent="yes" doctype-system=" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" /> <xsl:template match="/"> <html lang="en" xml:lang="en"> <head> <title>Order Information - ord123456</title> </head> <body> <table border="1"> <tr> <th>Artist</th><th>Title</th><th>Price</th> </tr> <xsl:for-each select="items/compact-disc"> <td><xsl:value-of xmlns="" select="artist" /></td> <td><xsl:value-of xmlns="" select="title" /></td> <td><xsl:value-of xmlns="" select="price" /></td> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> XML and Web Services Jin X. Liu

29 The XHTML document produced by the above stylesheet
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " <html lang="en" xml:lang="en" xmlns=" <head> <title>Order Information - ord123456</title> </head> <body> <table border="1"> <tr> <th>Artist</th> <th>Title</th> <th>Price</th> </tr> <tr> <td>Nelly</td> <td>Nellyville</td> <td>16.95</td> </tr> <td>Baby D</td> <td>Lil Chopper Toy</td> <td>17.55</td> </table> </body> </html> XML and Web Services Jin X. Liu

30 The html document will be displayed in a Web browser as…
XML and Web Services Jin X. Liu

31 Summary XML is more than a text format for describing documents.
XML is a mechanism for describing structured and semi-structured data, provides access to a rich family of technologies for processing such data. Abstractions of XML Information Set open the door to processing non-textual data (e.g. file systems, Windows registry, relational databases and programming language objects. XML brings us closer to universal data access. XML and Web Services Jin X. Liu


Download ppt "Fast Train Computer School"

Similar presentations


Ads by Google