130 likes | 209 Views
Chapter 13 XML. 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?. XML. Extensible Markup Language. De facto data language. http://www.w3.org/TR/REC-XML.
E N D
Chapter 13XML 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?
XML • Extensible Markup Language. • De facto data language. 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. • Many XML parsers already available. • Strong XML support in the FCL. .
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">
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>
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
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. • FCL XmlDocument, managed DOM implementation • 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.
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. Examples\c13\Quotes\Quotes.xsl http://winserv1.cs.uakron.edu/Examples/C13/Quotes/quotes.aspx
XSL Transformations (XSLT) • Extensible Stylesheet Language Transformations. • Converting XML documents into HTML documents. • Converting HTML documents into other XML documents. Examples\c13\Transform\Transform.cs http://winserv1.cs.uakron.edu/Examples/C13/Transform/Quotes.html