Language XML
I suggest to be defined{determined} at once with an abbreviation. "XML" it is deciphered as « Extensible Markup Language » (the expanded markup language), from here it is safely possible to draw a conclusion, what is it descendant HTML. A reasonable question: than did not arrange developers HTML and what was an incitement for creation of a modern language? It is enough to recollect applicability HTML: visual data presentation. XML it was developed for their structurization, on what his ancestor has not been designed. HTML concentrates on display of the data and their appearance, and XML - on the description of the data and their structure. From here follows, that XML is not replacement HTML. Before these languages absolutely different problems{tasks} though appearance of a code is similar are put.
In the pragmatical plan shortly to define{determine} XML it is possible as krossplatformennoe, programmno-and hardware - independent means for transfer of the information.
As well as his ancestor, a modern language ("new" - in comparison with HTML as XML has been developed about three years ago) has the tags (tags), named nodami. A difference only that HTML has precisely certain tags (<a>, <p>, <h1>, etc.), new tags appear only at will of developers of standards and browsers. In XML absolutely other picture: the quantity{amount} of the preestablished (service) tags is minimized, and the programmer is free to use own tags and own structure of the document for a data storage.
Let's consider a simple example. We have a pile of books which to us is necessary for structuring (i.e. to classify). What it is necessary for us for the characteristic of the book? Its{her} author, the name, year of publishing house. Our XML-document will look as follows:
<? xml version = " 1.0" encoding = "windows-1251"?>
<library>
<book>
<author> A.Konan-Dojl </author>
<title> Sherlok Holmes </title>
<year> 1996 </year>
</book>
<book>
<author> L.Tolstoj </author>
<title> Anna Karenina </title>
<year> 2001 </year>
</book>
</library>
Notice, in this example it is not specified anywhere as it is necessary to display the information (what color, a font, the size of a font, etc.), and the structure of the document is only shown: there is a library (<library>), in her books (<book>) which characteristics are the author (<author>), the name (<title>) and year of publishing house (<year>) lay.
I suggest to understand with use XML.
The information is stored{kept} in a classical HTML-marking inside a .html-file. When it is necessary for us to change something, we should work simultaneously both with the data, and with rules of their display that is rather inconvenient. XML solves this problem so: we can store{keep} the data in one file (XML), and rules of their performance - in the friend (HTML). However XML allows to store{keep} the data and inside a HTML-file - in ostrovkakh the data (data islands). Nevertheless, the information is still structured and separated from rules of visual performance.
XML allows to communicate to incompatible systems. For today there is a set of standards of storage of the information in different operational systems and databases that does not allow to communicate in different systems. XML successfully solves this problem.
Besides, XML it is stored{kept} in usual text files that allows developers of various programming languages and operational systems to create different applications on the basis of one standard accessible to all.
However it is not necessary to think, that XML does{makes} anything you like. He only enables to store{keep} the data, and programming languages are engaged in management of the data.
Let's return to our example. Before to create the XML-document, it is necessary to understand principles of creation of correctly generated XML-documents (well-formed).
Each document should inform, that for type of the data he contains. That obrabotchik (the program analyzing the document, - in our case browser Internet Explorer 5 +) correctly processed the XML-document, the first line specifies, that the document is generated under version XML 1.0 and has the coding windows-1251. The next line is a root, primary element of the document (root element), is an obligatory component and informs the developer that contains this document (in our case <library> speaks us, that we deal with library).
Not all elements of language HTML demand a closing tag (<br>, <hr>, etc.) Syntax XML demands obligatory closing of tags. Though there are also exceptions: the first line of our example has no closing tag. It speaks that, that this line does not act as a part of the document and the information, and is heading.
XML-tags registrozavisimy. I.e. tags <author> and <Author> will be two different tags; accordingly, both demand the closing tags identical opening, differently a mistake.
As XML it is intended for structurization of the data, a natural condition is clearness of an investment of tags. If in HTML it was possible to write <b> <i> the Text </b> </i> XML demands a precise enclosure: <b> <i> the Text </i> </b>, differently a mistake.
As it was already mentioned, the XML-document should contain a root element (tag), in our case it <library>.
As well as in HTML, in XML tags can have attributes. A difference only that values of attributes should be quoted necessarily (<book language = "rus">, but not <book language=rus>), differently a mistake.
Simultaneously lack and advantage HTML was his ability to ignore double blanks: if we shall write « It is my text » in HTML he will be apprehended as « It is my text ». In XML double blanks are not ignored.
But in connection with comments to a code in XML differs nothing from an ancestor (<! - the Comment->).
It happens, that it is necessary to store{keep} the specific information (for example, a program code), containing significant number specsimvolov (<>, and, ', ”). Certainly, it is possible to replace these symbols with escape-sequences (*lt;, *gt;, *amp;), however it is inconvenient, if it is necessary to store{keep} the whole functions. In XML it is possible to conclude the necessary data in a design <! [CDATA […]]>. The information which is stored{kept} in this section, is not processed by the interpreter therefore mistakes does not cause:
<script>
<! [CDATA [
function doThis (a, b) {
if (a <b ** a <0) {
return 1
}
else {
return 0
}
}
]]>
</script>
It is remarkable, that XML-documents can be supplemented, and it does not influence job of the program processing them. If our program deduces to us the author and the name of the book, and we shall add in the XML-document one more characteristic of the book (for example, quantity{amount} of pages - <pages> </pages>), it will not be reflected in any way at job of the program, in fact still in the characteristic of the book there will be tags necessary to us - <author> and <title>.
In HTML we had some performance about interrelations between tags - for example, <title> should contain inside a tag <head>. As XML has precisely certain structure, interrelations also are differentiated and characterize parental and affiliated elements. In our example the tag <book> is affiliated for a tag <library> and parental for tags <author>, <title>, <year>.
As well as in HTML, in XML there is a concept of contents of tags, only in more differentiated kind. In HTML we could characterize a tag <a href = “ http: // www.site.com “> my site </a> as a tag <a> which has attribute href with value http://www.site.com and contents of a tag my site. In XML the question is four content types: element (the given element (tag) contains other elements (tags)), mixed (tags and the text), idle time (text) and empty (the tag contains only attributes and has no contents - <a href = "http://www.site.com"> </a>). If we deal with tags which have no contents, for optimization of a code it is better to close such tags directly in an opening tag (<book language = "rus"/> instead of <book language = "rus"> </book>).
As XML gives the developer the right independently to establish tags, there are simple rules of the task of their names:
• the name of a tag can contain letters, figures and other symbols, except for a blank;
• the name cannot begin with figure or a sign on a punctuation;
• the name cannot begin with word XML.
Also it is not necessary to use a sign - as in some cases it can call a mistake of the program which processes the XML-document.
XML allows to call tags not only latin letters, however I would not recommend to use this opportunity.
At the initial stage of use XML there can be a question, what for use the enclosed tags when there are attributes? For example, it was possible to write down our library so:
<? xml version = " 1.0" encoding = "windows-1251"?>
<library>
<book author = "A.Konan-Dojl" title = "Sherlok Holmes" year = "1996"/>
</library>
Yes, it reduces a code, however it is necessary to remember, what for we have undertaken it - to structure the information. Such recording does not give evident performance about structure of the data, does not give an opportunity to store{keep} more than one value in attribute (as against affiliated elements), has the worse expansibility.
However it is not necessary to go to extremes and with affiliated elements. It is necessary to think over structure and is proved to use both attributes, and affiliated elements.
So, we have created our first XML-document (a file with expansion .xml). How it to see{overlook}? Whether there are no in him mistakes? All is extremely simple: it is necessary IE 5 + and a double click on a XML-file. If the document is generated without mistakes, he will be displayed in a browser, you can see a code with illumination of syntax and an opportunity to develop{unwrap} / turn off affiliated elements. If in a code mistakes are admitted{allowed}, instead of the document the message on a mistake will be given.
Before speaking about manipulations with the XML-document, we shall finally be defined{determined} with his structure. A final kind of our file:
<? xml version = " 1.0" encoding = "windows-1251"?>
<library>
<book pages = "843">
<author> A.Konan-Dojl </author>
<title> Sherlok Holmes </title>
<year> 1996 </year>
</book>
<book pages = "457">
<author> L.Tolstoj </author>
<title> Anna Karenina </title>
<year> 2001 </year>
</book>
</library>
In examples we shall use an external XML-file, however it is necessary to say a pair of words about « ostrovki the data » (data islands). With their help it is enabled to store{keep} the XML-given directly in the HTML-document. For this purpose the XML-structure should be made in tags <xml id = "library"> </xml> where the attribute id is used for identification "ostrovka" and uses of his contents. "Ostrovki" it is possible podgruzhat` and from external files: <xml id = "library" src = "library.xml"> </xml>. The interesting decision at management of the XML-given the opportunity of merge to HTML-rules of display of the information sees. In the following example the opportunity of data presentation of our library is shown:
<html>
<body>
<xml id = "library" src = "library.xml"> </xml>
<table border = "1" datasrc = " * library ">
<tr>
<td> <span datafld = "author"> </span> </td>
<td> <span datafld = "title"> </span> </td>
<td> <span datafld = "year"> </span> </td>
</tr>
</table>
</body>
</html>
There are some methods of job with data XML with reference to the Internet. It CSS, XSL and JavaScript. We shall consider them under the order.
The opportunity of use CSS at job with XML-documents is obvious: cascade tables of styles allow to set only style for display of the data, but to not manipulate in any way contents of the document. Though this sheaf is unproductive, if there is a desire, it is necessary to write only in the second line of the XML-document <? xml-stylesheet type = "text/css" href = "main.css"?>. The structure of a .css-file is simple: names of styles should coincide with names of tags for which these styles are intended. Why it is unproductive? Yes it is simple XML it is seasoned with the similar technology - XSL, Extensible Stylesheet Language (« the expanded language of the table of styles »). Apparently from the name, it too the table of styles, though and with bo'l`shimi opportunities, rather than CSS. However and it not top of perfection. For use XSL the design is written to the second line of a code <? xml-stylesheet type = "text/xsl" href = "main.xsl"?>.
Now we shall talk about JavaScript, though and not ideal by way of speed of job, however, in my opinion, optimum means for processing the XML-given in browsers.
How it can be used XML and than he can help us? To be used - everywhere, help he can not only in structurization of a content, but also in economy of the traffic and optimization of the sizes of documents. We admit{allow}, we should create the menu for a site. The XML-document will look so:
<? xml version = " 1.0" encoding = "windows-1251"?>
<menu onmouseover = ” mouseOver1 () “>
<unit>
<title> About us </title>
<style> menu1 </style>
<link> about.html </link>
</unit>
<unit>
<title> Contacts </title>
<style> menu1 </style>
<link> contacts.html </link>
</unit>
<unit onmouseover = ” mouseOver2 () “>
<title> Partners </title>
<style> menu2 </style>
<link> partners.html </link>
</unit>
</menu>
I shall remind, that the examples specified in clause{article} are not ideal - probably, at you more favourable structure will turn out, however as an example we shall use this. So, that characterize tags <title> and <link>, I think, it is clear - I shall say only, that the tag <style> informs us, what style CSS is used for this item{point} of the menu (naturally, these styles should be created in a .css-file).
The root tag contains attribute onmouseover with value mouseOver1 (). This attribute speaks about what JavaScript-function will be caused at prompting a mousy on item{point} of the menu. However last item{point} of the menu has the attribute onmouseover with other value. It is made to have an opportunity to appoint, for example, different obrabotchiki events for different items{points} of the menu (CSS-style at this item{point} of the menu too differs from two previous). So, when we shall form the menu, we shall establish parameters of our items{points}. In first two items{points} we shall not find obrabotchika event (onmouseover) from what we shall draw a conclusion, that it is necessary to search for it at a level above since he, turns out, the general{common} for these elements. It is done{made} with the purpose of economy of a code and the traffic. But since the third item{point} at us differs (to us so has taken in head), obrabotchik he will have another - we of it obviously and have specified.
So, we shall try to load in our JavaScript-program the XML-document, to generate from him the menu and to give out to the user. For an insert of the menu I suggest to use a layer (<div>). I suggest to run eyes the following code and to discuss it postrochno:
<html>
<head>
<title> the Menu </title>
<meta http-equiv = "Content-Type" content = " text/html; charset=windows-1251 ">
<link href = "main.css" rel = "stylesheet" type = "text/css">
<script language = "JavaScript">
function makeMenu () {
var xmlMenu=new ActiveXObject (" Microsoft. XMLDOM ");
xmlMenu.async = "false";
xmlMenu.load ("menu.xml");
nodes=xmlMenu.documentElement.childNodes;
content = ' <table width = "150" border = "0"> ';
for (a=0; a <nodes.length; a ++) {
mouseOver=nodes.item (a) .getAttribute ("onmouseover");
if (mouseOver == null) {
mouseOver = xmlMenu.documentElement.getAttribute ("onmouseover");
}
for (b=0; b <nodes.item (a) .childNodes.length; b ++) {
if (nodes.item (a) .childNodes.item (b) .nodeName == 'title') {
title=nodes.item (a) .childNodes.item (b) .text;
}
else if (nodes.item (a) .childNodes.item (b) .nodeName == 'style') {
style=nodes.item (a) .childNodes.item (b) .text;
}
else if (nodes.item (a) .childNodes.item (b) .nodeName == 'link') {
link=nodes.item (a) .childNodes.item (b) .text;
}
}
content + = ' <tr> <td> <a href = " ' + link + ' " class = " ' + style + ' " onmouseover = " ' + mouseOver + ' "> ' + title + ' </a> </td> </tr> ';
}
content + = ' </table> ';
document.all.menu.innerHTML=content;
}
</script>
</head>
<body>
<a href = "*" onclick = " makeMenu () "> To generate the menu </a> <br> <br>
<div id = "menu"> </div>
</body>
</html>
So, we analyze function makeMenu (). The first line creates object into which we shall load the XML-document. The second line informs, that processing of the XML-document will begin only after his full loading. Further we speak, what file to load. The ambassador we appropriate{give} a variable nodes structure of affiliated elements of a root tag. That is, the variable nodes contains everything, that there is at us inside a tag <menu> </menu> in our XML-document. a parameter childNodes specifies that we wish to work with affiliated nodami a root element (xmlMenu is the object created by us in which the XML-file is loaded; documentElement - a root element; childNodes - affiliated elements current nody). After that a variable content in which we also shall write down the generated menu, we appropriate{give} initial value as a tag of opening of the table (subsequently we shall finish lines of the table, and in the end we shall put a closing tag </table>). Further we start a cycle which quantity{amount} of revolutions corresponds{meets} to quantity{amount} affiliated nod a root element (nodes.length). The ambassador we try to appropriate{give} a variable mouseOver value of attribute onmouseover current nody: nodes.item (a) defines{determines} the current element - in brackets the index (a serial number since zero) affiliated nody in current parental node is underlined, i.e. in node <menu> we touch affiliated nody <unit>, and expression nodes.item (0), nodes.item (1), nodes.item (2) specifies each of nod <unit> accordingly. If in current node we have not found value of attribute onmouseover, we mount on a level above, in parental nodu, and we look there (it has been stipulated by us at creation of a XML-file). For nody <unit> it is necessary for us to see{overlook} affiliated nody and to deduce{remove} therefrom the data necessary for us (the text of the link, the link and its{her} style). Therefore we go down on a level below and we inform, that we want to see{overlook} affiliated nody current nody (current noda - <item>): nodes.item (a) .childNodes. Passes a cycle (three times since at us of three parameters - three nody) then we have the necessary values in variables title, link and style, and then we we add in a variable content a line of the table with item{point} of the menu. The attribute nodeName current nody returns a name of a tag (title, style, link), and attribute text - contents nody (About us, menu1, about.html). In the end we close the table and we insert in a layer (<div>) value of a variable content. our menu also it is ready.
It is natural, if the menu will consist all of three elements, such circuit is unprofitable neither on speed, nor on volume of the loaded information, however if the number of items{points} will pass for a dozen, we be the winner. Besides reading, JavaScript allows to add and delete nody, to change attributes. For addition nody it is necessary to specify the following:
newnode=xmlMenu.createElement ("newnodename");
newtext=xmlMenu.createTextNode (" Contents new nody ");
newnode.appendChild (newtext);
xmlMenu.documentElement.appendChild (newnode);
Newnodename is a name of a tag for creation nody. For installation of attribute the following code is used:
nodes.item (a) .setAttribute ("newattribute") = ” Value of attribute “;
Newattribute - a name of created attribute.
At last let a pair of advice{councils}.
By development of applications for internal use (in a local area network) the size of a .xml-file of the big role does not play. But it is necessary to remember, what speed of Internet - connection is insufficiently high, that it was possible to forget about an opportunity of optimization of XML-documents (even if speed allows, what for unfairly to increase the traffic?) It is possible to have two versions of a .xml-file: one for development and orientation in structure, another (reduced) - for users. Essentially to save it is possible on names of tags. It is not necessary to abuse long values of attributes. Our .xml-file which has been laid out and the Internet here is how could look:
<? xml version = " 1.0" encoding = "windows-1251"?>
<m omo = ” mOver1 () “>
<u>
<t> About us </t>
<s> menu1 </s>
<l> about.html </l>
</u>
</m>
Such structure considerably saves the traffic; in addition, we have the full version - for orientation in structure.
Use reductions and in JavaScript. For example, it is enough often in a code the object document.all is used. It{he} without ceremony can be appropriated{given} a variable: DA=document.all, and then to use in designs such as DA.menu.innerHTML=content.
Try to write names nod and attributes and the bottom register, in order to prevent misunderstanding (nody registrozavisimy).
Successes!

|