Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Introduction. What is XML? XML stands for eXtensible Markup Language XML stands for eXtensible Markup Language XML is a markup language much like.

Similar presentations


Presentation on theme: "XML Introduction. What is XML? XML stands for eXtensible Markup Language XML stands for eXtensible Markup Language XML is a markup language much like."— Presentation transcript:

1 XML Introduction

2 What is XML? XML stands for eXtensible Markup Language XML stands for eXtensible Markup Language XML is a markup language much like HTML that is used to describe data XML is a markup language much like HTML that is used to describe data XML tags are not predefined - you must define your own tags XML tags are not predefined - you must define your own tags XML uses a Document Type Definition (DTD) or an XML Schema to describe the data 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 with a DTD or XML Schema is designed to be self-descriptive

3 XML vs HTML XML was designed to carry data XML was designed to carry data HTML and XML were designed with different goals HTML and XML were designed with different goals HTML is about displaying information, while XML is about describing information HTML is about displaying information, while XML is about describing information

4 XML example <note> Tom Tom Jane Jane Reminder Reminder Meeting Meeting </note> The XML example was created to structure and store information. It does not DO anything because someone must write programs to send, receive or display it. The XML example was created to structure and store information. It does not DO anything because someone must write programs to send, receive or display it.

5 XML example (cont.) New tags and the corresponding document structure are “invented” in our example New tags and the corresponding document structure are “invented” in our example

6 How XML complements HTML XML separates Data from HTML XML separates Data from HTML Data can be exchanged between incompatible systems Data can be exchanged between incompatible systems With XML, plain text files can be used to share data With XML, plain text files can be used to share data XML can also be used to create new languages, such as the Wireless Markup Language (WML) XML can also be used to create new languages, such as the Wireless Markup Language (WML)

7 XML Elements <note> Tom Tom Jane Jane Reminder Reminder Meeting Meeting </note> The first line in the document – the XML declaration – defines the XML version and character encoding used in the document The first line in the document – the XML declaration – defines the XML version and character encoding used in the document

8 XML Elements (cont.) In the example, the document conforms to the 1.0 specification of XML and uses the ISO- 8859-1 (Latin-1/West European) character set In the example, the document conforms to the 1.0 specification of XML and uses the ISO- 8859-1 (Latin-1/West European) character set The next line describes the root element of the document The next line describes the root element of the document The next 4 lines describe 4 child elements of the root:,, and The next 4 lines describe 4 child elements of the root:,, and And finally the last line ends the root element And finally the last line ends the root element

9 XML Elements (cont.) All XML elements must have a closing tag All XML elements must have a closing tag In HTML, some elements do not have to have a closing tag, for example: In HTML, some elements do not have to have a closing tag, for example: This is a paragraph This is a paragraph XML tags are case sensitive, whereas HTML tags are case insensitive XML tags are case sensitive, whereas HTML tags are case insensitive All XML elements must be properly nested and improper nesting of tags makes no sense to XML. All XML elements must be properly nested and improper nesting of tags makes no sense to XML. This is bold and italic - Incorrect This is bold and italic - Incorrect This is bold and italic - Correct This is bold and italic - Correct

10 XML Elements (cont.) All XML documents must have a root element All XML documents must have a root element All other elements are within this root element All other elements are within this root element All elements can have sub elements (child elements) that are properly nested within their parent element: All elements can have sub elements (child elements) that are properly nested within their parent element:<root><child><subchild>….</subchild></child></root>

11 XML Elements (cont.) XML elements can have attributes in name/value pairs XML elements can have attributes in name/value pairs The attribute value must always be quoted The attribute value must always be quoted <to>Tom</to></note> The syntax for writing comments in XML is similar to that of HTML: The syntax for writing comments in XML is similar to that of HTML:

12 XML Elements (cont.) XML elements have relationships XML elements have relationships For example, a hierarchy such as: For example, a hierarchy such as: My First XML Introduction to XML What is HTML What is XML XML Syntax Elements must have a closing tag Elements must be properly nested

13 XML Elements (cont.) Can be organized into: Can be organized into:<book> My First XML My First XML Introduction to XML Introduction to XML What is HTML What is HTML What is XML What is XML </chapter> XML Syntax XML Syntax Elements must have a closing tag Elements must have a closing tag Elements must be properly nested Elements must be properly nested </chapter></book>

14 Element Naming XML elements must follow these naming rules: XML elements must follow these naming rules: Names can contain letters, numbers, and other characters Names can contain letters, numbers, and other characters Names must not start with a number or punctuation character Names must not start with a number or punctuation character Names must not start with the letters xml (or XML or Xml..) Names must not start with the letters xml (or XML or Xml..) Names cannot contain spaces Names cannot contain spaces

15 Element Naming (cont.) Non-English letters like 轵 are perfectly legal in XML element names, but watch out for problems if the software vendor doesn't support them Non-English letters like 轵 are perfectly legal in XML element names, but watch out for problems if the software vendor doesn't support them The ":" should not be used in element names because it is reserved for namespaces (discussed later) The ":" should not be used in element names because it is reserved for namespaces (discussed later)

