Download presentation
Presentation is loading. Please wait.
Published byKrista Piercefield Modified over 10 years ago
1
SE 5145 – eXtensible Markup Language (XML ) Introduction - I 2011-12/Spring, Bahçeşehir University, Istanbul
2
What is XML? eXtensible Markup Language Became a W3C Recommendation in 1998 A framework for defining markup languages Tag-based syntax, very much like HTML No fixed collection of markup tags Enables making up your own tags Each XML language targeted for application All XML languages share features Foundation for several next-gen Web technologies XHTML, RSS (Blogs), AJAX, Web Services 2
3
Why Use XML ? (Advantages) Represent semi-structured data (data that are structured, but don’t fit relational model) XML is more flexible than DBs XML can be used as a way to interchange data between disparate systems that were never designed to do so (e.g. hospitals x banks, etc.) You get a massive infrastructure for free XML documents can be modularized. Parts can be reused. Content is kept separate from any notion of presentation 3
4
Why Use XML ? (Advantages) XML is text (Unicode) based. Takes up less space. Can be transmitted efficiently. One XML document can be displayed differently in different media. Html, video, CD, DVD, You only have to change the XML document in order to change all the rest. Information can be easily read and understood XML is an open format that can be processed by any XML-aware app. like browser, word proc., spreadh.,etc 4
5
Potential Drawbacks of XML 5
6
6 HTML and XML, I 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. This is an element ) Both use tag attributes (e.g., ) Both use entities (<, >, &, ", ' ) More precisely, HTML is defined in SGML XML is a (very small) subset of SGML
7
7 HTML and XML, II 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.,, ) and appearance (e.g.,, ) XML describes only content, or “meaning” HTML uses a fixed, unchangeable set of tags In XML, you make up your own tags HTML is a markup language for a specific purpose (display in browsers) XML is a framework for defining markup languages
8
8 HTML and XML, III HTML is for humans 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
9
9 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
10
10 Extended document standards You can define your own XML tag sets, but here are some already available: XHTML: HTML redefined in XML SMIL: Synchronized Multimedia Integration Language MathML: Mathematical Markup Language SVG: Scalable Vector Graphics DrawML: Drawing MetaLanguage ICE: Information and Content Exchange ebXML: Electronic Business with XML cxml: Commerce XML CBL: Common Business Library
11
11 Vocabulary SGML: Standard Generalized Markup Language XML : Extensible Markup Language DTD: Document Type Definition element: a start and end tag, along with their contents attribute: a value given in the start tag of an element entity: a representation of a particular character or string PI: a Processing Instruction, to possibly be used by a program that processes this XML namespace: a unique string that references a DTD well-formed XML: XML that follows the basic syntax rules valid XML: well-formed XML that conforms to a DTD
12
12 XML-related technologies
13
13 XML-related technologies CSS (Cascading Style Sheets) describe how to display HTML or XML in a browser DTD (Document Type Definition) and XML Schemas are used to define legal XML tags and their attributes for particular purposes DOM (Document Object Model), SAX (Simple API for XML, and JAXP (Java API for XML Processing) are all APIs for XML parsing
14
Applications of XML XHTML CML – chemical markup language WML – wireless markup language ThML – theological markup language Having a Humble Opinion of Self EVERY man naturally desires knowledge Aristotle, Metaphysics, i. 1. ; but what good is knowledge without fear of God? Indeed a humble rustic who serves God is better than a proud intellectual who neglects his soul to study the course of the stars. Augustine, Confessions V. 4. 14
15
SUMMARY WHAT WHO WHEN WHY HOW 15
16
WHAT XML is a language for creating other languages! XML lets you define schemas for tag-based languages (ergo, “markup language”) XML allows you to extend any existing language (schema) with your own tags (ergo, “eXtensible”) Examples of XML schemas financial transactions (stock transactions) business documents (purchase order, invoice) remote procedure calls (SOAP) configuration files (security, server properties) 16
17
WHO 17
18
WHEN The World Wide Web Consortium (W3C) formed an XML Working Group in 1996 with these design goals: 1. XML shall be straightforwardly usable over the Internet 2. XML shall support a wide variety of applications 3. XML shall be compatible with SGML 4. It shall be easy to write programs which process XML documents 5. The number of optional features in XML is to be kept to the absolute minimum, ideally zero 6. XML documents should be human-legible and reasonably clear 7. The XML design should be prepared quickly 8. The design of XML shall be formal and concise 9. XML documents shall be easy to create 10. Terseness in XML markup is of minimal importance 18
19
WHEN Development started in 1996, 1st ed. was published in 1998, 2nd in 2000, 3rd in 2004, 4th in 2006, 5th in 2008 XML 1.0 became a standard (W3C recommendation) on 10 February 1998 effort grew out of experience with SGML cooperation between various W3C member organizations (including Microsoft, Sun, IBM, HP, Adobe) XML 1.1 became a standard (W3C recommendation) on 4 February 2004 less rigid constraints on names addresses issues involving character sets Fifth edition of “W3C Recommendation”: http://www.w3.org/TR/REC-xmlhttp://www.w3.org/TR/REC-xml 19
20
HOW 20
21
HOW 21
22
WHY: Application Integration 22
23
WHY: Application Integration 23
24
WHY: Application Integration 24
25
WHY: Platform-Independent Services In the past, functionality was exposed to remote clients via various remote object standards COM/DCOM CORBA Java RMI Using these remote objects required a significant investment in a platform Web Services provide a new way to expose functionality use XML and XML data types for transport work with any platform provide a bridge to existing business services 25
26
SE 5145 – eXtensible Markup Language (XML ) Introduction - II 2011-12/Spring, Bahçeşehir University, Istanbul
27
1st Assignment Well-formed, valid, and schema-valid documents 1) Find two HTML documents (on a Web server), which are NOT well-formed XML & briefly explain the violations (If you can’ t find any, then find well- formed ones & modify them to make NON-well-formed ;) 2) Find two more documents which, while well-formed but not valid XML. Explain again the fragments of the document and the validity constraint which is violated by this document. Submit hard copy on due date: Next lesson (2 weeks later) Randomly selected students will be asked to present the solutions on the due date 27
28
Example: Describing Information DATALABELS (Info about Data) 28
29
Describing Information with XML 29
30
Real Life Example XML used in a Desktop App., Dreamweaver C:\Program Files\Adobe\Adobe Dreamweaver CS5.5\configuration\Menus\menus.xml 30
31
Tools for working with XML 31
32
XML: Design Goals Separate syntax from semantics to provide a common framework for structuring information Allow tailor-made markup for any imaginable application domain Support internationalization (Unicode) and platform independence Be the future of (semi)structured information (do some of the work now done by databases) 32
33
Basic Structure An XML document is an ordered, labeled tree character data leaf nodes contain the actual data (text strings) element nodes, are each labeled with a name (often called the element type), and a set of attributes, each consisting of a name and a value, can have child nodes 33
34
XML Trees An XML document has a single root node. The tree is a general ordered tree. A parent node may have any number of children. Child nodes are ordered, and may have siblings. Preorder traversals are usually used for getting information out of the tree. 34
35
35 XML Semantics: a Tree ! Mary Maple 345 Seattle John Thailand 23456 Mary Maple 345 Seattle John Thailand 23456 data Mary person name address name address streetnocity Maple345 Seattle John Thai phone 23456 id o555 Element node Text node Attribute node Order matters !!!
36
XML Example 36
37
XML Example FileCab This chapter describes the commands that manage the FileCab inet application. 37
38
38 Overview of XML rules Start with XML is case sensitive There must be exactly single root element (tag) that encloses all the rest of the XML Tags are enclosed in angle brackets and can’t contain ‘<‘ or ‘&’. Tags come in pairs with start-tags and end-tags. Tags must be properly nested. … is not allowed. … is. Tags that do not have end-tags must be terminated by a ‘/’. is an html example. Attribute values must be enclosed in double or single quotation marks
39
39 Overall structure An XML document may start with one or more processing instructions (PIs) or directives: Following the directives, there must be exactly one tag, called the root element, containing all the rest of the XML:...
40
40 Example XML document 7/14/97 North Place, NX USA High Temp: 103 Low Temp: 70 Morning: Partly cloudy, Hazy Afternoon: Sunny & hot Evening: Clear and Cooler From: XML: A Primer, by Simon St. Laurent
41
41 XML building blocks Aside from the directives, an XML document is built from: elements: high in 103 tags, in pairs: 103 attributes: 103 entities: Sunny & hot character data, which may be: parsed (processed as XML)--this is the default unparsed (all characters stand for themselves)
42
42 Basic XML Rules
43
43 Proper XML Syntax
44
44 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
45
45 Well-Formed Documents An XML document is said to be well-formed if it follows all the rules. An XML parser is used to check that all the rules have been obeyed. All of the browsers as Internet Explorer, Firefox, Safar Opera and Chrome come with XML parsers. Parsers are also available for free download over the Internet. One is Xerces, from the Apache open-source project. Java 1.4 and later versions also support an open-source parser. PHP and Javascript also have XML processing capabilities.
46
46 Well-structured XML example This is the great American novel. It was a dark and stormy night. Suddenly, a shot rang out!
47
47 Contents of XML Files
48
48 The XML Declaration
49
49 Encoding XML (like Java) uses Unicode to encode characters. Unicode comes in many flavors. The most common one used in the world is UTF-8. It is backward compatible with ASCII UTF-8 is a variable length (multi byte) code. Characters are encoded in 1 byte, 2 bytes, or 4 bytes. The first 128 characters in Unicode are ASCII. In UTF-8, the numbers between 128 and 255 code for some of the more common characters used in western Europe, such as ã, á, å, ç. Two byte codes are used for some characters not listed in the first 256 and some Asian ideographs. Four byte codes can handle any ideographs that are left. Those using non-western languages should investigate other versions of Unicode (like UTF-16, UTF-32). UTF-8 encodes each of the 1,112,064 code points in the Unicode character set using one to four 8-bit bytes (termed “octets” in the Unicode Standard).
50
50 XML declaration The XML declaration looks like this: 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 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
51
51 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)
52
52 Elements (Tags)
53
Elements Elements are denoted by markup tags thetext Element start tag: foo Attribute: attr1 The character data: thetext Matching element end tag: 53
54
54 Attributes (Exception: by using diff. namespaces)
55
55 Elements and attributes Attributes and elements are somewhat interchangeable Example using just elements: David Matuszek Example using attributes: 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
56
56 Comments
57
57 Comments 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 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
58 Character Data Sections
59
59 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 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)
60
60 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 THESE INSTRUCTIONS ARE PARSER-SPECIFIC !
61
61 Entities
62
62 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
63
63 XML Namespaces
64
64 XML Namespaces
65
65 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 (. )
66
66 Namespaces and URIs A namespace is defined as a unique string To guarantee uniqueness, typically a URI (Uniform Resource Indicator) is used, because the author “owns” the domain It doesn't have to be a “real” URI; it just has to be a unique string Example: http://www.matuszek.org/ns There are two ways to use namespaces: Declare a default namespace Associate a prefix with a namespace, then use the prefix in the XML to refer to the namespace
67
67 Namespace syntax In any start tag you can use the reserved attribute name xmlns : This namespace will be used as the default for all elements up to the corresponding end tag You can override it with a specific prefix You can use almost this same form to declare a prefix: Use this prefix on every tag and attribute you want to use from this namespace, including end tags--it is not a default prefix To Begin You can use the prefix in the start tag in which it is defined:
68
68 White Space Handling
69
69 White Space Handling
70
SE 5145 – eXtensible Markup Language (XML ) Introduction – III (XPath & XSLT) 2011-12/Spring, Bahçeşehir University, Istanbul
71
71 XPath Introduction
72
72 Important XPath Concepts
73
73 XPath Introduction «Location step» html/head/title html/body/p[1] html/body/p (3 nodes)
74
74 Some XPath Examples
75
75 Some XPath Examples AltovaXMLSpy > Evaluate XPath.. businesscard.xml / BusinessCard BusinessCard/Name BusinessCard/Name/text() BusinessCard/phone BusinessCard/phone[2] BusinessCard/phone[last()]
76
76 Starting to Work with XML Your first XML file Associating an XML file with a CSS stylesheet Styling XML tags with CSS Working with XML and CSS2
77
77 Your first XML file This is our first XML file TRY TO OPEN THE FILE WITH A BROWSER, WHAT’S THE PROBLEM ?
78
78 Associating an XML file with a CSS stylesheet CSS: FirstTag { display : block; font-family : Arial; font-size : large; color : Blue; } XML Updated: This is our first XML file
79
79 Styling XML tags with CSS businesscard_nostyle.xml businesscard.css businesscard_style.xml ( ) CSS does not allow to display labels, but CSS2 does.
80
80 Working with XML and CSS2 businesscard_CSS2.css email:before {content: "e-mail: "} phone:before {content: attr(type) ": "} phone[primary]:after {content: " (" attr(primary) ")"} businesscard_style_CSS2.xml
81
81 Working with XML and CSS2
82
82 Using XSLT
83
83 Creating XSLT Stylesheets
84
84 How XSLT Works
85
85 How XSLT Works
86
86 An Example XSLT Template
87
87 Enf of XML Fundamentals Question ?
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.