XML and Data Structures for the Internet

advertisement
XML
&
Data Structures for the Internet
Yingcai Xiao
What is XML?
What is it for?
Examples
How to write?
How to validate?
How to read?
How to display?
How to format?
How to translate?
A Common Language for the
Internet (free of compilation and
translation)?
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 text
tags: type definition of text in text
<title>WP</title>
all tags are predefined in HTML
only system defined types, no user defined types
Recognizable by all types of computers. (World Wide Web)
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 Internet
Based on XML
WSDL (Web Service Description Language)
Standards for describing web services for the Internet
Based on XML
(XML) Web Services
Client 1
Proxy of Interface 2
UDDI Registry 2
SOAP
UDDI Registry 1
Client 2
Proxy of Interface 1
SOAP
Application 1
WSDL Interface 1
Application 2
WSDL Interface 2
WEB
A Web service is an application that exposes Web methods over the Web.
.
What is XML?
XML
.
 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 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>
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">
.
How to define an XML data structure?
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.
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>
.
How to validate XML data?
Example: Guitars.xsd
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
.
How to read XML data?
XML Parsers
 Most XML parsers implement one of two popular APIs: DOM
or SAX.
 DOM: Document Object Model http://www.w3.org/TR/DOMLevel-2-Core.
 SAX: Simple API for XML, unofficial,
http://www.saxproject.org.
 Examples of using XmlDocument:
Validate
ReadXml
XmlView
Transform
Quotes
ExpressAnalyzer
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);
}
}
}
XPath




XML Path Language
For addressing parts of an XML document.
“/Guitars/Guitar” is an XPath expression.
http://www.w3.org/TR/xpath.
.
How to display XML data with styles?
XSL
• 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/Quo
tes/quotes.aspx/
.
How to translate XML
into other languages?
XSL Transformations (XSLT)




Extensible Stylesheet Language Transformations.
Converting XML documents into HTML documents.
Converting HTML documents into other XML documents.
Example XML\Transform\Transform.cs
How to Program the Internet?
(Write code that runs on the Internet)
Web Service
&
Cloud Computing
Download