XML e Xtensible Markup Language Extensible Markup Language
XML e. Xtensible Markup Language
Extensible Markup Language XML is another format (similar to JSON) that is meant to convey semi-structured data. HTML (used in webpages) is a subset of XML. Heavily used in web services and inter-database communications. Often used for text document formatting • Microsoft Office • Open. Office. org / Libre. Office • Apple's i. Work Also often used in configuration files.
XML Tags and Elements Tags come in 3 types: 1. start-tags (e. g. <section>) 2. end-tags (e. g. </section>) 3. empty-element tags (e. g. <section />) An element consist of a start-tag, optional content (which may be text or other elements), and a matching end-tag. Or, an element is just a empty-element tag. Example elements: • <head></head> • <h> Hi <p /></h> • <h />
XML Attributes are name/value pairs within a start-tag or empty-element tag. There can be only one value in each pair, so multiple values are combined (often as a space-delimited string). Example attributes: • <person name="josh"></person> • <work loc="BPS Engineering" />
Well-Formed XML The document contains only properly encoded legal Unicode characters The element tags are casesensitive; the beginning and end tags must match exactly. None of the special syntax characters such as < and & appear except when performing their markup-delineation roles Tag names cannot contain any of the characters !"#$%&'()*+, /; <=>? @[]^`{| }~, nor a space character, and cannot start with -, . , or a numeric digit. The begin, end, and empty-element tags that delimit the elements are correctly nested, with none missing and none overlapping A single "root" element contains all the other elements.
XML Internal Representation What data structures would you use to represent an XML document: Graph Tree Hash table List
DTD - Document Type Definition A DTD defines what is "valid" XML data for a particular purpose. It defines what are allowable tags and the grammar for how they can be used. Details can be found here: http: //www. w 3 schools. com/xml/ xml_dtd_intro. asp Special DTD components: • (#PCDATA) - Parsed character data - Text content that has special characters(<&) escaped. • CDATA - Unparsed character data • EMPTY - empty-element tag, no content allowed.
Valid XML <bookstore> <name>Josh's Store</name> <topic> <name>XML</name> <book isbn="123 -456 -789"> <title>Josh's Guide To DTD's and XML Schemas</title> <author>Josh</author> </book> </topic> </bookstore> Document Type Definition <!ELEMENT bookstore (name, topic+)> <!ELEMENT topic (name, book*)> <!ELEMENT name (#PCDATA)> <!ELEMENT book (title, author)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ATTLIST book isbn CDATA #REQUIRED>
XML Schemas are a much more powerful way to describe an XML documents structure and limitations. They are written in XML and can even have DTDs and XML schemas of their own. They provide a basic set of types that are more descriptive than PCDATA / CDATA. Detail can be found here: http: //www. w 3 schools. com/xml_sch ema. asp Basic types (integer, byte, string, floating-point number) can be combined into complex types that are used to define an element. Let's build an XML schema to match our previous bookstore example.
Valid XML <bookstore> <name>Josh's Store</name> <topic> <name>XML</name> <book isbn="123 -456 -789"> <title>Josh's Guide To DTD's and XML Schemas</title> <author>Josh</author> </book> </topic> </bookstore> XML Schema <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchem a"> <xsd: annotation> <xsd: documentation xlm: lang="en"> XML Schema for a Bookstore as an example. </xsd: documentation> </xsd: annotation>
Valid XML <bookstore> <name>Josh's Store</name> <topic> <name>XML</name> <book isbn="123 -456 -789"> <title>Josh's Guide To DTD's and XML Schemas</title> <author>Josh</author> </book> </topic> </bookstore> XML Schema. . . <xsd: element name="bookstore" type="bookstore. Type"/> <xsd: complex. Type name="bookstore. Type"> <xsd: sequence> <xsd: element name="name" type="xsd: string"/> <xsd: element name="topic" type="topic. Type" min. Occurs="1"/> </xsd: sequence> </xsd: complex. Type>
Valid XML <bookstore> <name>Josh's Store</name> <topic> <name>XML</name> <book isbn="123 -456 -789"> <title>Josh's Guide To DTD's and XML Schemas</title> <author>Josh</author> </book> </topic> </bookstore> XML Schema. . . <xsd: complex. Type name="topic. Type"> <xsd: element name="name" type="xsd: string"/> <xsd: element name="book" type="book. Type" min. Occurs="0"/> </xsd: complex. Type>
Valid XML <bookstore> <name>Josh's Store</name> <topic> <name>XML</name> <book isbn="123 -456 -789"> <title>Josh's Guide To DTD's and XML Schemas</title> <author>Josh</author> </book> </topic> </bookstore> XML Schema. . <xsd: complex. Type name="book. Type"> <xsd: element name="title" type="xsd: string"/> <xsd: element name="author" type="xsd: string"/> <xsd: attribute name="isbn" type="isbn. Type"/> </xsd: complex. Type> <xsd: simple. Type name="isbn. Type"> <xsd: restriction base="xsd: string"> <xsd: pattern value="[0 -9]{3}[-][0 -9]{3}"/> </xsd: restriction> </xsd: simple. Type>
Schema versus DTD When should you use an XML Schema instead of an XML DTD? Data Typing Validation Speed Occurrence Contraints Always
- Slides: 14