What is XML



What is XML?

XML stands for EXtensible Markup Language

XML is a markup language much like HTML (though they have difference too) XML was designed to describe data

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

From the following simple XML code, you will have a brief idea of the structure of XML.

 

   Cindy

   Mike

   Reminder

   Don't forget to call me this weekend!

   

The very first line defines the XML version and the character encoding used in the document. In this case, the document conforms to 1.0 specification of XML and uses the ISo-8859-1(Latin-1/West European) character set. The second line is comment an the third line describe the root elements of the document. The following four lines describe four child elements of the root. And the last line defines the end of the root ().

From this example, we will talk about the next topic:

Well formed XML (XML syntax rules)

1. Every element must have a CLOSING TAG.

Example: Cindy is right while Cindy is not right.

2. XML tags are CASE SENSITIVE.

Example: is different from

  Don't forget to call me this weekend! is INCORRECT

3. No overlapping tags

Example: This text is bold and italic is not right while

This text is bold and italic is right.

4. All XML documents must have a root element. (All XML documents must contain a single tag pair to define a root element.)

Example: In the previous simple code example, and defines the root element.

5. Attribute values must always be quoted (but either single or double quotes can be used)

Example: Make a little change to the above code:

 

   Cindy

   Mike

   Reminder

   Don't forget to call me this weekend!

   

The date attribute of the note element is not quoted and is not correct. The correct syntax should be

6. White Space is reserved in XML

Unlike HTML, white space can not be stripped off in XML.

Example:

Hi How are you doing?

This sentence will display in XML with all the spaces in between “Hi” and “How” while in HTML, it will display just like: Hi How are you doing?

7. XML elements must follow these naming rules:

• Names can contain letters, numbers, and other characters

• Names must not start with a number or punctuation character ( is incorrect)

• Names must not start with the letters xml (or XML or Xml , is invalid)

• Names cannot contain spaces ( is invalid)

Display XML

As you can see from the above example, XML document is much like some describing text file with self-defined tags. In fact, if you try to see the .xml file using your IE or Netscape browser, it will just show you the plain text with all the tags and comments in your code. XML itself can not be viewed as some web page.

In fact, there are different solutions to display XML, and I will give you three major ones in the following: CSS, XSL and JavaScript

CSS is Cascading Style Sheets, it is a separate file named in .css

To display XML file using CSS, just add another line of code in XML to include the css file.

and the cd_catalog.css file looks like this:

CATALOG

{

background-color: #ffffff;

width: 100%;

}

CD

{

display: block;

margin-bottom: 30pt;

margin-left: 0;

}

TITLE

{

color: #FF0000;

font-size: 20pt;

}

ARTIST

{

color: #0000FF;

font-size: 20pt;

}

COUNTRY,PRICE,YEAR,COMPANY

{

Display: block;

color: #000000;

margin-left: 20pt;

}

In this way, the modified xml file will display like this:

[pic]

You can compare to the original xml file display without css file.

[pic]

The second solution to display XML file is using XSL.

XSL stands for Extensible Stylesheet Language.

Following is an XSL file:

 

  My CD Collection

Title

  Artist

 

(select records with artist equal to ‘Bob Dylan’)

(select price greater than 10)

 

 

 

 

 

 

 

 

 

 

 

In the above XSL code, the line

declares the document to be an XSL style sheet. Notice, either or is correct declaration.

The line describe a template element with certain attribute. The element contains rules to apply when a specified node is matched. The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).

Save the above code as cd_catalog.xsl and include this xsl file in the original XML file as:

The display of the modified XML file with xsl looks like:

[pic]

If you add the “if” displaying condition in your XSL file (the commented lines in the above), the output of the XML will change to

[pic]

which select all the records with cd price greater than 10 dollars.

The third solution for displaying XML is using JavaScirpt. This will be shown in next XML application after we introducing the XML Server.

XML Server

• You can store XML file on your server, in exactly the same way as save a HTML file.

• You can generate XML file with ASP

Use your browser to see the .asp file above; you will get the same result as a plain xml file.

• You can get XML from a database without any pre_installed XML software. For example:

Next, Let us have a look at a XML application using JavaScript and to display.

One XML Application

The following html code will include the cd_catalog.xml we mentioned before. Meanwhile, it will use the JavaScript for a fancy selected display.

function testclick(field)

{

var row=field.rowIndex

xmldso_list.recordset.absoluteposition=row

td_title.innerHTML=xmldso_list.recordset("TITLE")

td_artist.innerHTML=xmldso_list.recordset("ARTIST")

td_year.innerHTML=xmldso_list.recordset("YEAR")

td_country.innerHTML=xmldso_list.recordset("COUNTRY")

td_company.innerHTML=xmldso_list.recordset("COMPANY")

td_price.innerHTML=xmldso_list.recordset("PRICE")

}

Title:

Artist:

Year:

Country:

Company:

Price:

Click on one of the CDs in the list:

Title

Artist

Country

Company

Price

Year

Conclusion:

XML is a universal data exchange format. It is self-defined and extensible.

XML can do client-side processing and manipulation of data

XML can be used in a variety of areas: Electronic Commerce, Creating other markup languages (WML, WAP…), Advanced Search Engine, Web-based Control Systems, etc.

XML has been widely used and will be the most important universal markup language in the near future.

Reference:

1. Applied XML: a toolkit for programmers/Alex Ceponkus, Faraz Hoodbhoy. 1999

2. (find all the examples shown in this lecture and try it yourself)

3. Professional XML and ASP

XML Questions:

Multiple Choices:

1. What does XML stand for?

a. eXtensible Markup Language

b. Example Markup Language

c. X-Markup Language

d. eXtra Modern Link

2. What is the correct syntax of the declaration which defines the XML version?

a.

b.

c.

3. Which statement is true?

a. All XML elements must be lower case

b. All XML documents must have a DTD

c. All XML elements must have a closing tag

d. All the statements are true

4. Which is not a correct name for XML element?

a.

b. All 3 names are incorrect

c.

d.

5. What is a correct way of referring to a stylesheet called “mystyle.xsl”?

a.

b.

c.

True False:

1. XML’s goal is to replace HTML

2. Is this a correct XML document?

Tove

Jani

Reminder

Don't forget me this weekend!

3. Is this a correct XML document?

Tove

Jani

Reminder

Don't forget me this weekend!

4. XML preserve white spaces

5. XML attribute values must always be enclosed in quotes

Fill in Blanks:

1. XSL stands for:______________________________

2. List three of the solutions to display XML file on a browser:____________________,

___________________________, ________________________

3. List the potential areas that XML might be applied (at least two)__________________,

________________, _______________,

4. List three differences between XML and HTML.

___________________________________________,

___________________________________________,

___________________________________________.

5. XML stands for: _____________________________.

Answers:

Multiple choices: 1.a 2. a. 3. c. 4. c. 5.c.

True False: 1. F 2.T 3.F 4.T 5.T

Fill in Blanks:

1. eXtensible Stylesheet Language

2. CSS, JavaScript, XSL

3. Electronic Commerce, Creating other markup Languages, Advanced Search Engines, Web-Based control systems

4. XML must have a closing tag while HTML don’t

XML’s tag can not be overlapping while HTML don’t

XML use “self-defined” tags while HTML use “pre-defined” tags

XML tags are case sensitive while HTML are not.

5. EXtensible Markup Language

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download