Sunteți pe pagina 1din 30

c 


 

XML is a new type of language which has been developed for the web which is different to any
other type of scripting or programming language available before. Instead of being concerned
with the processing and display of data, XML's primary purpose is to tell the computer what data
entered actually means.

 c

There are two main reasons for the development of XML:

1. omputers do not understand the information placed in them.. For example there is no
way for a search engine, or any other computer,
to know that this is page contains the introduction part of an XML tutorial. All it is is a
collection of letters and numbers, with HTML formatting around it. The computer cannot
even tell what on this page is a heading, what is text and what is an advert. This is the
main problem which XML was designed to overcome. If a page or document is written in
XML, a computer can understand exactly what it is about. As will probably be obvious,
this has very major implications for search engine technology. If a search engine knew
exactly what was on a page, it would be able to instantly provide the exact results a
person was looking for, with no inaccurate matches and no half-relevant pages. This is
just the revolution the over-bloated web needs.

2. Web pages are not compatible across different devices. One of the major difficulties that
web designers have today is that people are now accessing the pages from a variety of
different devices. Ps, Macs, mobile phones, palmtop computers and even televisions.
Because of this, web designers must now either produce their pages in several different
formats to cope with this, or they must cut back on the design in order to have the page
compatible across the different formats. Because XML is used to define what data means
and not how it is displayed, it makes it very easy to use the same data on several different
platforms.

ë 

So what actually is XML? The thing about it which people find the most difficult to understand
is that XML does not actually do anything. XML is not a way to design your home page and it
won't change the way in which you build sites. This has made many people believe that XML is
useless, as they can't see a way that it will benefit them. XML has a wide variety of benefits
though, two of which were outlined above.
The real use of XML, though, is to describe data. It is used, in a similar way in which HTML is,
except for the fact that there is a major difference between the two:

HTML is used to describe how data is formatted.

XML is used to describe what data actually means.

  

As mentioned above, XML looks, and is structured very similarly to HTML. They both use the
system where tags are used to enclose the data they refer to. They both can use nested tags and
both can also have attributes added to their tags.

The most revolutionary thing about XML, though is that you are not restricted to just using the
normal, pre-defined tags like font and br. Instead you are responsible for making up the tags
yourself. You can name them anything you like and can use them to represent anything you like.
This is a feature which cannot be found in any other scripting language on the web.
 
  

The answer to this, in short, is no. The only thing you have to learn about XML is how to
structure your tags, and they are in fact almost identical to HTML tags. Most of it is just logical
thinking. Before learning XML it is important that you already know HTML. It is also useful if
you know a web scripting language such as PHP, ASP or JavaScript. If you do not yet know
these try some of the tutorials on the site. If you are looking to be able to format a web page, not
describe data, you will be better of learning XHTML, the new standard replacing H
TML.

c 

 

