XML & Data Structures for the Internet Yingcai Xiao.

24
XML & Data Structures for the Internet Yingcai Xiao

Transcript of XML & Data Structures for the Internet Yingcai Xiao.

Page 1: XML & Data Structures for the Internet Yingcai Xiao.

XML &

Data Structures for the Internet

Yingcai Xiao

Page 2: XML & Data Structures for the Internet Yingcai Xiao.

What is XML?What is it for?

ExamplesHow to write?

How to validate?How to read?

How to display?How to format?

How to translate?

Page 3: XML & Data Structures for the Internet Yingcai Xiao.

A Common Language for the Internet (free of compilation and

translation)?

Page 4: XML & Data Structures for the Internet Yingcai Xiao.

A Common Language for the Internet• Tim Berners-Lee• ASCII text (ISO/IEC 8859-1) is platform-independent. • HTTP (Hyper Text Transport Protocol)

e.g. GET wp.html

Assembly Language for the Internet• HTML (Hyper Text Markup Language)

High-level language for the Internet)hyper text: text that describes other texttags: type definition of text in text

<title>WP</title>all tags are predefined in HTMLonly system defined types, no user defined types

Recognizable by all types of computers. (World Wide Web)

Page 5: XML & Data Structures for the Internet Yingcai Xiao.

A Common Language for the Internet

XML (eXtensible Markup Language)Allow user defined tags (types)

SOAP (Simple Object Access Protocol)Standards for defining objects for the InternetBased on XML

WSDL (Web Service Description Language)Standards for describing web services for the InternetBased on XML

Page 6: XML & Data Structures for the Internet Yingcai Xiao.

(XML) Web Services

Proxy of Interface 2

Client 2

Application 1

UDDI Registry 1

WSDL Interface 1

UDDI Registry 2

Application 2 WSDL Interface 2

SOAP

SOAP

WEB

Proxy of Interface 1

Client 1

A Web service is an application that exposes Web methods over the Web.

Page 7: XML & Data Structures for the Internet Yingcai Xiao.

. What is XML?

Page 8: XML & Data Structures for the Internet Yingcai Xiao.

Extensible Markup Language. De facto data language for the Internet.

http://www.w3.org/TR/REC-XML. HTML expresses appearance;

XML describes data and its structure. Text based (platform-independent). Object-oriented data representation. Has no predefined tags. Provides rules to format data.

.

XML

Page 9: XML & Data Structures for the Internet Yingcai Xiao.

XML Example (Guitars.xml)<?xml version="1.0"?><Guitars> <Guitar> <Make>Gibson</Make> <Model>SG</Model> <Year>1977</Year> <Color>Tobacco Sunburst</Color> <Neck>Rosewood</Neck> </Guitar> <Guitar> <Make>Fender</Make> <Model>Stratocaster</Model> <Year></Year> <Color>Black</Color> <Neck>Maple</Neck> </Guitar></Guitars>

Page 10: XML & Data Structures for the Internet Yingcai Xiao.

XML Example (Guitars.xml) Document element (root): Guitars.

Guitar elements are children of Guitars.

Make contain data.

Empty element: <Year></Year>

Nested elements.

The content of an XML document can be viewed as a tree.

Attributes

<Guitar Year="1977">

Page 11: XML & Data Structures for the Internet Yingcai Xiao.

.

How to define an XML data structure?

Page 12: XML & Data Structures for the Internet Yingcai Xiao.

XML Description XML Elements: text-based object-oriented data (object) Error checking? Text-based object-oriented type (class) definition?

Early days: document type definitions (DTDs).

Today: XML Schema Definitions (XSDs). http://www.w3.org/TR/xmlschema-1 http://www.w3.org/TR/xmlschema-2.

Schema: a collection of meta data. Meta data: data that describes data. XSD is an XML-based language for describing XML

documents and the types that they contain.

Page 13: XML & Data Structures for the Internet Yingcai Xiao.

Example: Guitars.xsd<?xml version="1.0"?>

<xsd:schema id="Guitars" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="Guitars">

<xsd:complexType>

<xsd:choice maxOccurs="unbounded">

<xsd:element name="Guitar">

<xsd:complexType>

<xsd:sequence>

<xsd:element name="Make" type="xsd:string" />

<xsd:element name="Model" type="xsd:string" />

<xsd:element name="Year" type="xsd:Year“ minOccurs="0" />

<xsd:element name="Color" type="xsd:string“ minOccurs="0" />

<xsd:element name="Neck" type="xsd:string“ minOccurs="0" />

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:choice>

</xsd:complexType>

</xsd:element>

</xsd:schema>

Page 14: XML & Data Structures for the Internet Yingcai Xiao.

.

How to validate XML data?

Page 15: XML & Data Structures for the Internet Yingcai Xiao.

Examples\c13\Validate\

Typo => xsd:Year should be xsd:string

run.bat

Validate Guitars.xml Guitars.xsd

run-bad-xml.bat

Validate Guitars-Missing-Make.xml Guitars.xsd

Example: Guitars.xsd

Page 16: XML & Data Structures for the Internet Yingcai Xiao.

.

How to read XML data?

Page 17: XML & Data Structures for the Internet Yingcai Xiao.

XML Parsers Most XML parsers implement one of two popular APIs: DOM

or SAX. DOM: Document Object Model http://www.w3.org/TR/DOM-

Level-2-Core. SAX: Simple API for XML, unofficial,

http://www.saxproject.org. Examples of using XmlDocument:

Validate ReadXml

XmlViewTransformQuotesExpressAnalyzer

Page 18: XML & Data Structures for the Internet Yingcai Xiao.

ReadXml.cs (also see XmlView)using System;

using System.Xml;

class MyApp

{

static void Main ()

{

XmlDocument doc = new XmlDocument ();

doc.Load ("Guitars.xml");

XmlNodeList nodes = doc.GetElementsByTagName ("Guitar");

foreach (XmlNode node in nodes) {

Console.WriteLine(“Maker {0}; Model {1}", node["Make"].InnerText, node["Model"].InnerText);

}

}

}

Page 19: XML & Data Structures for the Internet Yingcai Xiao.

XML Path Language For addressing parts of an XML document. “/Guitars/Guitar” is an XPath expression. http://www.w3.org/TR/xpath.

XPath

Page 20: XML & Data Structures for the Internet Yingcai Xiao.

.

How to display XML data with styles?

Page 21: XML & Data Structures for the Internet Yingcai Xiao.

• XSL is a language for expressing style sheets. • Adopted from CSS, a file that describes how to

display an XML document of a given type. • Styling requires a source XML documents,

containing the information that the style sheet will display and the style sheet itself which describes how to display a document of a given type. It supports: Formatting Objects.

• It also adds a transformation language for XML documents: XSLT.

• Example: XML\Quotes\Quotes.xsl

http://winserv1.cs.uakron.edu/Examples/C13/Quotes/quotes.aspx/

XSL

Page 22: XML & Data Structures for the Internet Yingcai Xiao.

. How to translate XML into other languages?

Page 23: XML & Data Structures for the Internet Yingcai Xiao.

Extensible Stylesheet Language Transformations. Converting XML documents into HTML documents. Converting HTML documents into other XML documents. Example XML\Transform\Transform.cs

XSL Transformations (XSLT)

Page 24: XML & Data Structures for the Internet Yingcai Xiao.

How to Program the Internet?(Write code that runs on the Internet)

Web Service

&

Cloud Computing