Introduction to XML History of XML Advantages of XML Syntax and structure of XML Rules of well...
-
Upload
lynn-phelps -
Category
Documents
-
view
255 -
download
4
Transcript of Introduction to XML History of XML Advantages of XML Syntax and structure of XML Rules of well...
Introduction to XML
History of XMLAdvantages of XML
Syntax and structure of XML
Rules of well formed XML document
Components of XMLXML Basics
www.tech.findforinfo.com 2
Content
• History of XML• Advantages of XML• Syntax and structure of XML• Rules of well formed XML document• Components of XML
www.tech.findforinfo.com 3
History of XML
• Emerged as a way of its predecessors SGML and HTML
• Standard Generalised Markup Language is an extensible tool for semantic markup but pretty dam and complex
• HTML was free and simple but used only regular people
www.tech.findforinfo.com 4
History of XML
• In 1996 there was a discussion about to find a extensiblity of SGML and simplicity of HTML
• W3C decide to sponsor a new markup language• Finally in 1998 W3C approved the Version 1.0 of
XML specification
www.tech.findforinfo.com 5
What is XML
• XML stands for EXtensible Markup Language • XML is a markup language much like HTML • XML was designed to carry data, not to
display data • XML tags are not predefined. You must define
your own tags • XML is designed to be self-descriptive • XML is a W3C Recommendation
www.tech.findforinfo.com 6
Difference between HTML and XML
XML HTMLXML was designed to transport and store data, with focus on what data is.
HTML was designed to display data, with focus on how data looks.
XML is about carrying information
HTML is about displaying information
www.tech.findforinfo.com 7
XML Naming Rules
• XML elements must follow these naming rules:
• Names can contain letters, numbers, and other characters
• Names cannot start with a number or punctuation character
• Names cannot start with the letters xml (or XML, or Xml, etc)
• Names cannot contain spaces • Any name can be used, no words are
reserved.
www.tech.findforinfo.com 8
XML and Structured Data
• Pre-XML representation of data:
• XML representation of the same data:
“PO-1234”,”CUST001”,”X9876”,”5”,”14.98”
<PURCHASE_ORDER><PO_NUM> PO-1234 </PO_NUM><CUST_ID> CUST001 </CUST_ID><ITEM_NUM> X9876 </ITEM_NUM><QUANTITY> 5 </QUANTITY><PRICE> 14.98 </PRICE>
</PURCHASE_ORDER>
www.tech.findforinfo.com 9
Advantages of XML
• The programmer are not restricted to use the limited set of tags
• With XML the programmer can create own tags• Each organization can build a library of tags• HTML consists of dozens of pre-defined tags but
XML is a tool used to generates markup language
www.tech.findforinfo.com 10
Syntax and StructureComponents of an XML Document
<?xml version=“1.0” ?><?xml-stylesheet type="text/xsl"
href=“template.xsl"?><ROOT>
<ELEMENT1><SUBELEMENT1 /><SUBELEMENT2 /></ELEMENT1><ELEMENT2> </ELEMENT2><ELEMENT3 type=‘string’> </ELEMENT3><ELEMENT4 type=‘integer’ value=‘9.3’> </ELEMENT4>
</ROOT>
Prologue (processing instructions)
Elements
Elements with Attributes
www.tech.findforinfo.com 11
Syntax and StructureRules For Well-Formed XML• There must be one, and only one, root element• Sub-elements must be properly nested
– A tag must end within the tag in which it was started
• Attributes are optional– Defined by an optional schema
www.tech.findforinfo.com 12
• Attribute values must be enclosed in “” or ‘’• Processing instructions are optional• XML is case-sensitive
– <tag> and <TAG> are not the same type of element
www.tech.findforinfo.com 13
Syntax and StructureWell-Formed XML?• No, CHILD2 and CHILD3 do not nest propertly
<xml? Version=“1.0” ?><PARENT>
<CHILD1>This is element 1</CHILD1><CHILD2><CHILD3>Number 3</CHILD2></CHILD3>
</PARENT>
www.tech.findforinfo.com 14
Syntax and StructureWell-Formed XML?• No, there are two root elements
<xml? Version=“1.0” ?><PARENT>
<CHILD1>This is element 1</CHILD1></PARENT><PARENT>
<CHILD1>This is another element 1</CHILD1></PARENT>
www.tech.findforinfo.com 15
Syntax and StructureWell-Formed XML?• Yes
<xml? Version=“1.0” ?><PARENT>
<CHILD1>This is element 1</CHILD1><CHILD2/><CHILD3></CHILD3>
</PARENT>
www.tech.findforinfo.com 16
Components of XML
• XML declaration (or) Processing instructions• Elements and Sub elements• Elements with attributes
www.tech.findforinfo.com 17
XML declarations
• Suggested by W3C specification• It is a processing instruction
• The processing instruction notifies the processing agent that the following document has been marked up as XML doc
<?xml version = "1.0"?>
www.tech.findforinfo.com 18
XML declaration
• All XML declaration or processing instruction should begin with <? and ?>
• Following the <? You will find the name of processing instruction which in this case is “xml”
www.tech.findforinfo.com 19
Attributes of XML declaration
• The XML processing instruction requires that you to specify a version attribute
• The others are optional attribute• Standalone• encoding
www.tech.findforinfo.com 20
Attributes of the XML declaration
• VersionThe Version attribute allows
you to specify the current version of XML which is 1.0
• StandaloneThe standalone attribute specifies
whether the document has any markup declarations that are defined in a separate document
www.tech.findforinfo.com 21
Attributes of the XML declaration
• EncodeAll XML parsers must support 8-bit
and 16-bit Unicode encoding
www.tech.findforinfo.com 22
Elements
• Once declaring the processing instruction we need to start code the XML document
• Elements are the basic units of XML content• Every element consists of a start tag and an end tag
<NAME>Frank Lee</NAME>
www.tech.findforinfo.com 23
• All XML documents must have at least one root element to be well formed.
• The root element, also often called the document tag,
www.tech.findforinfo.com 24
Sample XML file<?xml version="1.0" encoding="UTF-8" standalone="yes"?><DOCUMENT><CONTACT><NAME>Gunther Birznieks</NAME><EMAIL>[email protected]</EMAIL><PHONE>662-9999</PHONE></CONTACT><CONTACT><NAME>Susan Czigany</NAME><EMAIL>[email protected]</EMAIL><PHONE>555-1234</PHONE></CONTACT></DOCUMENT>
www.tech.findforinfo.com 25
XML’s hidden role in .NET
• XML use is ubiquitous throughout .NET• Web Services & XML
– Based on XML standards: SOAP, WSDL, UDDI, …
• XML in Configuration files– Application information stored in XML-based
configuration files
www.tech.findforinfo.com 26
XML’s hidden role in .NET• XML&ADO.NET Data Access
– Provides conversion between DataSets and XML
– DataSets are serialized as XML
www.tech.findforinfo.com 27
Web Services & XML
• Web Services are a very general model for building applications and can be implemented for any operation system that supports communication over the Internet.
• The web services are initially created with the help of a XML file
www.tech.findforinfo.com 28
XML in Configuration files
• ASP.NET stores the settings in human readable format using the configuration files such as machine.config and web.config
• Here XML provides an all-purpose languages that is designed to let programmers to store data in customized and standardized way of using tags
www.tech.findforinfo.com 29
XML&ADO.NET Data Access
• The internal format of the ADO.NET component use to store the data is actually in XML
• Normally with ADO components the data is exchanged in the binary format,which has difficulty in crossing the boundaries
www.tech.findforinfo.com 30
XML &ADO.NET Data Access
• With ADO.NET components exchange pure XML which can flow over normal channels
• This XML format is extensible over to different operating system and non-Microsoft development language
www.tech.findforinfo.com 31
NET XML Framework
• • Microsoft's.NET introduces a new suite of XML APIs
• – Built on industry standards• XML 1.0, Namespaces, DOM L2, XPath
1.0,• XSLT 1.0, XSD, SOAP, WSDL, UDDI• Commonly referred to as the .NET XML
stack
www.tech.findforinfo.com 32
.NET XML Namespaces
• .NET XML stack is partitioned over several namespaces
• – System.Xml• – System.Xml.XPath• – System.Xml.Xsl• – System.Xml.Schema• – System.Xml.Serialization
www.tech.findforinfo.com 33
Attributes
• Attributes are used to add extra information to an element
• Instead of using a sub tag we can use the an attribute
<?xml version=“1.0”?><Supermarket>
<Product id=“1” Name=“Chair”><Price=“45.60”>
</Product>
www.tech.findforinfo.com 34
Using Attributes and Values
• Attributes in XML are more stringent• Attributes must always have values• Attribute values must be enclosed with
double quotes
Eg <Product name=“Chair”/> Acceptable
<Product name=chair/ Not Acceptable
<Product name /> Not Acceptable
www.tech.findforinfo.com 35
Adding comments
• Comments are bracketed by the codes
<!– This is test file -->
<?xml version="1.0"?><Supermarket><!-- This is test file --><Product ID="1" Name="Chair><Price ="45.67"></Product><!-- This to test end --></Supermarket>
www.tech.findforinfo.com 36
XML Syntax rule -I
• All XML Elements must Have a Closing Tag
<p>This is a paragraph</p> <p>This is another paragraph</p>
www.tech.findforinfo.com 37
XML Syntax rule-II
• XML Tags are Case Sensitive• With XML, the tag <Letter> is different
from the tag <letter>.
<Message>This is incorrect</message> <message>This is correct</message>
www.tech.findforinfo.com 38
XML Syntax rule-III
• XML Elements Must be Properly Nested
• Properly nested" simply means that since the <i> element is opened inside the <b> element, it must be closed inside the <b> element.
<b><i>This text is bold and italic</i></b>
www.tech.findforinfo.com 39
XML Syntax rule-IV
• XML Documents Must Have a Root Element
• XML documents must contain one element that is the parent of all other elements. This element is called the root element.
<root> <child> <subchild>....</subchild> </child> </root>
www.tech.findforinfo.com 40
XML Syntax rule-V
• XML Attribute Values Must be Quoted• In XML the attribute value must always be
quoted. Study the two XML documents below. The first one is incorrect, the second is correct:
<note date=12/11/2007><to>Tove</to> <from>Jani</from> </note>
<note date= “12/11/2007”><to>Tove</to> <from>Jani</from> </note>
www.tech.findforinfo.com 41
XML Tree
www.tech.findforinfo.com 42
XML Syntax rule-VI
• Entity References• Some characters have a special meaning in
XML.• If you place a character like "<" inside an
XML element, it will generate an error because the parser interprets it as the start of a new element.
<message>if salary < 1000 then</message>
www.tech.findforinfo.com 43
XML Syntax rule-VII
• To avoid this error, replace the "<" character with an entity reference
• Replace the “<“ character with an entity reference
<message>if salary < 1000 then</message>
www.tech.findforinfo.com 44
XML Syntax rule-VIII
• There are 5 predefined entity references in XML:
< < less than
> > greater than & & Ampersand' ‘ apostrophe " “ Quotation mark
www.tech.findforinfo.com 45
Entities
<message>if salary < 1000 then</message>
<message>if salary < 1000 then</message>
www.tech.findforinfo.com 46
XML Syntax rule-IX
• Comments in XML• The syntax for writing comments in XML is
similar to that of HTML.• <!-- This is a comment -->
www.tech.findforinfo.com 47
XML Syntax rules-X• With XML, White Space is Preserved• HTML reduces multiple white space characters to
a single white space
• With XML, the white space in your document is not truncated.
HTML: Hello my name is Tove
Output: Hello my name is Tove.
www.tech.findforinfo.com 48
XML Namespaces - The xmlns Attribute
• The namespace is defined by the xmlns attribute in the start tag of an element
• The namespace declaration has the following syntax. xmlns:prefix="URI".
<h:table xmlns:h="http://www.w3.org/TR/html4/">
www.tech.findforinfo.com 49
• In the example above, the xmlns attribute in the <table> tag give the h: and prefixes a qualified namespace.
• companies use the namespace as a pointer to a web page containing namespace information.
<root><h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table>
www.tech.findforinfo.com 50
Uniform Resource Identifier
• A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource.
• The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address
www.tech.findforinfo.com 51
Syntax and Structure Namespaces: Overview• Part of XML’s extensibility• Allow authors to differentiate between tags of the
same name (using a prefix)– Frees author to focus on the data and decide
how to best describe it– Allows multiple XML documents from multiple
authors to be merged
www.tech.findforinfo.com 52
• Identified by a URI (Uniform Resource Identifier)– When a URL is used, it does NOT have to
represent a live server
www.tech.findforinfo.com 53
Syntax and Structure Namespaces: Declaration
xmlns: bk = “http://www.example.com/bookinfo/”
xmlns: bk = “urn:mybookstuff.org:bookinfo”
Namespace declaration examples:
Namespace declaration Prefix URI (URL)
xmlns: bk = “http://www.example.com/bookinfo/”
www.tech.findforinfo.com 54
Syntax and Structure Namespaces: Default Namespace
• An XML namespace declared without a prefix becomes the default namespace for all sub-elements
• All elements without a prefix will belong to the default namespace:
<BOOK xmlns=“http://www.bookstuff.org/bookinfo”> <TITLE>All About XML</TITLE> <AUTHOR>Joe Developer</AUTHOR>
www.tech.findforinfo.com 55
Default Namespace
• Defining a default namespace for an element saves us from using prefixes in all the child elements.
<table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>