As you will have read in part I, the way in which XML is written is very similar to HTML. They
both use the same system of enclosing pieces of information or data in tags to apply formatting
(in the case of HTML or data rules (in the case of XML) to it.

 

The tags used in XML, as well as being very similar in construction to HTML, also look like
HTML tags. They are formed by a word (or a number of words) enclosed inside <> and </>
signs. Just like, for example the <font></font> tag in HTML. The difference, of course, though is
that XML tags are not pre-defined like HTML ones are. An example could be the XML tag
<message> and the end tag </message> which could be used to enclose an e-mail message stored
on a web based e-mail system.

 
 

Much like HTML tags, XML tags can be nested. Using the example of the e-mail above, this is a
piece of XML code:
<message>
<header>
<from>webmaster@gowansnet.com</from>
<to>webmaster@xml.org</to>
<subject>omments on XML</subject>
</header>
<body>
I think that XML has great potential. It will work very well and will help many people to make
much better use of the internet.
</body>
</message>

As you can see. this piece of code includes nested tags. The first element (tag) in the XML code
is the <message> element. This is what is called the root element. It defines the bottom level of
the document and is saying 'This is an e-mail message'. All the other tags are nested inside this
<message> tag. The next tag which appears is the <header> tag. This is saying that the
information contained within it is the e-mail header. This also has nested tags. for example the
<subject> tag, which appears as part of the header tag. as the subject is part of the header.
Something which is often done in HTML is incorrect nesting. For example: the code:
<b><i>Bold and italic</b></i> would work correctly in a web browser. even though the italic
tags should both be inside the bold tags. This must not be done in XML. It is very important that
all XML tags are correctly nested.




Another point which should be brought up now, is the strictness of XML when writing code. The
whole idea of XML is that it should be independent of the platform it is running on. The same
code should run the same way on a P, a Mac, a mobile phone and even a toaster. As XML does
not actually do anything (it is just a language for defining data), it is up to software developers to
make software to use this data on a particular platform. This means that it is important that all
XML code is structured the same way, so that software can easily be developed. Because of this
requirement for correct code, it has been decided (and is now a standard) that if any mistakes (for
example incorrectly nested tags) are found in XML code, it will not execute, and will just give an
error message. This means that when writing XML, you must be very careful about correct
syntax.


  

The final part of the XML syntax you should learn just now is how to declare an XML
document. The correct way of doing this is to use the tag:

<?xml version="1.0"?>

This tells whatever software receives this data that you are writing XML and that it should match
the specification for version 1.0. As this is not actually an XML tag it does not require a closing
tag.

c  

 

Now you should know what XML is for and how to write a basic XML document. In this part I
will show you how to create a full XML document and load it in a browser, as well and the
different ways it can be displayed.

 !  


reating your XML document is as easy as making an HTML page. All you need is a text editor
(for example Notepad). reate a new document and enter the XML document into it, for
example, the e-mail message from part 2:

<?xml version="1.0"?>
<message>
<header>
<from>webmaster@gowansnet.com</from>
<to>webmaster@xml.org</to>
<subject>omments on XML</subject>
</header>
<body>
I think that XML has great potential. It will work very well and will help many people to make
much better use of the internet.
</body>
</message>

Then, all you have to do is to save the document with a .xml extension. Now, try loading this file
in your browser.
lick Here To Load The File

This is probably quite a surprising result, whatever browser you are using. I will now cover the
results for both Internet Explorer and Netscape/Mozilla.

"#$

Internet Explorer is probably one of the best browsers for viewing XML pages. It provides a
hierarchical display of the XML file, color coding the elements and allowing you to expand and
collapse the nested elements.

If you don't have Internet Explorer you can see what it looks like in the image below (without the
collapsable elements, though).

This is proably quite surpising to see, as it doesn't look like any other web page you will have
seen before. You may also be surprised that you can't really do much, but this is exactly what
XML is. Some sort of program or code must be written to process the data.


$%& 

The Mozilla and Netscape browsers are not as good as Internet Explorer at supporting XML.
Mozilla, for example, presents the XML data as plain text:

This is also a valid display of XML, because, as you will have noticed from the code above,
there is really no way to tell the browser how to display the data, so it just shows it as plain text.

ë
'

Probably the best way to develop your XML files is to use Internet Explorer. Apart from the fact
that it will provide you with a nicely formatted version of your XML file, it also has another
benefit. If there is an error in your XML file, Internet Explorer provides a helpful message telling
you exactly where the error is and displaying the incorrect piece of code. The latest version of
Mozilla will also do this, although its XML formatting is not as good.

( )   *ë  c 

This is the major problem with XML. With so many browsers around there is no way to
guarantee that your data will be displayed the way you want it (which is the reason why there are
images of the output in this tutorial). Luckily, there are very few occasions where you will want
your users to see the raw XML data, and in most cases a piece of software or a script will process
the data first. For now, processing the data first is really the best course of action to take.
c +

 

As you will have seen in the last part of the tutorial, browsers are not particularly good at
formatting XML, and only the very latest browsers support it at all. Although most of the time
XML will be used to define data, not to display it, there may be occasions where you decide that
you want to format the XML data for viewing. There are three main ways of doing this.



ascading Style Sheets (SS) are one of the more recent web technologies, and are used
extensively for formatting standard HTML pages. If you would like to find out more about
ascading Style Sheets read the tutorial on Free Webmaster Help (see related links).

SS can also be used to format XML documents, though. SS can 'redefine' HTML tags,
allowing them to be presented in different ways. Similarly, it can be used to define how XML
tags are displayed. In this section of the tutorial, I will be using an expanded version of my
earlier e-mail example:

<email>
<message>
<header>
<from>webmaster@gowansnet.com</from>
<to>webmaster@xml.org</to>
<subject>omments on XML</subject>
</header>
<body>
I think that XML has great potential. It will work very well and will help many people to make
much better use of the internet.
</body>
</message>
<message>
<header>
<from>webmaster@gowansnet.com</from>
<to>webmaster@goodsite.com</to>
<subject>An excellent site</subject>
</header>
<body>
I have just visited your site and I think it is amazing. Keep up the good work!
</body>
</message>
</email>

If I wanted to display this on a web page, I could use the following SS code:

email
{
background-color: #ffffff;
width: 100%;
}
message
{
display: block;
background-color: #DDDDDD;
margin-bottom: 30pt;
}
header
{
display: block;
background-color: #999999;
margin-bottom: 10pt;
}
from
{
display: block;
color: #0000FF;
font-size: 12pt;
}
to
{
display: block;
color: #FF0000;
font-size: 12pt;
}
subject
{
display: block;
font-size: 14pt;
font-weight: bold;
}
body
{
display: block;
font size: 12pt;
}

There may be a few pieces of code here that are unfamiliar, so I will just cover them. display:
block; is important as it tells the system to display the data inside this tag as a block on the page,
and most importantly, taking a new line after it. This is also related to the margin-bottom
declaration, which allows a space after pieces of data have been displayed.

