XML

download XML

of 89

description

XML Introduction

Transcript of XML

XML

eXtensible Markup Language

XMLIntroductionXML stands for EXtensible Markup LanguageXML is text based markup language.XML is a markup language much like HTMLXML was designed to carry data, not to display dataXML tags are not predefined. You must define your own tagsXML is designed to be self-descriptiveXML is a W3C RecommendationAn XML document resides in its own file with an .xml extension.

The Difference between XML and HTMLXML is not a replacement for HTML.XML and HTML were designed with different goals:XML was designed to transport and store data, with focus on what data isHTML was designed to display data, with focus on how data looksHTML is about displaying information, while XML is about carrying information.

Though HTML and XML are part of SGML, there are some important differences:HTML is for humans HTML describes web pages XML is for computers XML describes data Unlike HTML, XML is case sensitive. For example, and are different XMLtags.In HTML, you can omit end tags such as or tags if it is clear from the context where a paragraph or list item ends. In XML, you can never omit an end tag.

In XML, elements that have a single tag without a matching end tag must end in a /, as in . That way, the parser knows not to look for a tag.In XML, attribute values must be enclosed in quotation marks. In HTML, quotation marks are optional. For example, is legal HTML but not legal XML. In XML, you have to use quotation marks: width="300".

In HTML, you can have attribute names without values. For example, . In XML, all attributes must have values, such as checked="true" or (ugh) checked="checked".

With XML You Invent Your Own Tagsthe XML language has no predefined tags.The tags used in HTML are predefined. HTML documents can only use tags defined in the HTML standard (like , , etc.).XML allows the author to define his/her own tags and his/her own document structure.XML is used in many aspects of web development, often to simplify data storage and sharing.

XML Separates Data from HTMLIf you need to display dynamic data in your HTML document, it will take a lot of work to edit the HTML each time the data changes.With XML, data can be stored in separate XML files. This way you can concentrate on using HTML/CSS for display and layout, and be sure that changes in the underlying data will not require any changes to the HTML.

XML Simplifies Data SharingXML data is stored in plain text format. This provides a software- and hardware-independent way of storing data.This makes it much easier to create data that can be shared by different applications.

XML Simplifies Data TransportOne of the most time-consuming challenges for developers is to exchange data between incompatible systems over the Internet.Exchanging data as XML greatly reduces this complexity, since the data can be read by different incompatible applications.

XML Simplifies Platform ChangesXML data is stored in text format. This makes it easier to expand or upgrade to new operating systems, new applications, or new browsers, without losing data.

XML Makes Your Data More AvailableDifferent applications can access your data, not only in HTML pages, but also from XML data sources.

XML is used to Create New Internet LanguagesXHTMLWSDL for describing available web servicesWAP and WML as markup languages for handheld devicesRSS languages for news feeds

XML Documents Form a Tree StructureXML documents must contain aroot element. This element is "the parent" of all other elements.The elements in an XML document form a document tree. The tree starts at the root and branches to the lowest level of the tree.All elements can have sub elements (child elements):

.....

The terms parent, child, and sibling are used to describe the relationships between elements. Parent elements have children. Children on the same level are called siblings (brothers or sisters).All elements can have text content and attributes (just like in HTML).

XMLSyntax RulesAll XML Elements Must Have a Closing TagXML Tags are Case SensitiveXML Elements Must be Properly NestedXML Documents Must Have a Root ElementXML Attribute Values Must be QuotedComments in XML:

Identify Root element, child element, sub child elements and attributes in following XML file. Also draw the tree structure for the same.

Everyday ItalianGiada De Laurentiis200530.00Harry PotterJ K. Rowling200529.99Learning XMLErik T. Ray200339.95

XML - DocumentsThe following image depicts the parts of XML document. Document Prolog Section Tanmay Patil ABC Corp Document (011) 123-4567 Element Document Prolog Section: Thedocument prologcomes at the top of the document, before the root element. This section contains:XML declarationDocument type declarationXML declaration contains details that prepare an XML processor to parse the XML document. It is optional, but when used, it must appear in first line of the XML document.An XML declaration should abide with the following rules:If the XML declaration is present in the XML, it must be placed as the first line in the XML document.If the XML declaration is included, it must contain version number attribute.The Parameter names and values are case-sensitive.The names are always in lower case.The order of placing the parameters is important. The correct order is:version, encoding and standalone.Either single or double quotes may be used.The XML declaration has no closing tag i.e. Document Elementsare the building blocks of XML. These divide the document into a hierarchy of sections, each serving a specific purpose. You can separate a document into multiple sections so that they can be rendered differently, or used by a search engine. The elements can be containers, with a combination of text and other elements.

