1 / 13

Chapter 13 XML

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.

leena
Download Presentation

Chapter 13 XML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13XML Yingcai Xiao

  2. 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?

  3. 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. .

  4. 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>

  5. 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">

  6. 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.

  7. 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>

  8. 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

  9. 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

  10. 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); } } }

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

  12. 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

  13. 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

More Related