Download presentation
Presentation is loading. Please wait.
1
XML 28-Dec-17
2
What is XML? XML stands for EXtensible Markup Language
XML is a markup language much like HTML More accurately defined as a metalanguage XML was designed to describe data
3
Historical Development
SGML( Standard Generalized Markup Language) XML HTML MusicXML . . . MathML XHTML CML RSS XBRL Comp Sci 346 12/28/2017
4
What is XML? XML tags are not predefined. You define your own tags depending on the application XML uses a Document Type Definition (DTD) or an XML Schema to describe the data XML with a DTD or XML Schema is designed to be self- descriptive XML is a W3C Recommendation
5
Difference Between XML and HTML
XML was designed to carry data. XML is not a replacement for HTML. XML and HTML were designed with different goals: XML was designed to describe data and to focus on what data is. HTML was designed to display data and to focus on how data looks. HTML is about displaying information, while XML is about describing information.
6
XML Does Not DO Anything
XML was not designed to DO anything. XML was created to structure, store and to send information.
7
More XML XML is free and extensible
XML tags are not predefined. You must "invent" your own tags. The tags used to mark up HTML documents and the structure of HTML documents are predefined. XML allows the author to define their own tags and their own document structure. The tags in the example above (like <to> and <from>) are not defined in any XML standard. These tags are "invented" by the author of the XML document.
8
XML is a Complement to HTML
It is important to understand that XML is not a replacement for HTML. In 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. XML is a cross-platform, software and hardware independent tool for transmitting information.
9
XML Can Separate Data from HTML
With XML, your data is stored outside your HTML. When HTML is used to display data, the data is stored inside your HTML. With XML, data can be stored in separate XML files. This way you can concentrate on using HTML for data layout and display, and be sure that changes in the underlying data will not require any changes to your HTML.
10
XML is Used to Exchange Data
With XML, data can be exchanged between incompatible systems. Computer systems and databases contain data in incompatible formats. One of the most time-consuming challenges for developers has been to exchange data between such systems over the Internet. Converting the data to XML can greatly reduce this complexity and create data that can be read by many different types of applications.
11
XML Can Be Used to Share Data
Since XML data is stored in plain text format, XML provides a software- and hardware-independent way of sharing data. This makes it much easier to create data that different applications can work with. It also makes it easier to expand or upgrade a system to new operating systems, servers, applications, and new browsers.
12
XML Can Be Used to Store Data
With XML, plain text files can be used to store data. XML can also be used to store data in files or in databases. Applications can be written to store and retrieve information from the store, and generic applications can be used to display the data.
13
Editors XML files can be edited using any text editor:
Windows: Notepad, NotePad++, EditPad Lite, etc Mac OS: Text Wrangler, BBEdit, TextMate Linux: Emacs, VIM, Pico, etc.
14
Viewing XML Files in a Web Browser
XML documents can be opened in Internet Explorer or in Firefox. If there are no syntax errors: IE and Firefox will display the document’s contents in an expandable/collapsible outline format including all markup tags.
15
Style Sheets & XML There are two main style sheet languages used with XML: Cascading Style Sheets (CSS) and Extensible Style Sheets (XSL) The important benefits to using style sheets are the same as using them with XHTML docs.
16
A sample XML document <?xml version="1.0" encoding=“UTF-8"?>
<customers> <name>Fred Flintstone</name> <street>123 Main St.</street> <city>Lansing</city> <state>MI</state> <zip>48901</zip> <phone> </phone> </customers>
17
Introduction to 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
18
XML is Not a Replacement for HTML
It is important to understand that XML is not a replacement for HTML. In most web applications, XML is used to transport data, while HTML is used to format and display the data.
19
With XML You Invent Your Own Tags
The tags used in HTML are predefined. HTML documents can only use tags defined in the HTML standard (like <p>, <h1>, etc.). XML allows the author to define his/her own tags and his/her own document structure.
20
XML Separates Data from HTML
If 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. With a few lines of JavaScript code, you can read an external XML file and update the data content of your web page.
21
XML Simplifies Data Transport
One 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.
22
HTML and XML, I XML stands for eXtensible Markup Language
HTML is used to mark up text so it can be displayed to users XML is used to mark up data so it can be processed by computers HTML describes both structure (e.g. <p>, <h2>, <em>) and appearance (e.g. <br>, <font>, <i>) XML describes only content, or “meaning” In XML, you make up your own tags HTML uses a fixed, unchangeable set of tags
23
Difference between XML and HTML
XML was designed to carry data, not displaying data XML is not a replacement for HTML. Different goals: XML was designed to describe data and to focus on what data is. HTML was designed to display data and to focus on how data looks. HTML is about displaying information, XML is about describing information. XML Introdution
24
An example of XML <?xml version="1.0"?> <products>
<product id="PRO001"> <name>Coca Cola</name> <price>5000</price> </product> <product id="PRO002"> <name>Pepsi</name> <price>4500</price> <product id="PRO003"> <name>Number One</name> <price>5500</price> </products> XML Introdution
25
HTML and XML, II HTML and XML look similar, because they are both SGML languages (SGML = Standard Generalized Markup Language) Both HTML and XML use elements enclosed in tags (e.g. <body>This is an element</body>) Both use tag attributes (e.g., <font face="Verdana" size="+1" color="red">) Both use entities (<, >, &, ", ') More precisely, HTML is defined in SGML XML is a (very small) subset of SGML
26
HTML and XML, III HTML is for humans XML is for computers
HTML describes web pages You don’t want to see error messages about the web pages you visit Browsers ignore and/or correct as many HTML errors as they can, so HTML is often sloppy XML is for computers XML describes data The rules are strict and errors are not allowed In this way, XML is like a programming language Current versions of most browsers can display XML However, browser support of XML is spotty at best
27
SGML(ISO 8879) Standard Generalized Markup Language
The international standard for defining descriptions of structure and content in text documents Interchangeable: device-independent, system-independent Tags are not predefined Using DTD to validate the structure of the document Large, powerful, and very complex Heavily used in industrial and commercial for over a decade XML Introdution
28
HTML(RFC 1866) HyperText Markup Language
A small SGML application used on web (a DTD and a set of processing conventions) Can only use a predefined set of tags XML Introdution
29
What Is XML? eXtensible Markup Language A simplified version of SGML
Maintains the most useful parts of SGML Designed so that SGML can be delivered over the Web XHTML -- a reformulation of HTML 4 in XML 1.0 XML Introdution
30
XML Syntax All XML elements must have a closing tag
XML tags are case sensitive All XML elements must be properly nested All XML documents must have a root tag Attribute values must always be quoted With XML, white space is preserved With XML, a new line is always stored as LF XML Introdution
31
XML-related technologies
DTD (Document Type Definition) and XML Schemas are used to define legal XML tags and their attributes for particular purposes CSS (Cascading Style Sheets) describe how to display HTML or XML in a browser XSLT (eXtensible Stylesheet Language Transformations) and XPath are used to translate from one form of XML to another DOM (Document Object Model), SAX (Simple API for XML, and JAXP (Java API for XML Processing) are all APIs for XML parsing
32
Example XML document <?xml version="1.0"?> <weatherReport>
<date>7/14/97</date> <city>North Place</city>, <state>NX</state> <country>USA</country> High Temp: <high scale="F">103</high> Low Temp: <low scale="F">70</low> Morning: <morning>Partly cloudy, Hazy</morning> Afternoon: <afternoon>Sunny & hot</afternoon> Evening: <evening>Clear and Cooler</evening> </weatherReport>
33
The Basic Rules XML is case sensitive
<Message>This is incorrect</message> <message>This is correct</message>
34
The Basic Rules Empty Element All start tags must have end tags
<composer>Mozart <composer>Mozart</composer> Empty Element <BR></BR> <BR/> <img align=“center” src=“logo.gif”/> <composer name=“Mozart”></composer> <composer name=“Mozart”/>
35
The Basic Rules Elements must be properly nested
<b><i>This is incorrect nesting</b></i> <b><i>This is correct nesting</i></b>
36
The Basic Rules XML declaration must be the first statement <?xml version="1.0" encoding="ISO " stAandalone="yes"?>
37
The Basic Rules <root> <child>
Every document must contain a root element <root> <child> <subchild>.....</subchild> </child> </root>
38
The Basic Rules <note date="12/11/2007">
Attribute values must be quoted with inverted commas <note date="12/11/2007"> <to>Ani</to> <from>John</from> </note>
39
The Basic Rules Certain characters are reserved for parsing
<message>if salary < 1000 then</message>
40
Predefined entities < less than > greater than & &
&ersand ' ' apostrophe " " quotation mark
41
XML Building blocks--Prolog
The part of an XML document that precedes the XML data Includes A declaration: version [, encoding, standalone] An optional DTD (Document Type Definition ) Example <?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?> XML Introdution
42
Tags Tags are used to specify a name for a given piece of information.
A tag consists of opening and closing angular brackets (<>) that enclose the name of the tag. Example <EMP_NAME>Nick Shaw</EMP_NAME> XML Introdution
43
Elements Elements are represented using tags.
An XML document must always have a root element. General format: <element> … </element> Empty element: <empty-Element /> Example <Authorname>John Smith</Authorname> XML Introdution
44
XML Elements are Extensible
XML documents can be extended to carry more information XML Elements have Relationships Elements are related as parents and children Elements have Content Elements can have different content types: element content, mixed content, simple content, or empty content and attributes XML elements must follow the naming rules XML Introdution
45
Content Content refers to the information represented by the elements of an XML document. Character or data content Element content Combination or mixed content Example <BOOKNAME>The Painted House</BOOKNAME> XML Introdution
46
Attributes Located in the start tag of elements
Provide additional information about elements Often provide information that is not a part of data Must be enclosed in quotes Should I use an element or an attribute? metadata (data about data) should be stored as attributes, and that data itself should be stored as elements XML Introdution
47
Entities An entity is a name that is associated with a block of data..
Internal Entities: < , >… General Entities General entities are declared in Document Type Definitions (DTD) Example <! ENTITY nyt "The Times of India."> For more information, please visit &nyt; Thank you! Parameter Entities Are only declared and used in DTDs <!ENTITY % bool ("yes | no")> <ATTLIST membership (%bool;)> XML Introdution
48
Overall structure An XML document may start with one or more processing instructions (PIs) or directives: <?xml version="1.0"?> <?xml-stylesheet type="text/css" href="ss.css"?> Following the directives, there must be exactly one root element containing all the rest of the XML: <weatherReport> </weatherReport>
49
XML building blocks Aside from the directives, an XML document is built from: elements: high in <high scale="F">103</high> tags, in pairs: <high scale="F">103</high> attributes: <high scale="F">103</high> entities: <afternoon>Sunny & hot</afternoon> character data, which may be: parsed (processed as XML)--this is the default unparsed (all characters stand for themselves)
50
Elements and attributes
Attributes and elements are somewhat interchangeable Example using just elements: <name> <first>David</first> <last>Matuszek</last> </name> Example using attributes: <name first="David" last="Matuszek"></name> You will find that elements are easier to use in your programs--this is a good reason to prefer them Attributes often contain metadata, such as unique IDs Generally speaking, browsers display only elements (values enclosed by tags), not tags and attributes
51
Well-formed XML Every element must have both a start tag and an end tag, e.g. <name> ... </name> But empty elements can be abbreviated: <break />. XML tags are case sensitive XML tags may not begin with the letters xml, in any combination of cases Elements must be properly nested, e.g. not <b><i>bold and italic</b></i> Every XML document must have one and only one root element The values of attributes must be enclosed in single or double quotes, e.g. <time unit="days"> Character data cannot contain < or &
52
Entities Five special characters must be written as entities:
& for & (almost always necessary) < for < (almost always necessary) > for > (not usually necessary) " for " (necessary inside double quotes) ' for ' (necessary inside single quotes) These entities can be used even in places where they are not absolutely required These are the only predefined entities in XML
53
XML declaration The XML declaration looks like this: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> The XML declaration is not required by browsers, but is required by most XML processors (so include it!) If present, the XML declaration must be first--not even whitespace should precede it Note that the brackets are <? and ?> version="1.0" is required (this is the only version so far) encoding can be "UTF-8" (ASCII) or "UTF-16" (Unicode), or something else, or it can be omitted standalone tells whether there is a separate DTD
54
XML Encoding XML documents can contain international characters, like Norwegian æøå, or French êèé. To avoid errors, you should specify the encoding used, or save your XML files as UTF-8. Unicode is an industry standard for character encoding of text documents. It defines (nearly) every possible international character by a name and a number. Unicode has two variants: UTF-8 and UTF-16. UTF = Universal character set Transformation Format.
55
XML Encoding UTF-8 uses a single byte (8-bits) to represent commonly used characters and two (or three) bytes for the rest. UTF-16 uses two bytes (16 bits) for most characters, and three bytes for the rest. UTF-8 Web Standard UTF-8 is the standard character encoding on the web. UTF-8 is the default character encoding for HTML-5, CSS, JavaScript, PHP, SQL, and XML.
56
Processing instructions
PIs (Processing Instructions) may occur anywhere in the XML document (but usually first) A PI is a command to the program processing the XML document to handle it in a certain way XML documents are typically processed by more than one program Programs that do not recognize a given PI should just ignore it General format of a PI: <?target instructions?> Example: <?xml-stylesheet type="text/css" href="mySheet.css"?>
57
Comments <!-- This is a comment in both HTML and XML -->
Comments can be put anywhere in an XML document Comments are useful for: Explaining the structure of an XML document Commenting out parts of the XML during development and testing Comments are not elements and do not have an end tag The blanks after <!-- and before --> are optional The character sequence -- cannot occur in the comment The closing bracket must be --> Comments are not displayed by browsers, but can be seen by anyone who looks at the source code
58
CDATA By default, all text inside an XML document is parsed
You can force text to be treated as unparsed character data by enclosing it in <![CDATA[ ]]> Any characters, even & and <, can occur inside a CDATA Whitespace inside a CDATA is (usually) preserved The only real restriction is that the character sequence ]]> cannot occur inside a CDATA CDATA is useful when your text has a lot of illegal characters (for example, if your XML document contains some HTML text)
59
Names in XML Names (as used for tags and attributes) must begin with a letter or underscore, and can consist of: Letters, both Roman (English) and foreign Digits, both Roman and foreign . (dot) - (hyphen) _ (underscore) : (colon) should be used only for namespaces Combining characters and extenders (not used in English)
60
Namespaces Recall that DTDs are used to define the tags that can be used in an XML document An XML document may reference more than one DTD Namespaces are a way to specify which DTD defines a given tag XML, like Java, uses qualified names This helps to avoid collisions between names Java: myObject.myVariable XML: myDTD:myTag Note that XML uses a colon (:) rather than a dot (.)
61
Review of XML rules Start with <?xml version="1"?>
XML is case sensitive You must have exactly one root element that encloses all the rest of the XML Every element must have a closing tag Elements must be properly nested Attribute values must be enclosed in double or single quotation marks There are only five predeclared entities
62
Another well-structured example
<novel> <foreword> <paragraph> This is the great American novel </paragraph> </foreword> <chapter number="1"> <paragraph>It was a dark and stormy night </paragraph> <paragraph>Suddenly, a shot rang out! </paragraph> </chapter> </novel>
63
Valid XML You can make up your own XML tags and attributes, but...
...any program that uses the XML must know what to expect! A DTD (Document Type Definition) defines what tags are legal and where they can occur in the XML An XML document does not require a DTD XML is well-structured if it follows the rules given earlier In addition, XML is valid if it declares a DTD and conforms to that DTD A DTD can be included in the XML, but is typically a separate document Errors in XML documents will stop XML programs Some alternatives to DTDs are XML Schemas and RELAX NG
64
Viewing XML XML is designed to be processed by computer programs, not to be displayed to humans Nevertheless, almost all current browsers can display XML documents They don’t all display it the same way They may not display it at all if it has errors For best results, update your browsers to the newest available versions Remember: HTML is designed to be viewed, XML is designed to be used
65
Displaying your XML Files with CSS
With CSS (Cascading Style Sheets) you can add display information to an XML document. <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="cd_catalog.css"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> </CATALOG>
66
CATALOG { background-color: #ffffff; width: 100%; } CD display: block; margin-bottom: 30pt; margin-left: 0;
67
TITLE { color: #FF0000; font-size: 20pt; } ARTIST color: #0000FF;
68
COUNTRY,PRICE,YEAR,COMPANY
{ display: block; color: #000000; margin-left: 20pt; }
69
Easily Processed Why Is XML Important? Plain Text Easy to edit
Platform independent Data Identification Tell you what kind of data you have Can be used in different ways by different applications Easily Processed Vendor-neutral standard XML Introdution
70
Inherently style-free XSL---Extensible Stylesheet Language
Why Is XML Important? Stylability Inherently style-free XSL---Extensible Stylesheet Language Different XSL formats can then be used to display the same data in different ways Inline Reusability Can be composed from separate entities Modularize your documents XML Introdution
71
Linkability -- XLink and XPointer Simple unidirectional hyperlinks
Why is XML important? Linkability -- XLink and XPointer Simple unidirectional hyperlinks Two-way links Multiple-target links “Expanding” links Hierarchical Faster to access Easier to rearrange XML Introdution
72
XML Application1—Separate data
XML can Separate Data from HTML Store data in separate XML files Using HTML for layout and display Using Data Islands Data Islands can be bound to HTML elements Benefits: Changes in the underlying data will not require any changes to your HTML XML Introdution
73
XML Application2—Exchange data
XML is used to Exchange Data Text format Software-independent, hardware-independent Exchange data between incompatible systems, given that they agree on the same tag definition. Can be read by many different types of applications Benefits: Reduce the complexity of interpreting data Easier to expand and upgrade a system XML Introdution
74
XML Application3—Store Data
XML can be used to Store Data Plain text file Store data in files or databases Application can be written to store and retrieve information from the store Other clients and applications can access your XML files as data sources Benefits: Accessible to more applications XML Introdution
75
XML Application4—Create new language
XML can be used to Create new Languages WML (Wireless Markup Language) used to markup Internet applications for handheld devices like mobile phones (WAP) MusicXML used to publishing musical scores RSS. MathML. XML Introdution
76
Overview 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> </PRICE> </PURCHASE_ORDER>
77
Overview Benefits of XML
Open W3C standard Representation of data across heterogeneous environments Cross platform Allows for high degree of interoperability Strict rules Syntax Structure Case sensitive
78
Overview Who Uses XML? Submissions by Technologies using XML Microsoft
IBM Hewlett-Packard Fujitsu Laboratories Sun Microsystems Netscape (AOL), and others… Technologies using XML SOAP, ebXML, BizTalk, WebSphere, many others…
79
Syntax and Structure Components of an XML Document
Elements Each element has a beginning and ending tag <TAG_NAME>...</TAG_NAME> Elements can be empty (<TAG_NAME />) Attributes Describes an element; e.g. data type, data range, etc. Can only appear on beginning tag Processing instructions Encoding specification (Unicode by default) Namespace declaration Schema declaration
80
Syntax and Structure Components 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> Elements with Attributes Elements Prologue (processing instructions)
81
Syntax and Structure Rules 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 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
82
Comp Sci 346 12/28/2017
83
Comp Sci 346 12/28/2017
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.