16 "Well Formed" XML documents XML with correct syntax is Well Formed XML XML with correct syntax is Well Formed XML XML validated against a Document Type Definition (DTD) is Valid XML XML validated against a Document Type Definition (DTD) is Valid XML A DTD defines the legal elements of an XML document A DTD defines the legal elements of an XML document A Schema is an alternative to a DTD but we are not going to cover it because Greenstone does not use XML Schema A Schema is an alternative to a DTD but we are not going to cover it because Greenstone does not use XML Schema

17 Introduction to DTD The purpose of a Document Type Definition is to define the legal building blocks of an XML document The purpose of a Document Type Definition is to define the legal building blocks of an XML document A DTD defines the document structure with a list of legal elements A DTD defines the document structure with a list of legal elements A DTD can be declared inline in your XML document or as an external reference A DTD can be declared inline in your XML document or as an external reference If a DTD is included in your XML document, it should be wrapped in a DOCTYPE definition: If a DTD is included in your XML document, it should be wrapped in a DOCTYPE definition: - Inline - Inline - External Reference - External Reference

18 Introduction to DTD (cont.) An Inline DTD example: An Inline DTD example: 1: 1: 2: <!DOCTYPE note [ 3: 3: 4: 4: 5: 5: 6: 6: 7: 7: 8: ]> 9: 9: 10: Tom 10: Tom 11: Jane 11: Jane 12: Reminder 12: Reminder 13: Don't forget me this weekend! 13: Don't forget me this weekend! 14: 14:

19 Introduction to DTD (cont.) !DOCTYPE note (in line 2) defines that this is a document of type note !DOCTYPE note (in line 2) defines that this is a document of type note !ELEMENT note (in line 3) defines the note element as having four elements: "to,from,heading,body" !ELEMENT note (in line 3) defines the note element as having four elements: "to,from,heading,body" !ELEMENT to (in line 4) is defined to be of type "#PCDATA". !ELEMENT to (in line 4) is defined to be of type "#PCDATA". !ELEMENT from (in line 5) is defined to be of type "#PCDATA" !ELEMENT from (in line 5) is defined to be of type "#PCDATA" and so on..... and so on.....

20 Introduction to DTD (cont.) External Reference example: External Reference example: <note> Tom Tom Jane Jane <heading>Reminder</heading> Don't forget me this weekend! Don't forget me this weekend!

21 Introduction to DTD (cont.) Note.dtd Note.dtd

22 Introduction to DTD (cont.) PCDATA PCDATA PCDATA is text that will be parsed by a parser PCDATA is text that will be parsed by a parser Tags inside the text will be treated as markup and entities will be expanded Tags inside the text will be treated as markup and entities will be expanded CDATA CDATA CDATA is text that will NOT be parsed by a parser CDATA is text that will NOT be parsed by a parser Tags inside the text will NOT be treated as markup and entities will not be expanded Tags inside the text will NOT be treated as markup and entities will not be expanded

23 Declaring a DTD Element In the DTD, XML elements are declared with an element declaration: In the DTD, XML elements are declared with an element declaration: or or There are various types of elements that we can declare in a DTD There are various types of elements that we can declare in a DTD

24 Types of DTD elements Empty Elements Empty Elements Elements with only character data Elements with only character data Elements with any contents Elements with any contents Elements with children (sequences) Elements with children (sequences) Note that when children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. Note that when children are declared in a sequence separated by commas, the children must appear in the same sequence in the document.

25 Types of DTD elements (cont.) Declaring only one occurrence of the same element Declaring only one occurrence of the same element Declaring minimum one occurrence of the same element Declaring minimum one occurrence of the same element Declaring zero or more occurrences of the same element Declaring zero or more occurrences of the same element Declaring zero or one occurrences of the same element Declaring zero or one occurrences of the same element

26 Types of DTD elements (cont.) Declaring either/or content Declaring either/or content Declaring mixed content Declaring mixed content DTD attributes are declared with an ATTLIST declaration - not covered because Greenstone does not use them DTD attributes are declared with an ATTLIST declaration - not covered because Greenstone does not use them

27 DTD Validation Internet Explorer 5.0 and above can validate your XML against a DTD Internet Explorer 5.0 and above can validate your XML against a DTD An ill-formed or illegal XML document will generate an error An ill-formed or illegal XML document will generate an error

28 Why use a DTD? Each XML file can carry a description of its own format with it Each XML file can carry a description of its own format with it With a DTD, independent groups of people can agree to use a common DTD for interchanging data With a DTD, independent groups of people can agree to use a common DTD for interchanging data A standard DTD can be used to verify that the data one receives from the outside world is valid A standard DTD can be used to verify that the data one receives from the outside world is valid We can also use a DTD to verify our own data We can also use a DTD to verify our own data

29 References DTD Tutorial http://www.w3schools.com/dtd/default.asp DTD Tutorial http://www.w3schools.com/dtd/default.asp http://www.w3schools.com/dtd/default.asp XML Tutorial XML Tutorial http://www.xmlfiles.com/xml/ http://www.w3schools.com/xml/default.asp HTML Tutorial HTML Tutorial http://www.w3schools.com/html/default.asp


Download ppt "XML Introduction. What is XML? XML stands for eXtensible Markup Language XML stands for eXtensible Markup Language XML is a markup language much like."

Similar presentations


Ads by Google