The actual format of this SS code is quite simple, though. The XML element name is given,
followed by the formatting data inside curly brackets { }. The easiest way to use this with your
code is to save it as a .css file (which is just a plain text file, which can be made in any text
editor.

Finally, add the following to the beginning of the XML code:

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="estyle.css"?>

The first line is the standard declaration of the XML document. The second line points to the
stylesheet which will format this document (in this case estyle.css).

You can click here to see the output of this (only recent browsers will support this).



XSL stands for eXstensible Stylesheet Language, and is a new language developed to format
XML docuements. For this example, I will use the same XML code from above.

To format the code, you must create an XSL stylesheet. Although XSL is a language in itself, I
will just cover the basics here. The following code goes in a file estyle.xsl:

<?xml version="1.0"?>
<HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt;
background
-color:#FFFFFF">
<xsl:for-each select="email/message">
<xsl:for-each select="header">
<DIV STYLE="background-color:#EEEEEE; padding:4px">
<SPAN STYLE="color:black">To: <xsl:value-of select="to"/></SPAN>
</DIV>
<DIV STYLE="background-color:#EEEEEE; padding:4px">
<SPAN STYLE="color:black">From: <xsl:value-of select="from"/></SPAN>
</DIV>
<DIV STYLE="background-color:#EEEEEE; padding:4px">
<SPAN STYLE="font-weight: bold; color:black"><xsl:value-of select="subject"/></SPAN>
</DIV>
</xsl:for-each>
<DIV STYLE="margin-left:20px; margin-bottom:1em; font-size:10pt">
<xsl:value-of select="body"/>
</DIV>
</xsl:for-each>
</BODY>
</HTML>

At first glance it looks very strange, but really it is just HTML DIV and SPAN tags, combined
with a little XSL code. I won't cover DIV and SPAN tags fully here, as this is not an HTML
tutorial, but the basics of them are that you are assigning areas of the page which contain
formatting. The XSL document is really just an HTML page with a bit of XSL code added to it.
For anyone who has used PHP or another scripting language to output HTML, this will all be
quite familiar. The actual XSL is as follows:

<?xml version="1.0"?>
<HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl">

This is the standard header for an XSL document.

<xsl:for-each select="email/message">

This works just like a for loop in a scripting or programming language. It tells the browser to
loop through all the items <message> inside the <email> tag.
<xsl:for-each select="header">

This is another for loop to go through all the occurences of the <header> tag inside the
<message> tag. In this example of code, there is only one <header> for each message, but this
code needs to be included so that the browser looks inside the <header> tag.

To: <xsl:value-of select="to"/>

This is probably the best feature of XSL over SS. You will have noticed that in the SS
formatted document, all I could do was to display the e-mail addresses at the top of the message.
Using XSL (as it is really just an HTML document with extra coding in it), I can tell the browser
to output To: before the value. The second part of this line tells the browser to output the value of
the tag <to> in the position of the XSL tag.

</xsl:for-each>

This is the end of the loop through the header. At this point the browser looks to see if there is
another <header> in the <message> section of the document. As there is not, it continues.

</xsl:for-each>

The second occurance of this tag tells the browser to loop through to the next <message> tag. As
you can see, it can get difficult to follow your nested loops like this, so often it is helpful to
indent your code.

Finally, add the following to your XML code:

<?xml version="1.0"?>
<?xml:stylesheet type="text/xsl" href="estyle.xsl" ?>

As with the SS, this tells the browser to look for the XSL file estyle.xsl to get formatting
details.

You can view the output of these files here.

   

Another way of formatting XML is to use Data Islands. urrently, only Internet Explorer 5 and
upwards support this, and it is an unofficial standard. Again, I will use the same XML to
demonstrate this. Using this method, you use the unofficial <xml> tag in a normal HTML
document. You can either surround your XML data with <xml> and </xml> or you can embed a
remote file.

To embed data straight into the file you use the folloing format:

<xml id="emails">
XML code goes in here but without first declaration line
</xml>

To embed XML from a remote file use:

<xml id="emails" src="emails.xml">


</xml>

As you will have noticed, you must give an ID to your XML.

Now you have got the XML da


ta into the file, you can format it by normal HTML, but using <span> tags to insert particular
fields. This is an example of formatting the e-mail file:

<html>
<body>
<xml id="emails" src="emaildata.xml"></xml>

<table bgcolor= "#EEEEEE" border="0" datasrc="#emails">


<tr bgcolor="#"><td>To: <span datafld="to"></span></td></tr>
<tr bgcolor="#"><td>From: <span datafld="from"></span></td></tr>
<tr bgcolor="#"><td><b>Subject: <span datafld="subject"></span></b></td></tr>
<tr><td><span datafld="body"></span></td></tr>
</table>
</body>

Although I used the same XML data for this as for all the others, I removed the <header> item as
the data objects only appear to work on the first level of the document.

c ,


 

In the last four parts of this tutorial, I have shown you how to create a basic XML document and
how it can be displayed in the browser. This section explains a few more XML techniques, and
also provides a real-world usage of XML.

  

Attributes are another way of storing data using XML. Up until now, we have just used very
basic tags, surrounding information with tags which describe them. For example, this is the code
we have been using so far:

<message>
<header>
<from>webmaster@gowansnet.com</from>
<to>webmaster@xml.org</to>
<subject>omments on XML</subject>
</header>
<body>
I think that XML has great potential. It will work very well and will help many people to make
much better use of the internet.
</body>
</message>

If you go back to thinking of XML as HTML, you will notice that this is made up completely of
'simple' tags. In HTML varient os tags are used which have attributes, for example to output text
in the Arial font the following code would be used:

<font face="Arial">The text</font>

Similarly, in XML attributes can be used to store data. If I wanted, for example, to get rid of the
subject tags in this example, I could use the following code:

<message subject="omments on XML">


<header>
<from>webmaster@gowansnet.com</from>
<to>webmaster@xml.org</to>
</header>
<body>
I think that XML has great potential. It will work very well and will help many people to make
much better use of the internet.
</body>
</message>

As you can see, I have used the attribute of the tag <message> to store the subject instead of it
having its own tag.
This, although correct XML, would not really be a correct usage of the attributes of a tag. The
attribute is used to give information about what is contained in the tag. Although it could be
argued that it is telling you what the message is about, it would be more correct to provide this
document in the original form, where there is a subject tag.

Although I have said that this would not really be a correct usage, you can use these fully
interchangably, for example all the data for this e-mail message could have been stored as
attributes of the message tag. To really benefit from XML, though, it is probably best to use
attributes as little as possible, and to concentrate on structuring your documents correctly.

 

One problem which becomes apparent when using XML is that the parser parses all data in an
XML document. So in the following:

<body>Sales last year were less than sales this year</body>

Would be fully parsed by the parser, both the tags and text. This does not cause a problem,
though. If this was written as:

<body>Sales last year < Sales this year</body>

This would cause a problem, because the XML parser would read this and think that the less than
sign (<) in the text was the beginning of a new tag, so would cause an error. This can be
overcome, though as, like HTML, XML has a variety of special codes for displaying these
characters. There are 5 in XML:

- 

< &lt;

> &gt;
&amp;
="Arial, Helvetica, sans-serif">&

' &apos;

" &quot;

By using these you can display the correct symbols, and the XML parser still works. So you
could enter this text as:

<body>Sales last year &lt; Sales this year</body>

There are occasions, though, when you will have a lot of these special symbols in one section of
your XML code, for example if you want to display programming code on your site. For this, the
DATA has been invented. This is like the HTML <xmp> tag, which causes the parser to ignore
everything contained in it (so it misses all the special characters, but also will miss any tags
contained in it). It is constructed as follows:

<![DATA[
Text to be ignored
]]>

â ë* 

After reading this whole tutorial, you may still be wondering what the point of XML is. It doesn't
improve the look of your web page and the lack of browser support means that you can't use it as
an alternative to a server-side database. There are uses which have been developed, though,
although it will take a lot more development to make XML a mainstream language.

XMLNews is a system which allows news stories to be stored as XML. By using tags like
<headline>, <byline>, <location> and <story> web pages and software systems can be
developed which will take the XML data and will output it as a correctly formatted web page. In
fact, the same story could be displayed on a WAP phone, news website, headlines news ticker,
news e-mail, SMS message or in a piece of software, all from the same source file. As you can
see, this creates a huge benefit, as a story can be written once by a journalist, but distributed
around the world in many different formats. You can find more information at XMLNews.org.

ë  

XML (eXtensible Markup Language) is a meta-language; that is, it is a language in which other
languages are created. In XML, data is "marked up" with tags, similar to HTML tags. In fact, the
latest version of HTML, called XHTML, is an XML-based language, which means that XHTML
follows the syntax rules of XML.

XML is used to store data or information. This data might be intended to be by read by people or
by machines. It can be highly structured data such as data typically stored in databases or
spreadsheets, or loosely structured data, such as data stored in letters or manuals.

' 

Initially XML received a lot of excitement, which has now died down some. This isn't because
XML is not as useful, but rather because it doesn't provide the Wow! factor that other
technologies, such as HTML do. When you write an HTML document, you see a nicely
formatted page in a browser - instant gratification. When you write an XML document, you see
an XML document - not so exciting. However, with a little more effort, you can make that XML
document sing!

This section discusses some of the major benefits of XML.

(  . 

XML does not really do much of anything. Rather, developers can create XML-based languages
that store data in a structure way. Applications can then use this data to do any number of things.
$  
 /  

One of the difficulties with HTML documents, word processor documents, spreadsheets, and
other forms of documents is that they mix structure with formatting. This makes it difficult to
manage content and design, because the two are intermingled.

As an example, in HTML, there is a <u> tag used for underlining text. Very often, it is used for
emphasis, but it also might be used to mark a book title. It would be very difficult to write an
application that searched through such a document for book titles.

In XML, the book titles could be placed in <book_title> tags and the emphasized text could be
place in <em> tags. The XML document does not specify how the content of either tag should be
displayed. Rather, the formatting is left up to an external stylesheet. Even though the book titles
and emphasized text might appear the same, it would be relatively straight forward to write an
application that finds all the book titles. It would simply look for text in <book_title> tags. It also
becomes much easier to reformat a document; for example, to change all emphasized text to be
italicized rather than underlined, but leave book titles underlined.

c    

Very often, applications that hold data in different structures must share data with one another. It
can be very difficult for a developer to map the different data structures to each other. XML can
serve as a go between. Each application's data structure is mapped to an agreed-upon XML
structure. Then all the applications share data in this XML format. Each application only has to
know two structures, its own and the XML structure, to be able to share data with many other
applications.

 (  0â  

XML documents are (or can be) read by people. Perhaps this doesn't sound so exciting, but
compare it to data stored in a database. It is not easy to browse through a database and read
different segments of it as you would a text file. Take a look at the XML document below.
 $1' 
%%c 2#
<?xml version="1.0"?>
<person>
<name>
<firstname>Paul</firstname>
<lastname>Mcartney</lastname>
</name>
<job>Singer</job>
<gender>Male</gender>
</person>
ode Explanation

It is not hard to tell from looking at this that the XML is describing a person named Paul
Mcartney, who is a singer and is male.

Do people read XML documents? Programmers do (hey, we're people too!). And it is easier for
us if the documents we work with are easy to read.

 /

XML doesn't cost anything to use. It can be written with a simple text editor or one of the many
freely available XML authoring tools, such as XML Notepad. In addition, many web
development tools, such as Dreamweaver and Visual Studio .NET have built-in XML support.
There are also many free XML parsers, such as Microsoft's MSXML (downloadable from
microsoft.com) and Xerces (downloadable at apache.org).

 c



  

Almost all of the leading content management systems use XML in one way or another. A
typical use would be to store a company's marketing content in one or more XML documents.
These XML documents could then be transformed for output on the Web, as Word documents,
as PowerPoint slides, in plain text, and even in audio format. The content can also easily be
shared with partners who can then output the content in their own formats.

Storing the content in XML makes it much easier to manage content for two reasons.

1. ontent changes, additions, and deletions are made in a central location and the changes
will cascade out to all formats of presentation. There is no need to be concerned about
keeping the Word documents in sync with the website, because the content itself is
managed in one place and then transformed for each output medium.
2. Formatting changes are made in a central location. To illustrate, suppose a company had
many marketing web pages, all of which were produced from XML content being
transformed to HTML. The format for all of these pages could be controlled from a single
XSLT and a sitewide formatting change could be made modifying that XSLT.

ë3


XML Web services are small applications or pieces of applications that are made accessible on
the Internet using open standards based on XML. Web services generally consist of three
components:

O SOAP - an XML-based protocol used to transfer Web services over the Internet.
O WSDL (Web Services Description Language) - an XML-based language for describing a
Web service and how to call it.
O Universal Discovery Description and Integration (UDDI) - the yellow pages of Web
services. UDDI directory entries are XML documents that describe the Web services a
group offers. This is how people find available Web services.

â/%â/

RDF (Resource Description Framework) is a framework for writing XML-based languages to


describe information on the Web (e.g, web pages). RSS (RDF Site Summary) is an
implementation of this framework; it is a language that adheres to RDF and is used to describe
web content. Website publishers can use RSS to make content available as a "feed", so that web
users can access some of their content without actually visiting their site. Often, RSS is used to
provide summaries with links to the company's website for additional information.




An XML document is made up of the following parts.

O An optional prolog.
O A document element, usually containing nested elements.
O Optional comments or processing instructions.

c

The prolog of an XML document can contain the following items.

O An XML declaration
O Processing instructions
O omments
O A Document Type Declaration


   

The XML declaration, if it appears at all, must appear on the very first line of the document with
no preceding white space. It looks like this.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

This declares that the document is an XML document. The version attribute is required, but the
encoding and standalone attributes are not. If the XML document uses any markup declarations
that set defaults for attributes or declare entities then standalone must be set to no.

c
 
 

Processing instructions are used to pass parameters to an application. These parameters tell the
application how to process the XML document. For example, the following processing
instruction tells the application that it should transform the XML document using the XSL
stylesheet beatles.xsl.

<?xml-stylesheet href="beatles.xsl" type="text/xsl"?>

As shown above, processing instructions begin with and <? end with ?>.



omments can appear throughout an XML document. Like in HTML, they begin with <!-- and
end with -->.

<!--This is a comment-->


 -$
   

The Document Type Declaration (or DOTYPE Declaration) has three roles.

1. It specifies the name of the document element.


2. It may point to an external Document Type Definition (DTD).
3. It may contain an internal DTD.

The DOTYPE Declaration shown below simply states that the document element of the XML
document is beatles.

<!DOTYPE beatles>

If a DOTYPE Declaration points to an external DTD, it must either specify that the DTD is on
the same system as the XML document itself or that it is in some public location. To do so, it
uses the keywords SYSTEM and PUBLI. It then points to the location of the DTD using a
relative Uniform Resource Indicator (URI) or an absolute URI. Here are a couple of examples.

Syntax

<!--DTD is on the same system as the XML document-->


<!DOTYPE beatles SYSTEM "dtds/beatles.dtd">
Syntax

<!--DTD is publicly available-->


<!DOTYPE beatles PUBLI "-//Webucator//DTD Beatles 1.0//EN"
"http://www.webucator.com/beatles/DTD/beatles.dtd">

As shown in the second declaration above, public identifiers are divided into three parts:

1. An organization (e.g, Webucator)


2. A name for the DTD (e.g, Beatles 1.0)
3. A language (e.g, EN for English)

"

Every XML document must have at least one element, called the document element. The
document element usually contains other elements, which contain other elements, and so on.
Elements are denoted with tags. Let's look again at the Paul.xml.

 $1' 
%%c 2#
<?xml version="1.0"?>
<person>
<name>
<firstname>Paul</firstname>
<lastname>Mcartney</lastname>
</name>
<job>Singer</job>
<gender>Male</gender>
</person>
ode Explanation

The document element is person. It contains three elements: name, job and gender. Further, the
name element contains two elements of its own: firstname and lastname. As you can see, XML
elements are denoted with tags, just as in HTML. Elements that are nested within another
element are said to be children of that element.
"$-"

Not all elements contain other elements or text. For example, in XHTML, there is an img
element that is used to display an image. It does not contain any text or elements within it, so it is
called an empty element. In XML, empty elements must be closed, but they do not require a
separate close tag. Instead, they can be closed with a forward slash at the end of the open tag as
shown below.

<img src="images/paul.jpg"/>

The above code is identical in funciton to the code below.

<img src="images/paul.jpg"></img>

  

XML elements can be further defined with attributes, which appear inside of the element's open
tag as shown below.

Syntax

<name title="Sir">
<firstname>Paul</firstname>
<lastname>Mcartney</lastname>
</name>

 

Sometimes it is necessary to include sections in an XML document that should not be parsed by
the XML parser. These sections might contain content that will confuse the XML parser, perhaps
because it contains content that appears to be XML, but is not meant to be interpreted as XML.
Such content must be nested in DATA sections. The syntax for DATA sections is shown
below.

Syntax
<![DATA[
This section will not get parsed
by the XML parser.
]]>

ë $


In XML data, there are only four white space characters.

1. Tab
2. Line-feed
3. arriage-return
4. Single space

There are several important rules to remember with regards to white space in XML.

1. White space within the content of an element is significant; that is, the XML processor
will pass these characters to the application or user agent.
2. White space in attributes is normalized; that is, neighboring white spaces are condensed
to a single space.
3. White space in between elements is ignored.

#1$
  

The xml:space attribute is a special attribute in XML. It can only take one of two values: default
and preserve. This attribute instructs the application how to treat white space within the content
of the element. Note that the application is not required to respect this instruction.

- #â 

XML has relatively straightforward, but very strict, syntax rules. A document that follows these
syntax rules is said to be well-formed.

1. There must be one and only one document element.


2. Every open tag must be closed.
3. If an element is empty, it still must be closed.
? Poorly-formed: <tag>

? Well-formed: <tag></tag>

? Also well-formed: <tag />

4. Elements must be properly nested.


? Poorly-formed: <a><b></a></b>

? Well-formed: <a><b></b></a>

5. Tag and attribute names are case sensitive.


6. Attribute values must be enclosed in single or double quotes.

$
 


There are five special characters that can not be included in XML documents. These characters
are replaced with predefined entity references as shown in the table below.

Special haracters

 
" -â


< &lt;

> &gt;

& &amp;

" &quot;

' &apos;

    $/ 

The following is relatively simple XML file describing the Beatles.


 $1' 
%%' 2#
<?xml version="1.0"?>
<beatles>
<beatle link="http://www.paulmccartney.com">
<name>
<firstname>Paul</firstname>
<lastname>Mcartney</lastname>
</name>
</beatle>
<beatle link="http://www.johnlennon.com">
<name>
<firstname>John</firstname>
<lastname>Lennon</lastname>
</name>
</beatle>
<beatle link="http://www.georgeharrison.com">
<name>
<firstname>George</firstname>
<lastname>Harrison</lastname>
</name>
</beatle>
<beatle link="http://www.ringostarr.com">
<name>
<firstname>Ringo</firstname>
<lastname>Starr</lastname>
</name>
</beatle>
<beatle link="http://www.webucator.com" real="no">
<name>
<firstname>Nat</firstname>
<lastname>Dunn</lastname>
</name>
</beatle>
</beatles>


S-ar putea să vă placă și