XML 4 Beginners by Pratap

Embed Size (px)

Citation preview

  • 7/31/2019 XML 4 Beginners by Pratap

    1/96

    XML 4 BEGINNERSXML 4 BEGINNERS-- PRATAP ---- PRATAP --

  • 7/31/2019 XML 4 Beginners by Pratap

    2/96

    XMLXML TutorialTutorial

    XML stands for eXML stands for eXXtensibletensible MMarkuparkup LLanguage.anguage. XML is designed to transport and store data.XML is designed to transport and store data. XML is important to know, and very easy to learn.XML is important to know, and very easy to learn.

  • 7/31/2019 XML 4 Beginners by Pratap

    3/96

    Introduction toIntroduction to XMLXML

    XML was designed to transport and store data.XML was designed to transport and store data. HTML was designed to display data.HTML was designed to display data.

    What You Should Already KnowWhat You Should Already KnowBefore you continue you should have a basicBefore you continue you should have a basic

    understanding of the following:understanding of the following: HTMLHTML

    JavaScriptJavaScript

  • 7/31/2019 XML 4 Beginners by Pratap

    4/96

    What is XML?What is XML?

    XML stands for EXtensible Markup LanguageXML stands for EXtensible Markup Language XML is a markup language much like HTMLXML is a markup language much like HTML XML was designed to carry data, not to displayXML was designed to carry data, not to display

    datadata XML tags are not predefined. You must define yourXML tags are not predefined. You must define your

    own tagsown tags XML is designed to be self-descriptiveXML is designed to be self-descriptive

    XML is a W3C RecommendationXML is a W3C Recommendation

  • 7/31/2019 XML 4 Beginners by Pratap

    5/96

    The Difference Between XMLThe Difference Between XMLand HTMLand HTML

    XML is not a replacement for HTML.XML is not a replacement for HTML. XML and HTML were designed with different goals:XML and HTML were designed with different goals: XML was designed to transport and store data,XML was designed to transport and store data,

    with focus on what data iswith focus on what data is HTML was designed to display data, with focus onHTML was designed to display data, with focus on

    how data lookshow data looks HTML is about displaying information, while XML isHTML is about displaying information, while XML is

    about carrying information.about carrying information.

  • 7/31/2019 XML 4 Beginners by Pratap

    6/96

    XML Does Not DO AnythingXML Does Not DO Anything

    Maybe it is a little hard to understand, but XMLMaybe it is a little hard to understand, but XMLdoes not DO anything. XML was created todoes not DO anything. XML was created tostructure, store, and transport information.structure, store, and transport information.

    The following example is a note to Tove, from Jani,The following example is a note to Tove, from Jani,

    stored as XML:stored as XML:

    ToveTove

    JaniJaniReminderReminderDon't forget me this weekend!Don't forget me this weekend!

  • 7/31/2019 XML 4 Beginners by Pratap

    7/96

    The note above is quite self descriptive. It hasThe note above is quite self descriptive. It hassender and receiver information, it also has asender and receiver information, it also has aheading and a message body.heading and a message body.

    But still, this XML document does not DO anything.But still, this XML document does not DO anything.It is just information wrapped in tags. SomeoneIt is just information wrapped in tags. Someonemust write a piece of software to send, receive ormust write a piece of software to send, receive ordisplay it.display it.

  • 7/31/2019 XML 4 Beginners by Pratap

    8/96

    With XML You Invent Your OwnWith XML You Invent Your OwnTagsTags

    The tags in the example above (like andThe tags in the example above (like and) are not defined in any XML standard.) are not defined in any XML standard.

    These tags are "invented" by the author of theThese tags are "invented" by the author of theXML document.XML document.

    That is because the XML language has noThat is because the XML language has nopredefined tags.predefined tags.

    The tags used in HTML are predefined. HTMLThe tags used in HTML are predefined. HTMLdocuments can only use tags defined in the HTMLdocuments can only use tags defined in the HTML

    standard (like

    , , etc.).standard (like

    , , etc.). XML allows the author to define his/her own tagsXML allows the author to define his/her own tags

    and his/her own document structure.and his/her own document structure.

  • 7/31/2019 XML 4 Beginners by Pratap

    9/96

    XML is Not a Replacement forXML is Not a Replacement forHTMLHTML

    XML is a complement to HTML.XML is a complement to HTML.

    It is important to understand that XML is not aIt is important to understand that XML is not areplacement for HTML. In most web applications,replacement for HTML. In most web applications,

    XML is used to transport data, while HTML is usedXML is used to transport data, while HTML is usedto format and display the data.to format and display the data.

    My best description of XML is this:My best description of XML is this:

    XML is a software- and hardware-XML is a software- and hardware-independent tool for carrying information.independent tool for carrying information.

    XML is a W3C RecommendationXML is a W3C Recommendation

    XML became a W3C Recommendation onXML became a W3C Recommendation onFebruar 10 1998.Februar 10, 1998.

  • 7/31/2019 XML 4 Beginners by Pratap

    10/96

    XML is EverywhereXML is Everywhere XML is now as important for the Web asXML is now as important for the Web as

    HTML was to the foundation of the Web.HTML was to the foundation of the Web.

    XML is the most common tool for dataXML is the most common tool for datatransmissions between all sorts oftransmissions between all sorts of

    applications.applications.

  • 7/31/2019 XML 4 Beginners by Pratap

    11/96

    How Can XML beHow Can XML be Used?Used?

    XML is used in many aspects of web development,XML is used in many aspects of web development,often to simplify data storage and sharing.often to simplify data storage and sharing.

    XML Separates Data from HTMLXML Separates Data from HTML If you need to display dynamic data in your HTMLIf you need to display dynamic data in your HTML

    document, it will take a lot of work to edit the HTML eachdocument, it will take a lot of work to edit the HTML eachtime the data changes.time the data changes.

    With XML, data can be stored in separate XML files. ThisWith XML, data can be stored in separate XML files. This

    way you can concentrate on using HTML/CSS for displayway you can concentrate on using HTML/CSS for displayand layout, and be sure that changes in the underlyingand layout, and be sure that changes in the underlyingdata will not require any changes to the HTML.data will not require any changes to the HTML.

    With a few lines of JavaScript code, you can read anWith a few lines of JavaScript code, you can read anexternal XML file and update the data content of yourexternal XML file and update the data content of your

    web page.web page.

  • 7/31/2019 XML 4 Beginners by Pratap

    12/96

    XML Simplifies Data SharingXML Simplifies Data Sharing

    In the real world, computer systems andIn the real world, computer systems anddatabases contain data in incompatible formats.databases contain data in incompatible formats.

    XML data is stored in plain text format. ThisXML data is stored in plain text format. Thisprovides a software- and hardware-independentprovides a software- and hardware-independentway of storing data.way of storing data.

    This makes it much easier to create data that canThis makes it much easier to create data that canbe shared by different applications.be shared by different applications.

  • 7/31/2019 XML 4 Beginners by Pratap

    13/96

    XML Simplifies DataXML Simplifies DataTransportTransport

    One of the most time-consuming challenges forOne of the most time-consuming challenges fordevelopers is to exchange data betweendevelopers is to exchange data betweenincompatible systems over the Internet.incompatible systems over the Internet.

    Exchanging data as XML greatly reduces thisExchanging data as XML greatly reduces thiscomplexity, since the data can be read by differentcomplexity, since the data can be read by differentincompatible applications.incompatible applications.

  • 7/31/2019 XML 4 Beginners by Pratap

    14/96

    XML Simplifies PlatformXML Simplifies PlatformChangesChanges

    Upgrading to new systems (hardware or softwareUpgrading to new systems (hardware or softwareplatforms), is always time consuming. Largeplatforms), is always time consuming. Largeamounts of data must be converted andamounts of data must be converted andincompatible data is often lost.incompatible data is often lost.

    XML data is stored in text format. This makes itXML data is stored in text format. This makes iteasier to expand or upgrade to new operatingeasier to expand or upgrade to new operatingsystems, new applications, or new browsers,systems, new applications, or new browsers,without losing data.without losing data.

  • 7/31/2019 XML 4 Beginners by Pratap

    15/96

    XML Makes Your Data MoreXML Makes Your Data MoreAvailableAvailable

    Different applications can access your data, notDifferent applications can access your data, notonly in HTML pages, but also from XML dataonly in HTML pages, but also from XML datasources.sources.

    With XML, your data can be available to all kinds ofWith XML, your data can be available to all kinds of"reading machines" (Handheld computers, voice"reading machines" (Handheld computers, voicemachines, news feeds, etc), and make it moremachines, news feeds, etc), and make it moreavailable for blind people, or people with otheravailable for blind people, or people with otherdisabilities.disabilities.

  • 7/31/2019 XML 4 Beginners by Pratap

    16/96

    XML is Used to Create NewXML is Used to Create NewInternet LanguagesInternet Languages

    A lot of new Internet languages are created with XML.A lot of new Internet languages are created with XML. Here are some examples:Here are some examples: XHTMLXHTML WSDL for describing available web servicesWSDL for describing available web services WAP and WML as markup languages for handheld devicesWAP and WML as markup languages for handheld devices RSS languages for news feedsRSS languages for news feeds RDF and OWL for describing resources and ontologyRDF and OWL for describing resources and ontology SMIL for describing multimedia for the webSMIL for describing multimedia for the web

    If Developers Have SenseIf Developers Have Sense

    If they DO have sense, future applications willIf they DO have sense, future applications willexchange their data in XML.exchange their data in XML.

    The future might give us word processors, spreadsheetThe future might give us word processors, spreadsheetapplications and databases that can read each other's dataapplications and databases that can read each other's datain XML format, without any conversion utilities in between.in XML format, without any conversion utilities in between.

  • 7/31/2019 XML 4 Beginners by Pratap

    17/96

    XMLXMLTreeTree

    XML documents form a tree structure that starts at "theXML documents form a tree structure that starts at "theroot" and branches to "the leaves".root" and branches to "the leaves".

    An Example XML DocumentAn Example XML Document

    XML documents use a self-describing and simple syntax:XML documents use a self-describing and simple syntax:

    ToveTove

    JaniJaniReminderReminderDon't forget me this weekend!Don't forget me this weekend!

  • 7/31/2019 XML 4 Beginners by Pratap

    18/96

    The first line is the XML declaration. It defines the XML versionThe first line is the XML declaration. It defines the XML version(1.0) and the encoding used (ISO-8859-1 = Latin-1/West(1.0) and the encoding used (ISO-8859-1 = Latin-1/WestEuropean character set).European character set).

    The next line describes theThe next line describes the root elementroot element of the documentof the document(like saying: "this document is a note"):(like saying: "this document is a note"):

    The next 4 lines describe 4The next 4 lines describe 4 child elementschild elements of the root (to,of the root (to,from, heading, and body):from, heading, and body):

    ToveToveJaniJaniReminderReminderDon't forget me this weekend!Don't forget me this weekend!

  • 7/31/2019 XML 4 Beginners by Pratap

    19/96

    And finally the last line defines the end of the rootAnd finally the last line defines the end of the rootelement:element:

    You can assume, from this example, that the XMLYou can assume, from this example, that the XMLdocument contains a note to Tove from Jani.document contains a note to Tove from Jani.

    Don't you agree that XML is pretty self-descriptive?Don't you agree that XML is pretty self-descriptive?

  • 7/31/2019 XML 4 Beginners by Pratap

    20/96

    XML Documents Form a TreeXML Documents Form a TreeStructureStructure

    XML documents must contain aXML documents must contain a root elementroot element..This element is "the parent" of all other elements.This element is "the parent" of all other elements.The elements in an XML document form aThe elements in an XML document form a

    document tree. The tree starts at the root anddocument tree. The tree starts at the root andbranches to the lowest level of the tree.branches to the lowest level of the tree.

    All elements can have sub elements (childAll elements can have sub elements (childelements):elements):

    ..........

  • 7/31/2019 XML 4 Beginners by Pratap

    21/96

    The terms parent, child, and sibling are used toThe terms parent, child, and sibling are used todescribe the relationships between elements.describe the relationships between elements.Parent elements have children. Children on theParent elements have children. Children on the

    same level are called siblings (brothers or sisters).same level are called siblings (brothers or sisters). All elements can have text content and attributesAll elements can have text content and attributes

    (just like in HTML).(just like in HTML).

  • 7/31/2019 XML 4 Beginners by Pratap

    22/96

    The image above represents one book in the XML below:The image above represents one book in the XML below:

    Everyday ItalianEveryday ItalianGiada De LaurentiisGiada De Laurentiis2005200530.0030.00

    Harry PotterHarry Potter

    J K. RowlingJ K. Rowling2005200529.9929.99

    Learning XMLLearning XMLErik T. RayErik T. Ray2003200339.9539.95

  • 7/31/2019 XML 4 Beginners by Pratap

    23/96

    The root element in the example is .The root element in the example is .All elements in the document areAll elements in the document arecontained within .contained within .

    The element has 4 children: ,, , .author>, , .

  • 7/31/2019 XML 4 Beginners by Pratap

    24/96

    XMLXML Syntax RulesSyntax Rules

    The syntax rules of XML are very simple andThe syntax rules of XML are very simple andlogical. The rules are easy to learn, and easy tological. The rules are easy to learn, and easy touse.use.

    All XML Elements Must Have a Closing TagAll XML Elements Must Have a Closing Tag In HTML, some elements do not have to have a closingIn HTML, some elements do not have to have a closing

    tag:tag:

    This is a paragraph.

    This is a paragraph.

    In XML, it is illegal to omit the closing tag. AllIn XML, it is illegal to omit the closing tag. Allelementselements mustmust have a closing tag:have a closing tag:

  • 7/31/2019 XML 4 Beginners by Pratap

    25/96

    NoteNote: You might have noticed from the previous: You might have noticed from the previousexample that the XML declaration did not have aexample that the XML declaration did not have aclosing tag. This is not an error. The declaration isclosing tag. This is not an error. The declaration isnot a part of the XML document itself, and it hasnot a part of the XML document itself, and it has

    no closing tag.no closing tag.

  • 7/31/2019 XML 4 Beginners by Pratap

    26/96

    XML Tags are CaseXML Tags are CaseSensitiveSensitive

    XML tags are case sensitive. The tag isXML tags are case sensitive. The tag isdifferent from the tag .different from the tag .

    Opening and closing tags must be written with theOpening and closing tags must be written with thesame case:same case:

    This is incorrectThis is incorrectThis is correct

  • 7/31/2019 XML 4 Beginners by Pratap

    27/96

    XML Elements Must be Properly NestedXML Elements Must be Properly Nested In HTML, you might see improperly nested elements:In HTML, you might see improperly nested elements:

    This text is bold and italicThis text is bold and italic

    In XML, all elementsIn XML, all elements mustmust be properly nested within each other:be properly nested within each other:

    This text is bold and italicThis text is bold and italic

    In the example above, "Properly nested" simply means thatIn the example above, "Properly nested" simply means that

    since the element is opened inside the element, itsince the element is opened inside the element, itmust be closed inside the element.must be closed inside the element.

    XML Documents Must Have a Root ElementXML Documents Must Have a Root Element XML documents must contain one element that is theXML documents must contain one element that is the

    parentparent of all other elements. This element is called theof all other elements. This element is called the

    rootroot element.element.

    ..........

  • 7/31/2019 XML 4 Beginners by Pratap

    28/96

    XML Attribute Values Must be QuotedXML Attribute Values Must be Quoted XML elements can have attributes in name/value pairsXML elements can have attributes in name/value pairsjust like in HTML.just like in HTML. In XML, the attribute values must always be quoted.In XML, the attribute values must always be quoted. Study the two XML documents below. The first one isStudy the two XML documents below. The first one is

    incorrect, the second is correct:incorrect, the second is correct:

    ToveToveJaniJani

    ToveToveJaniJani

    The error in the first document is that the dateThe error in the first document is that the dateattribute in the note element is not quoted.attribute in the note element is not quoted.

  • 7/31/2019 XML 4 Beginners by Pratap

    29/96

    Entity ReferencesEntity References

    Some characters have a special meaning in XML.Some characters have a special meaning in XML.

    If you place a character like "

  • 7/31/2019 XML 4 Beginners by Pratap

    30/96

    There are 5 predefined entity references in XML:There are 5 predefined entity references in XML:

    Note:Note: Only the characters "

  • 7/31/2019 XML 4 Beginners by Pratap

    31/96

    Comments in XMLComments in XML

    The syntax for writing comments in XML is similarThe syntax for writing comments in XML is similarto that of HTML.to that of HTML.

    White-space is Preserved in XMLWhite-space is Preserved in XML HTML truncates multiple white-space characters to oneHTML truncates multiple white-space characters to one

    single white-space:single white-space:

  • 7/31/2019 XML 4 Beginners by Pratap

    32/96

    XML Stores New Line as LFXML Stores New Line as LF

    In Windows applications, a new line is normallyIn Windows applications, a new line is normallystored as a pair of characters: carriage return (CR)stored as a pair of characters: carriage return (CR)and line feed (LF). In Unix applications, a new lineand line feed (LF). In Unix applications, a new lineis normally stored as an LF character. Macintoshis normally stored as an LF character. Macintosh

    applications also use an LF to store a new line.applications also use an LF to store a new line.

    XML stores a new line as LF.XML stores a new line as LF.

  • 7/31/2019 XML 4 Beginners by Pratap

    33/96

    XMLXML ElementsElements

    An XML document contains XML Elements.An XML document contains XML Elements.

    What is an XML Element?What is an XML Element?

    An XML element is everything from (including) theAn XML element is everything from (including) theelement's start tag to (including) the element's end tag.element's start tag to (including) the element's end tag.

    An element can contain:An element can contain: other elementsother elements texttext attributesattributes or a mix of all of the above...or a mix of all of the above...

  • 7/31/2019 XML 4 Beginners by Pratap

    34/96

    Harry PotterHarry PotterJ K. RowlingJ K. Rowling2005200529.9929.99

    Learning XMLLearning XMLErik T. RayErik T. Ray2003200339.9539.95

    In the example above, and haveIn the example above, and have

    element contentselement contents, because they contain other, because they contain otherelements. also has anelements. also has an attributeattribute(category="CHILDREN"). , , ,(category="CHILDREN"). , , ,

    and haveand have text contenttext content because they containbecause they contain

  • 7/31/2019 XML 4 Beginners by Pratap

    35/96

    XML Naming RulesXML Naming Rules

    XML elements must follow these naming rules:XML elements must follow these naming rules: Names can contain letters, numbers, and otherNames can contain letters, numbers, and other

    characterscharacters Names cannot start with a number or punctuationNames cannot start with a number or punctuation

    charactercharacter Names cannot start with the letters xml (or XML, or Xml,Names cannot start with the letters xml (or XML, or Xml,

    etc)etc) Names cannot contain spacesNames cannot contain spaces

    Any name can be used, no words are reserved.Any name can be used, no words are reserved.

  • 7/31/2019 XML 4 Beginners by Pratap

    36/96

    Best Naming PracticesBest Naming Practices Make names descriptive. Names with an underscoreMake names descriptive. Names with an underscore

    separator are nice: , .separator are nice: , . Names should be short and simple, like this: Names should be short and simple, like this:

    not like this: .not like this: . Avoid "-" characters. If you name something "first-name,"Avoid "-" characters. If you name something "first-name,"

    some software may think you want to subtract name fromsome software may think you want to subtract name fromfirst.first.

    Avoid "." characters. If you name something "first.name,"Avoid "." characters. If you name something "first.name,"some software may think that "name" is a property of thesome software may think that "name" is a property of theobject "first."object "first."

    Avoid ":" characters. Colons are reserved to be used forAvoid ":" characters. Colons are reserved to be used forsomething called namespaces (more later).something called namespaces (more later).

    XML documents often have a corresponding database. AXML documents often have a corresponding database. Agood practice is to use the naming rules of your databasegood practice is to use the naming rules of your databasefor the elements in the XML documents.for the elements in the XML documents.

    Non-English letters like are perfectly legal in XML, butNon-English letters like are perfectly legal in XML, butwatch out for problems if your software vendor doesn'twatch out for problems if your software vendor doesn'tsupport them.support them.

  • 7/31/2019 XML 4 Beginners by Pratap

    37/96

    XML Elements areXML Elements areExtensibleExtensible

    XML elements can be extended to carry moreXML elements can be extended to carry moreinformation.information. Look at the following XML example:Look at the following XML example:

  • 7/31/2019 XML 4 Beginners by Pratap

    38/96

    Imagine that the author of the XML document added someImagine that the author of the XML document added someextra information to it:extra information to it:

    2008-01-102008-01-10ToveToveJaniJaniReminderReminderDon't forget me this weekend!Don't forget me this weekend!

    Should the application break or crash?Should the application break or crash? No. The application should still be able to find the ,No. The application should still be able to find the ,

    , and elements in the XML document and, and elements in the XML document andproduce the same output.produce the same output. One of the beauties of XML, is that it can be extendedOne of the beauties of XML, is that it can be extended

    without breaking applications.without breaking applications.

  • 7/31/2019 XML 4 Beginners by Pratap

    39/96

    XMLXML AttributesAttributes

    XML elements can have attributes, just like HTML.XML elements can have attributes, just like HTML. Attributes provide additional information about an element.Attributes provide additional information about an element.

    XML AttributesXML Attributes In HTML, attributes provide additional information aboutIn HTML, attributes provide additional information about

    elements:elements:

    Attributes often provide information that is not a part of theAttributes often provide information that is not a part of the

    data. In the example below, the file type is irrelevant to thedata. In the example below, the file type is irrelevant to thedata, but can be important to the software that wants todata, but can be important to the software that wants tomanipulate the element:manipulate the element:

    computer.gifcomputer.gif

    ib b

  • 7/31/2019 XML 4 Beginners by Pratap

    40/96

    XML Attributes Must beXML Attributes Must beQuotedQuoted

    Attribute values must always be quoted. EitherAttribute values must always be quoted. Eithersingle or double quotes can be used. For asingle or double quotes can be used. For aperson's sex, the person element can be writtenperson's sex, the person element can be writtenlike this:like this:

    or like this:or like this:

  • 7/31/2019 XML 4 Beginners by Pratap

    41/96

    If the attribute value itself contains double quotesIf the attribute value itself contains double quotesyou can use single quotes, like in this example:you can use single quotes, like in this example:

    or you can use character entities:or you can use character entities:

  • 7/31/2019 XML 4 Beginners by Pratap

    42/96

    XML Elements vs. AttributesXML Elements vs. Attributes

    Take a look at these examples:Take a look at these examples:

    AnnaAnna

    SmithSmith

    femalefemale

    AnnaAnnaSmithSmith

  • 7/31/2019 XML 4 Beginners by Pratap

    43/96

    In the first example sex is an attribute. In the last,In the first example sex is an attribute. In the last,sex is an element. Both examples provide thesex is an element. Both examples provide thesame information.same information.

    There are no rules about when to use attributes orThere are no rules about when to use attributes or

    when to use elements. Attributes are handy inwhen to use elements. Attributes are handy inHTML. In XML my advice is to avoid them. UseHTML. In XML my advice is to avoid them. Useelements instead.elements instead.

  • 7/31/2019 XML 4 Beginners by Pratap

    44/96

    My Favorite WayMy Favorite Way

    The following three XML documents containThe following three XML documents containexactly the same information:exactly the same information:

    A date attribute is used in the first example:A date attribute is used in the first example:

    ToveToveJaniJaniReminderReminder

    Don't forget me this weekend!Don't forget me this weekend!

    d l i d i h d lA d t l t i d i th d l

  • 7/31/2019 XML 4 Beginners by Pratap

    45/96

    A date element is used in the second example:A date element is used in the second example:

    10/01/200810/01/2008ToveToveJaniJaniReminderReminderDon't forget me this weekend!Don't forget me this weekend!

    An expanded date element is used in the third: (THIS IS MYAn expanded date element is used in the third: (THIS IS MYFAVORITE):FAVORITE):

    1010010120082008

    ToveToveJaniJaniReminderReminderDon't forget me this weekend!Don't forget me this weekend!

  • 7/31/2019 XML 4 Beginners by Pratap

    46/96

    Avoid XML Attributes?Avoid XML Attributes?

    Some of the problems with using attributes are:Some of the problems with using attributes are: attributes cannot contain multiple values (elements can)attributes cannot contain multiple values (elements can) attributes cannot contain tree structures (elements can)attributes cannot contain tree structures (elements can) attributes are not easily expandable (for future changes)attributes are not easily expandable (for future changes)

    Attributes are difficult to read and maintain. Use elementsAttributes are difficult to read and maintain. Use elementsfor data. Use attributes for information that is not relevantfor data. Use attributes for information that is not relevantto the data.to the data.

    Don't end up like this:Don't end up like this:

    XML Attributes for MetadataXML Attributes for Metadata

  • 7/31/2019 XML 4 Beginners by Pratap

    47/96

    XML Attributes for MetadataXML Attributes for Metadata

    Sometimes ID references are assigned to elements. TheseSometimes ID references are assigned to elements. TheseIDs can be used to identify XML elements in much the sameIDs can be used to identify XML elements in much the sameway as the id attribute in HTML. This example demonstratesway as the id attribute in HTML. This example demonstrates

    this:this:

    ToveToveJaniJani

    ReminderReminderDon't forget me this weekend!Don't forget me this weekend!

    JaniJaniToveToveRe: ReminderRe: ReminderI will notI will not

    The id attributes above are for identifying the differentThe id attributes above are for identifying the differentnotes. It is not a part of the note itself.notes. It is not a part of the note itself.

    What I'm trying to say here is that metadata (data aboutWhat I'm trying to say here is that metadata (data about

  • 7/31/2019 XML 4 Beginners by Pratap

    48/96

    XMLXML ValidationValidation

    XML with correct syntax is "Well Formed" XML.XML with correct syntax is "Well Formed" XML. XML validated against a DTD is "Valid" XML.XML validated against a DTD is "Valid" XML. Well Formed XML DocumentsWell Formed XML Documents

    A "Well Formed" XML document has correct XML syntax.A "Well Formed" XML document has correct XML syntax.The syntax rules were described in the previousThe syntax rules were described in the previous

    chapters:chapters: XML documents must have a root elementXML documents must have a root element XML elements must have a closing tagXML elements must have a closing tag

    XML tags are case sensitiveXML tags are case sensitive XML elements must be properly nestedXML elements must be properly nested XML attribute values must be quotedXML attribute values must be quoted

  • 7/31/2019 XML 4 Beginners by Pratap

    49/96

    Valid XML DocumentsValid XML Documents

    A "Valid" XML document is a "Well Formed" XML document,A "Valid" XML document is a "Well Formed" XML document,which also conforms to the rules of a Document Typewhich also conforms to the rules of a Document TypeDefinition (DTD):Definition (DTD):

    ToveToveJaniJaniReminderReminder

    Don't forget me this weekend!Don't forget me this weekend!

    The DOCTYPE declaration in the example above, is aThe DOCTYPE declaration in the example above, is areference to an external DTD file. The content of the file isreference to an external DTD file. The content of the file isshown in the paragraph below.shown in the paragraph below.

  • 7/31/2019 XML 4 Beginners by Pratap

    50/96

    XML DTDXML DTD

    The purpose of a DTD is to define the structure ofThe purpose of a DTD is to define the structure ofan XML document. It defines the structure with aan XML document. It defines the structure with alist of legal elements:list of legal elements:

    ]>]>

  • 7/31/2019 XML 4 Beginners by Pratap

    51/96

    Why Use a DTD?Why Use a DTD?

    With a DTD, each of your XML files can carry aWith a DTD, each of your XML files can carry adescription of its own format.description of its own format.

    With a DTD, independent groups of people canWith a DTD, independent groups of people canagree to use a standard DTD for interchangingagree to use a standard DTD for interchanging

    data.data.Your application can use a standard DTD to verifyYour application can use a standard DTD to verify

    that the data you receive from the outside world isthat the data you receive from the outside world isvalid.valid.

    You can also use a DTD to verify your own data.You can also use a DTD to verify your own data.

    Th B ildi Bl k f XMLTh B ildi Bl k f XML

  • 7/31/2019 XML 4 Beginners by Pratap

    52/96

    The Building Blocks of XMLThe Building Blocks of XMLDocumentsDocuments

    The main building blocks of both XML and HTMLThe main building blocks of both XML and HTMLdocuments are elements.documents are elements.

    Seen from a DTD point of view, all XML documentsSeen from a DTD point of view, all XML documents(and HTML documents) are made up by the(and HTML documents) are made up by the

    following building blocks:following building blocks: ElementsElements AttributesAttributes EntitiesEntities

    PCDATAPCDATA CDATACDATA

  • 7/31/2019 XML 4 Beginners by Pratap

    53/96

    PCDATAPCDATA

    PCDATA means parsed character data.PCDATA means parsed character data.Think of character data as the text found betweenThink of character data as the text found between

    the start tag and the end tag of an XML element.the start tag and the end tag of an XML element. PCDATA is text that WILL be parsed by aPCDATA is text that WILL be parsed by a

    parserparser.. The text will be examined by theThe text will be examined by theparser for entities and markupparser for entities and markup..

    Tags inside the text will be treated as markup andTags inside the text will be treated as markup andentities will be expanded.entities will be expanded.

    However, parsed character data should notHowever, parsed character data should notcontain any &, characters; these need tocontain any &, characters; these need tobe represented by the & < and >be represented by the & < and >entities, respectively.entities, respectively.

  • 7/31/2019 XML 4 Beginners by Pratap

    54/96

    CDATACDATA

    CDATA means character data.CDATA means character data. CDATA is text that will NOT be parsed by aCDATA is text that will NOT be parsed by a

    parserparser. Tags inside the text will NOT be treated as. Tags inside the text will NOT be treated asmarkup and entities will not be expanded.markup and entities will not be expanded.

    DTD -DTD - ElementsElements

  • 7/31/2019 XML 4 Beginners by Pratap

    55/96

    In a DTD, elements are declared with an ELEMENTIn a DTD, elements are declared with an ELEMENTdeclaration.declaration.

    Declaring ElementsDeclaring Elements In a DTD, XML elements are declared with an elementIn a DTD, XML elements are declared with an element

    declaration with the following syntax:declaration with the following syntax:

    oror

    Empty ElementsEmpty Elements Empty elements are declared with the category keywordEmpty elements are declared with the category keyword

    EMPTY:EMPTY:

    Example:Example:

    Elements with Parsed Character DataElements with Parsed Character Data

  • 7/31/2019 XML 4 Beginners by Pratap

    56/96

    Elements with only parsed character data are declared withElements with only parsed character data are declared with#PCDATA inside parentheses:#PCDATA inside parentheses:

    Example:Example:

    Elements with any ContentsElements with any Contents Elements declared with the category keyword ANY, can containElements declared with the category keyword ANY, can contain

    any combination of parsable dataany combination of parsable data

    Example:Example:

    Elements with Children (sequences)Elements with Children (sequences)

  • 7/31/2019 XML 4 Beginners by Pratap

    57/96

    Elements with one or more children are declared withElements with one or more children are declared withthe name of the children elements inside parentheses:the name of the children elements inside parentheses:

    oror

    Example:Example:

    When children are declared in a sequenceWhen children are declared in a sequenceseparated by commas, the children must appear inseparated by commas, the children must appear inthe same sequence in the document. In a fullthe same sequence in the document. In a fulldeclaration, the children must also be declared,declaration, the children must also be declared,and the children can also have children. The fulland the children can also have children. The full

    declaration of the "note" element is:declaration of the "note" element is:

  • 7/31/2019 XML 4 Beginners by Pratap

    58/96

    Declaring Only One Occurrence of an ElementDeclaring Only One Occurrence of an Element

    Example:Example:

    Declaring Minimum One Occurrence of an ElementDeclaring Minimum One Occurrence of an Element

    Example:Example:

    The + sign in the example above declares that the child elementThe + sign in the example above declares that the child element"message" must occur one or more times inside the "note""message" must occur one or more times inside the "note"element.element.

    Declaring Zero or More Occurrences of an ElementDeclaring Zero or More Occurrences of an Element

  • 7/31/2019 XML 4 Beginners by Pratap

    59/96

    Example:Example:

    The * sign in the example above declares that theThe * sign in the example above declares that thechild element "message" can occur zero or morechild element "message" can occur zero or moretimes inside the "note" element.times inside the "note" element.

    Declaring Zero or One Occurrences of an ElementDeclaring Zero or One Occurrences of an Element

  • 7/31/2019 XML 4 Beginners by Pratap

    60/96

    Example:Example:

    The ? sign in the example above declares that theThe ? sign in the example above declares that thechild element "message" can occur zero or onechild element "message" can occur zero or onetime inside the "note" element.time inside the "note" element.

    Declaring either/or ContentDeclaring either/or Content

  • 7/31/2019 XML 4 Beginners by Pratap

    61/96

    Example:Example:

    The example above declares that the "note"The example above declares that the "note"element must contain a "to" element, a "from"element must contain a "to" element, a "from"element, a "header" element, and either aelement, a "header" element, and either a

    "message" or a "body" element."message" or a "body" element.

    Declaring Mixed ContentDeclaring Mixed Content

    Example:Example:

    message)*>

    The example above declares that the "note"The example above declares that the "note"

    element can contain zero or more occurrences ofelement can contain zero or more occurrences of" " " " " "" " " " " "

    DTD -DTD - AttributesAttributes

  • 7/31/2019 XML 4 Beginners by Pratap

    62/96

    In a DTD, attributes are declared with an ATTLISTIn a DTD, attributes are declared with an ATTLISTdeclaration.declaration.

    Declaring AttributesDeclaring Attributes An attribute declaration has the following syntaxAn attribute declaration has the following syntax

    attribute-type default-value>attribute-type default-value>

    DTD example:DTD example:

    XML example:XML example:

    TheThe attribute-typeattribute-type can be one of the following:can be one of the following:

  • 7/31/2019 XML 4 Beginners by Pratap

    63/96

    A Default Attribute ValueA Default Attribute Value

  • 7/31/2019 XML 4 Beginners by Pratap

    64/96

    DTD:DTD:

    Valid XML:Valid XML:

    In the example above, the "square" element isIn the example above, the "square" element is

    defined to be an empty element with a "width"defined to be an empty element with a "width"attribute of type CDATA. If no width is specified, itattribute of type CDATA. If no width is specified, ithas a default value of 0.has a default value of 0.

    #REQUIRED#REQUIREDSyntaxSyntax

    attribute-type #REQUIRED>attribute-type #REQUIRED>

    ExampleExample

  • 7/31/2019 XML 4 Beginners by Pratap

    65/96

    DTD:DTD:

    Valid XML:Valid XML:

    Invalid XML:Invalid XML:

    Use the #REQUIRED keyword if you don't have anUse the #REQUIRED keyword if you don't have an

    option for a default value, but still want to forceoption for a default value, but still want to forcethe attribute to be present.the attribute to be present.

    #IMPLIED#IMPLIED

  • 7/31/2019 XML 4 Beginners by Pratap

    66/96

    SyntaxSyntax

    #IMPLIED>

    ExampleExample

    DTD:DTD:

    Valid XML:Valid XML:

    Valid XML:Valid XML:

    Use the #IMPLIED keyword if you don't want to force theUse the #IMPLIED keyword if you don't want to force the

    author to include an attribute, and you don't have an optionauthor to include an attribute, and you don't have an option

    #FIXED#FIXED

    SyntaxSyntax

  • 7/31/2019 XML 4 Beginners by Pratap

    67/96

    SyntaxSyntax

    #FIXED "value">

    ExampleExample

    DTD:DTD:

    Valid XML:Valid XML:

    Invalid XML:Invalid XML:

    Use the #FIXED keyword when you want an attribute toUse the #FIXED keyword when you want an attribute tohave a fixed value without allowing the author to change it.have a fixed value without allowing the author to change it.

    If an author includes another value, the XML parser willIf an author includes another value, the XML parser will

    Enumerated Attribute ValuesEnumerated Attribute Values

  • 7/31/2019 XML 4 Beginners by Pratap

    68/96

    SyntaxSyntax

    en2|..) default-value>en2|..) default-value>

    ExampleExample

    DTD:DTD:

    XML example:XML example:oror

    Use enumerated attribute values when you wantUse enumerated attribute values when you wantthe attribute value to be one of a fixed set of legalthe attribute value to be one of a fixed set of legal

    values.values.

    Some Examples Some Examples TV Schedule DTDTV Schedule DTD

  • 7/31/2019 XML 4 Beginners by Pratap

    69/96

    TV Schedule DTDTV Schedule DTD

    ]>]>

    Newspaper Article DTDNewspaper Article DTD

  • 7/31/2019 XML 4 Beginners by Pratap

    70/96

    (HEADLINE,BYLINE,LEAD,BODY,NOTES)>

    ]>]>

    Product Catalog DTDProduct Catalog DTD

  • 7/31/2019 XML 4 Beginners by Pratap

    71/96

    (SPECIFICATIONS+,OPTIONS?,PRICE+,NOTES?)>INVENTORY (InStock|Backordered|Discontinued) "InStock">

    POWER CDATA #IMPLIED>

    CASE (HardShell|Soft|NotApplicable) "HardShell">

    SHIPPING CDATA #IMPLIED>

    ]>]>

    DTD SummaryDTD Summary

  • 7/31/2019 XML 4 Beginners by Pratap

    72/96

    This tutorial has taught you how to describe theThis tutorial has taught you how to describe thestructure of an XML document.structure of an XML document.

    You have learned how to use a DTD to define the legalYou have learned how to use a DTD to define the legalelements of an XML document, and how a DTD can beelements of an XML document, and how a DTD can bedeclared inside your XML document, or as an externaldeclared inside your XML document, or as an externalreference.reference.

    You have learned how to declare the legal elements,You have learned how to declare the legal elements,

    attributes, entities, and CDATA sections for XMLattributes, entities, and CDATA sections for XMLdocuments.documents.You have also seen how to validate an XML documentYou have also seen how to validate an XML document

    against a DTD.against a DTD.

  • 7/31/2019 XML 4 Beginners by Pratap

    73/96

    XML SchemaXML Schema

    W3C supports an XML-based alternative to DTD, called XMLW3C supports an XML-based alternative to DTD, called XMLSchema:Schema:

  • 7/31/2019 XML 4 Beginners by Pratap

    74/96

    XMLXML ValidatorValidator

    Use our XML validator to syntax-check your XML.Use our XML validator to syntax-check your XML.

    XML Errors Will Stop YouXML Errors Will Stop You Errors in XML documents will stop your XMLErrors in XML documents will stop your XML

    applications.applications.The W3C XML specification states that a programThe W3C XML specification states that a program

    should stop processing an XML document if it findsshould stop processing an XML document if it findsan error. The reason is that XML software shouldan error. The reason is that XML software shouldbe small, fast, and compatible.be small, fast, and compatible.

    HTML browsers will display documents with errorsHTML browsers will display documents with errors(like missing end tags). HTML browsers are big and(like missing end tags). HTML browsers are big andincompatible because they have a lot ofincompatible because they have a lot ofunnecessary code to deal with (and display) HTMLunnecessary code to deal with (and display) HTML

    errors.errors.

  • 7/31/2019 XML 4 Beginners by Pratap

    75/96

    Syntax-Check Your XMLSyntax-Check Your XML

    To help you syntax-check your XML, we haveTo help you syntax-check your XML, we havecreated an XML validator.created an XML validator.

    Paste your XML into the text area below, andPaste your XML into the text area below, and

    syntax-check it by clicking the "Validate" button.syntax-check it by clicking the "Validate" button.

  • 7/31/2019 XML 4 Beginners by Pratap

    76/96

  • 7/31/2019 XML 4 Beginners by Pratap

    77/96

    h k il

  • 7/31/2019 XML 4 Beginners by Pratap

    78/96

    Syntax-Check an XML FileSyntax-Check an XML File

    You can syntax-check an XML file by typing theYou can syntax-check an XML file by typing theURL of the file into the input field below, and thenURL of the file into the input field below, and thenclick the "Validate" button:click the "Validate" button:

    Validate Your XML Against aValidate Your XML Against a

  • 7/31/2019 XML 4 Beginners by Pratap

    79/96

    Validate Your XML Against aValidate Your XML Against aDTDDTD

    If you know DTD, and you run Internet Explorer, you can validate your XML inIf you know DTD, and you run Internet Explorer, you can validate your XML inthe text area below.the text area below. Just add the DOCTYPE declaration to your XML and click the "Validate"Just add the DOCTYPE declaration to your XML and click the "Validate"

    button:button:

    ]>]>ToveToveJaniJaniReminderReminderDon't forget me this weekend!Don't forget me this weekend!

    Vi i

    XML Fil

  • 7/31/2019 XML 4 Beginners by Pratap

    80/96

    ViewingViewing XML FilesXML Files

    Raw XML files can be viewed in all major browsers.Raw XML files can be viewed in all major browsers. Don't expect XML files to be displayed as HTMLDon't expect XML files to be displayed as HTML

    pages.pages.

    Viewing XML FilesViewing XML Files

  • 7/31/2019 XML 4 Beginners by Pratap

    81/96

    The XML document will be displayed with color-The XML document will be displayed with color-coded root and child elements. A plus (+) or minuscoded root and child elements. A plus (+) or minussign (-) to the left of the elements can be clicked tosign (-) to the left of the elements can be clicked toexpand or collapse the element structure. To viewexpand or collapse the element structure. To view

    the raw XML source (without the + and - signs),the raw XML source (without the + and - signs),select "View Page Source" or "View Source" fromselect "View Page Source" or "View Source" fromthe browser menu.the browser menu.

    Note:Note: In Safari, only the element text will beIn Safari, only the element text will be

    displayed. To view the raw XML, you must rightdisplayed. To view the raw XML, you must rightclick the page and select "View Source"click the page and select "View Source"

    i i lid ilVi i I lid XML Fil

  • 7/31/2019 XML 4 Beginners by Pratap

    82/96

    Viewing an Invalid XML FileViewing an Invalid XML File

    If an erroneous XML file is opened, the browser willIf an erroneous XML file is opened, the browser willreport the error.report the error.

    O h XML E lOth XML E l

  • 7/31/2019 XML 4 Beginners by Pratap

    83/96

    Other XML ExamplesOther XML Examples

    Viewing some XML documents will help you get theViewing some XML documents will help you get theXML feeling.XML feeling.

    An XML CD CatalogAn XML CD Catalog

  • 7/31/2019 XML 4 Beginners by Pratap

    84/96

    Why Does XML Display LikeWhy Does XML Display Like

  • 7/31/2019 XML 4 Beginners by Pratap

    85/96

    Why Does XML Display LikeWhy Does XML Display LikeThis?This?

    XML documents do not carry information aboutXML documents do not carry information abouthow to display the data.how to display the data.

    Since XML tags are "invented" by the author of theSince XML tags are "invented" by the author of theXML document, browsers do not know if a tag likeXML document, browsers do not know if a tag like

    describes an HTML table or a dining table. describes an HTML table or a dining table. Without any information about how to display theWithout any information about how to display the

    data, most browsers will just display the XMLdata, most browsers will just display the XMLdocument as it is.document as it is.

    In the next chapters, we will take a look atIn the next chapters, we will take a look atdifferent solutions to the display problem, usingdifferent solutions to the display problem, usingCSS, XSLT and JavaScript.CSS, XSLT and JavaScript.

    Di l iDi l i XML ith CSSXML ith CSS

  • 7/31/2019 XML 4 Beginners by Pratap

    86/96

    DisplayingDisplaying XML with CSSXML with CSS

    With CSS (Cascading Style Sheets) you can addWith CSS (Cascading Style Sheets) you can adddisplay information to an XML document.display information to an XML document.

    Displaying your XML Files with CSS?Displaying your XML Files with CSS? It is possible to use CSS to format an XMLIt is possible to use CSS to format an XML

    document.document. Below is an example of how to use a CSS styleBelow is an example of how to use a CSS style

    sheet to format an XML document:sheet to format an XML document:

  • 7/31/2019 XML 4 Beginners by Pratap

    87/96

    XML FILEXML FILE

  • 7/31/2019 XML 4 Beginners by Pratap

    88/96

    XML FILEXML FILE

    CSS FILECSS FILE

  • 7/31/2019 XML 4 Beginners by Pratap

    89/96

    CSS FILECSS FILE

    Finally, view:Finally, view:

  • 7/31/2019 XML 4 Beginners by Pratap

    90/96

    Finally, view:y,

    The CD catalog formatted witThe CD catalog formatted wit

    Below is a fraction of the XML file. The second line links the XML file to theBelow is a fraction of the XML file. The second line links the XML file to theCSS file:CSS file:

    http://www.w3schools.com/xml/cd_catalog_with_css.xmlhttp://www.w3schools.com/xml/cd_catalog_with_css.xmlhttp://www.w3schools.com/xml/cd_catalog_with_css.xml
  • 7/31/2019 XML 4 Beginners by Pratap

    91/96

    Empire BurlesqueEmpire BurlesqueBob DylanBob DylanUSAUSAColumbiaColumbia10.9010.9019851985

    Hide your heartHide your heartBonnie TylerBonnie TylerUKUKCBS RecordsCBS Records9.909.9019881988

    ..

    ....

    Formatting XML with CSS is not the most common method.Formatting XML with CSS is not the most common method.

    Di l i XMLDisplaying XML ith XSLTwith XSLT

  • 7/31/2019 XML 4 Beginners by Pratap

    92/96

    Displaying XMLDisplaying XML with XSLTwith XSLT

    With XSLT you can transform an XML documentWith XSLT you can transform an XML documentinto HTMLinto HTML

    Displaying XML with XSLTDisplaying XML with XSLT

    XSLT is the recommended style sheet language ofXSLT is the recommended style sheet language ofXML.XML.

    XSLT (eXtensible Stylesheet LanguageXSLT (eXtensible Stylesheet LanguageTransformations) is far more sophisticated thanTransformations) is far more sophisticated than

    CSS.CSS. XSLT can be used to transform XML into HTML,XSLT can be used to transform XML into HTML,

    before it is displayed by a browser:before it is displayed by a browser:

    XML Code:XML Code:

  • 7/31/2019 XML 4 Beginners by Pratap

    93/96

    Belgian WafflesBelgian Waffles$5.95$5.95two of our famous Belgian Waffles withtwo of our famous Belgian Waffles with

    plenty of real maple syrupplenty of real maple syrup

    650650

    Strawberry Belgian WafflesStrawberry Belgian Waffles$7.95$7.95

    light Belgian waffles covered withlight Belgian waffles covered withstrawberries and whipped creamstrawberries and whipped cream900900

    B B B l i W ffl /B B B l i W ffl /

  • 7/31/2019 XML 4 Beginners by Pratap

    94/96

    Berry-Berry Belgian WafflesBerry-Berry Belgian Waffles$8.95$8.95light Belgian waffles covered with an assortment of freshlight Belgian waffles covered with an assortment of fresh

    berries and whipped creamberries and whipped cream

    900900

    French ToastFrench Toast$4.50$4.50thick slices made from our homemade sourdoughthick slices made from our homemade sourdough

    breadbread

    600600

    Homestyle BreakfastHomestyle Breakfast$6.95$6.95two eggs, bacon or sausage, toast, and our ever-populartwo eggs, bacon or sausage, toast, and our ever-popular

    hash brownshash browns950950

    XSLT CodeXSLT Code

  • 7/31/2019 XML 4 Beginners by Pratap

    95/96

    - -

    (calories per serving) (calories per serving)

  • 7/31/2019 XML 4 Beginners by Pratap

    96/96

    Transforming XML with XSLT on the ServerTransforming XML with XSLT on the Server In the example above, the XSLT transformation is doneIn the example above, the XSLT transformation is done

    by the browser, when the browser reads the XML file.by the browser, when the browser reads the XML file. Different browsers may produce different result whenDifferent browsers may produce different result when

    transforming XML with XSLT. To reduce this problem thetransforming XML with XSLT. To reduce this problem theXSLT transformation can be done on the server.XSLT transformation can be done on the server.