XML Extensible Markup Language. What is XML? o XML stands for EXtensible Markup Language o XML is a markup language much like HTML o XML was designed

  • Published on
    11-Jan-2016

  • View
    212

  • Download
    0

Embed Size (px)

Transcript

<ul><li><p>XMLExtensible Markup Language</p></li><li><p>What is XML?XML stands for EXtensible Markup LanguageXML is a markup language much like HTMLXML was designed to describe dataXML tags are not predefined in XML. You must define your own tags.XML uses a Document Type Definition (DTD) or XML Schema to describe the dataXML with a DTD or XML Schema is designed to be self-descriptive</p></li><li><p>XML Vs HTMLXML was designed to carry data. XML is not a replacement for HTML.XML and HTML were designed with different goals:</p><p>XML was designed to describe data and to focus what data is.HTML was designed to display data and to focus on how data looks.</p><p>HTML is about displaying information, XML is about describing information.</p></li><li><p>XML was designed NOT to do anythingXML was created to structure, store, and to send information.Example of a note to Tove from Jani, stored as XML: ToveJaniRemainderDont forget me this weekend!</p><p>The note has a header, and a message body. It also has sender and receiver information. But still, this XML document does not do anything. Its just pureinformation wrapped in XML tags. Someone must write a piece of software to send, receive or display it.</p></li><li><p>XML is free and extensibleXML tags are not predefined. You must invent your own tags. The tags used to markup HTML documents and the structure of HTML documents are predefined. The author of HTML documents can only use tags that are defined in HTML standard (like, , etc.). XML allows the author to define his own tags and his own document structure. The tags like and in the example above are not defined in any XML standard. These tags are invented by the author of the XML document.</p></li><li><p>XML is a complement to HTMLXML is not a replacement for HTML. In the future Web development, it is most likely that XML will be used to describe the data, while HTML will be used to format and display the same data.</p><p>XML is a cross-platform, software and hardwareindependent tool for transmitting information. XML is expected to be as important to the future of the Web as HTML has been to the foundation of the Web and to be the most common tool for all data manipulation and data transmition.</p></li><li><p>XML was not designed to display data.It is important to understand that XML was designed to store, carry, and exchange data. </p></li><li><p>XML FeaturesXML is used to Exchange Data with XML, data can be exchanged between incompatible systems.XML can be used to Share Data with XML, plain text files can be used to share data.XML can be used to Store Data with XML, plain text files can be used to store data.XML can make your Data more Useful with XML, your data is available to more users. XML can be used to Create new Languages XML is the mother of WAP and WML The Wireless Markup Language (WML), used to markup Internet applications for handheld devices like mobile phones, is written in XML. </p></li><li><p>An example XML documentXML documents use a self-describing and simple syntax. ToveJaniRemainderDont forget me this weekend! The first line in the document- describes the root element of the document (like: this document is a note). </p></li><li><p>An example XML document (Cont.)The next 4 lines describe 4 child elements of the root (to, from, heading, and body): ToveJaniRemainderDont forget me this weekend! And finally the last line defines the end of the root element: .</p><p>From the example, dont you agree that XML is pretty self-descriptive?</p></li><li><p>TagsAll XML elements must have a closing tag With XML it is illegal to omit the closing tag.For example: this is a paragraph this is another paragraph XML Tags are case sensitive Unlike HTML, XML tags are case sensitive. The tag is different from the tag : This is incorrect This is correct </p></li><li><p>Properly NestedAll XML element must be properly nestedThis is not properly nestedThis is properly nestedImproper nesting of tags make no sense to XML. All XML document must have a root tagThe first tag in an XML document is a root tag.All XML documents must contain a single tag pair to define the root element. All other elements must be nested within the root element.</p></li><li><p>Properly Nested (Cont.)All elements can have sub elements (children). Sub elements must be correctly nested within their parent element: ...... </p></li><li><p>Attribute values must always be quotedWith XML, it is illegal to omit quotation marks around attribute values.For example: ToveJaniRemainderDont forget me this weekend! This is incorrect!The date attribute in note element is not quoted</p></li><li><p>Attribute values must always be quoted (Cont.)ToveJaniRemainderDont forget me this weekend! This is correct!</p><p>This is correct: date=12/11/02This is incorrect: date=12/11/02</p></li><li><p>Comments in XMLThe syntax for writing comments in XML is similar to that of HTML:</p></li><li><p>XML Elements are ExtensibleXML documents can be extended to carry more information.E.g.ToveJaniRemainderDont forget me this weekend!</p><p>Imagine if later on the author decided to add some extra information to it:</p></li><li><p>XML Elements are Extensible (Cont.)2002-10-12ToveJaniRemainderDont forget me this weekend! Should the application break or crash?No. The application should still be able to find the , , and elements in the XML document and produce the same output.</p><p>XML documents are Extensible!</p></li><li><p>XML Elements have RelationshipsElements are related as parents and children. To understand XML terminology, you have to know how relationships between XML elements are named, and how element content is described.</p></li><li><p>XML Elements have Relationships (Cont.)For Instance, this is the description of a book: Book title: My First XML Chapter 1: Introduction to XML What is HTML What is XML Chapter 2: XML Syntax Elements must have a closing tagsElements must be properly nested</p></li><li><p>XML Elements have Relationships (Cont.)Then, this is the XML document that describes the book: My First XML</p><p>Introduction to XMLWhat is HTMLWhat is XML</p><p>XML SyntaxElements must have a closing tagsElements must be properly nested</p></li><li><p>ExplanationBook is the root element</p><p>Title and chapter are child elements of book</p><p>Book is the parent element of the title and chapter</p><p>Title and chapter are siblings (or sister elements) because they have the same parent.</p></li><li><p>Elements have contentsElements can have different content typesAn XML element is everything from (including) the elements start tag to (including) elements end tag.An element can have element content, mixed contend, simple content, or empty content. An element can also have attributes.In the previous example, book has element content, because it contains other elements. Chapter has mixed contents because it contains both text and other elements. Para has simple content (or text content) because it contains only text. Prod has empty content, because it carries no information.</p></li><li><p>Elements have contents (Cont.)In that example, only the prod element has attributes. The attribute named id has the value 33-657. The attribute named media has the value paper.</p></li><li><p>Element NamingXML elements must follow these naming rules: Names can contain letters, numbers, and other characters.Names must not start with a number or punctuation character.Names must not start with the letters xml (or XML or Xml ) Any name can be used, no words are reserved, but the idea is to make names Descriptive-Names with an underscore separator are nice.Examples: , .</p></li><li><p>XML AttributesXML Elements can have attributes. Attributes are used to provide additional information about the elements. Attributes often provide information that is not a part of the data. For example:computer.gif In the example, the file type is irrelevant to the data, but important to the software that wants to manipulate the element.</p></li><li><p>Quote StylesAttribute values must always be enclosed in quotes, but either double or single quote can be used.E.g. or Double quotes are the most common, but sometimes (if the attribute value itself contains quotes) it is necessary to use single quotes.</p></li><li><p>Elements Vs AttributesE.g.1: Anna Smith E.g.2: female Anna Smith </p><p>In the first example, sex is an attribute. In the last, sex is a child element. Both examples provide the same information.There are no rules about when to use attributes and when to use elements. </p></li><li><p>Should we avoid using attributes?Some problem of using attributes are: - Attributes cant contain multiple values (child element can) - Attributes are not easily extendable (for future changes) - Attributes cant describe structure (child elements can) - Attributes are more difficult to manipulate by program code - Attribute values are not easy to test against a DTD Use attributes only to provide information that is not relevant to the data! Dont end up like this: </p></li><li><p>MetadataMetadata (or data about data) should be stored as attributes, and the data itself should be stored as elements.For example: Tove Jani Remainder Dont forget me this weekend! Jani Tove Re: Remainder I will not! </p></li><li><p>Metadata (Cont.)The ID in the previous example is just a counter, or a unique identifier, to identify the different notes in the XML file, and not a part of the note data.</p></li><li><p>XML DTDA DTD defines the legal elements of an XML elements.</p><p>The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements.</p><p>A DTD can be declared inline in your XML document, or as an external reference.</p></li><li><p>Internal DOCTYPE DeclarationIf the DTD is included in your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax: Example XML document with a DTD: ]&gt; Tove Jani Remainder Dont forget me this weekend </p></li><li><p>DTD Interpretation!DOCTYPE note (in line 2) defines that this is a document of the type note.</p><p>!ELEMENT note (in line 3) defines the note element as having four elements: to, from, heading, body. </p><p>!ELEMENT to (in line 4) defines the to element to be of type #PCDATA.</p><p>!ELEMENT from (in line 5) defines the from element to be of type PCDATA.</p><p>And so on . . .</p></li><li><p>External DOCTYPE declarationIf the DTD is external to your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax:</p><p>E.g. : Tove Jani Remainder Dont forget me this weekend! </p></li><li><p>note.dtdAnd then the copy of the file note.dtd will be like this:</p></li><li><p>Why use a DTD?With DTD, each of your XML file can carry a description of its own format with it. With a DTD, independent groups of people can agree to use a common DTD for interchanging data. Your application can use a standard DTD to verify that the data you received from the outside world is valid. You can also use DTD to verify your own data.</p></li><li><p>DTD-XML building blocksThe building blocks of XML documentsSeen from a DTD point of view, all XML documents are made up by following simple building blocks: - Elements - Tags - Attributes - Entities - PCDATA - CDATA</p></li><li><p>DTD-ElementsIn a DTD, XML elements are declared with a DTD element declaration. Declaring an element An element declaration has the following syntax: OR Empty elementsEmpty elements are declared with the category keyword EMPTY: example:in XML:</p></li><li><p>DTD-Elements (Cont.)Elements with only character data Elements with only character data are declared with #PCDATA inside parentheses: example: </p><p>Elements with any contents Elements declared with category keyword ANY, can contain any combination of parsable data: example: </p></li><li><p>DTD-Elements (Cont.)Elements with children (sequences) Elements with one or more children are defined with the name of the children elements inside parentheses: When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. In a full declaration, the children must also be declared, and the children can also have children.</p></li><li><p>DTD-Elements (Cont.)The full declaration of the note element will be </p><p>Declaring only one occurrence of the same element example: The + sign in the example declares that the child element message can occur one or more times inside the note element.</p></li><li><p>DTD-Elements (Cont.)Declaring zero or more occurrences of the sameelement example: The * sign in the example above declares that the child element message can occur zero or more times inside the element note element.</p></li><li><p>DTD-Elements (Cont.)Declaring zero or one occurrences of the same element example: The ? sign in the example above declares that the child element message can occur zero or one times inside the note element.</p></li><li><p>DTD-Elements (Cont.)Declaring either/or content Example: The example declares that the note element must contain a to element, a from element, a header element, and either a message or a body element.</p></li><li><p>DTD-AttributesIn a DTD, Attributes are declared with an ATTLIST declaration.</p><p>Declaring AttributesAn attribute declaration has the following syntax: DTD example: XML example: </p></li><li><p>DTD-Attributes (Cont.)The attribute-type can have the following values: ValueExplanation CDATAThe value is character data (en1|en2|..)The value must be one from an enumerated list IDThe value is a unique ID IDREFThe value is the ID of another element IDREFSThe value is a list of other ids NMTOKENThe value is a valid XML name NMTOKENSThe value is a list of valid XML names ENTITYThe value is an entity ENTITIESThe value is a list of entities NOTATIONThe value is a name of a notation xml:The value is a predefined xml value</p></li><li><p>DTD-Attributes (Cont.)The default-value can have the following values: ValueExplanation valueThe default value of the attribute #DEFAULT valueThe default value of the attribute #REQUIREDThe attribute value must be included in the element #IMPLIEDThe attribute does not have to be included #FIXED valueThe attribute value is fixed </p></li><li><p>Attribute Declaration ExampleDTD example: </p><p>XML example: The square element is defined to be an empty element with a width attribute of type CDATA. If no width specified, it has a default value of 0.</p></li><li><p>Default Attribute ValueSyntax:</p><p>DTD example:</p><p>XML example:</p><p>Specifying a default value for an attribute ensures that the attribute will get a value even if the author of the XML document does not include it.</p></li><li><p>Implied AttributeSyntax:</p><p>DTD example:</p><p>XML example: </p><p>Use implied attribute if you dont want to force the author to include an attribute, and you dont have an option for default value.</p></li><li><p>Required AttributeSyntax:</p><p>DTD example:</p><p>XML example: </p><p>Use a required attribute if you dont have an option for a default value, but still want to force the attribute to be present.</p></li><li><p>Fixed Attribute ValueSyntax:</p><p>DTD example: </p><p>XML example:</p><p>Use a fixed attribute value when you want an attribute to have a fixed value without allowing the author to change it. If an author includes another value, the XML parser will return an error.</p></li><li><p>Enumerated Attribute ValuesSyntax:</p><p>DTD example: </p><p>XML example: or</p><p>Use enumerated attribute values when you want the attribute values to be one of a fixed set of legal values.</p></li><li><p>DTD-EntitiesEntities are variables used to define shortcuts to common text. Entity references are references to entities.Entity can be declared internal or external. Internal Entity DeclarationSyntax:DTD exam...</p></li></ul>

Recommended

View more >