XML - ElementsXML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these.Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag. ....content element-nameis the name of the element. Thenameits case in the start and end tags must match.attribute1, attribute2are attributes of the element separated by white spaces. An attribute defines a property of the element. It associates a name with a value, which is a string of characters. An attribute is written as:name = "valueAn empty element (element with no content) has following syntax:

XML Elements Rules: Following rules are required to be followed for XML elements:An elementnamecan contain any alphanumeric characters. The only punctuation mark allowed in names are the hyphen (-), under-score (_) and period (.).Names are case sensitive. For example, Address, address, and ADDRESS are different names.Start and end tags of an element must be identical.An element, which is a container, can contain text or elements.

XML - AttributesAttributes are part of the XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always aname-valuepair.Attributes are used to add a unique label to an element. Attributes are used to distinguish among elements of the same name. When you do not want to create a new element for every situation. Hence, use of an attribute can add a little more detail in differentiating two or more similar elements.ExcerciseIdentify errors in following XML syntax.This is a paragraph.

This is incorrectThis text is bold and italic

Tove Jani

5. Tanmay Patil ABC Corp (011) 123-4567 Anuradha Patil XYZ Corp (011) 123-4567

ToveJaniReminderDon't forget me this weekend!

Some important notesEntity References: Some characters have a special meaning in XML. If you place a character like "

Declaring Only One Occurrence of an Element

Example:

The example above declares that the child element "message" must occur once, and only once inside the "note" element.Declaring Minimum One Occurrence of an Element

Example: The + sign in the example above declares that the child element "message" must occur one or more times inside the "note" element. Declaring Zero or More Occurrences of an Element

Example:

The * sign in the example above declares that the child element "message" can occur zero or more times inside the "note" element.Declaring Zero or One Occurrences of an Element

Example:

The ? sign in the example above declares that the child element "message" can occur zero or one time inside the "note" element.Declaring either/or Content

The example above declares that the "note" element must contain a "to" element, a "from" element, a "header" element, and either a "message" or a "body" element.Declaring Mixed Content

The example above declares that the "note" element can contain zero or more occurrences of parsed character data, "to", "from", "header", or "message" elements.Attributes in DTDIn a DTD, attributes are declared with an ATTLIST declaration.

DTD example:Valid XML:

#REQUIRED:

DTD:

Valid XML:

Invalid XML:

Use the #REQUIRED keyword if you don't have an option for a default value, but still want to force the attribute to be present.

#IMPLIED

DTD:

Valid XML:

Valid XML:

Use the #IMPLIED keyword if you don't want to force the author to include an attribute, and you don't have an option for a default value.

#FIXED

DTD:

Use the #FIXED keyword 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.Enumerated Attribute Values

DTD:

Apples Bananas

African Coffee Table80120

Namespaces can be declared in the elements where they are used or in the XML root element:

Apples Bananas

African Coffee Table80120

The purpose is to give the namespace a unique name. However, often companies use the namespace as a pointer to a web page containing namespace information.Default NamespacesDefining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:xmlns="namespaceURI"This XML carries HTML table information: Apples Bananas

This XML carries information about a piece of furniture: African Coffee Table 80 120

AUniform Resource Identifier(URI) is a string of characters which identifies an Internet Resource.The most common URI is theUniform Resource Locator(URL) which identifies an Internet domain address.

We are having below XML.FanCeiling Fan Table FanFanEnglish Movies Fan Hindi Movies FanApply Qualified Namespace to differentiate Both Fan Tag.

XMLSchemaAn XML Schema describes the structure of an XML document, just like a DTD.An XML document with correct syntax is called "Well Formed".An XML document validated against an XML Schema is both "Well Formed" and "Valid".XML Schemas are More Powerful than DTDXML Schemas are written in XMLXML Schemas are extensible to additionsXML Schemas support data typesXML Schemas support namespacesXML Schema is an XML-based alternative to DTD:

or:

To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.

The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0.Start with a Raw XML DocumentWe want totransformthe following XML document ("cdcatalog.xml") into XHTML:

Empire Burlesque Bob Dylan USA Columbia 10.90 1985 ..

Create an XSL Style Sheet(Extensible Stylesheet Language)Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:

My CD Collection Title Artist

Link the XSL Style Sheet to the XML Document

Add the XSL style sheet reference to your XML document ("cdcatalog.xml"):