Scripts
Scripts can be used in documents XSL just as and in HTML. Besides the scripts contained inside the XSL-document and started by a browser during processing the document can dynamically create HTML-documents, taking the information necessary for it directly from elements of the XSL-document.
For a spelling of scripts XSL uses special skriptovyj language - ECMAScript. However in msxsl for these purposes it is possible to apply Microsoft JavaScript - language which has united in itself some elements standard JavaScript and ECMAScript.
Calculation of expressions
The most simple example of use of scripts in XSL the document is calculation of values of parameters of described elements. For this purpose it is necessary to put simply an equal-sign as the first symbol of parameter that will force the XSL-processor to calculate value of expression (syntax of this expression should meet the requirements JavaScript). For example, after analysis of this rule:
<rule>
<target-element type = "header">
<hr width = " = 100-20 + '%' ">
<children/>
<hr width = " 80 % ">
</rule>
In the target document there are following instructions:
<hr width=80 %>
...
<hr width=80 %>
Very much often in rules XSL it is necessary to use attributes of an element described in them. For this purpose we should use a method getAttribute (), described in objective model XML (we consider{examine} the objective model of XML-documents offered{suggested} Microsoft, the list of these functions is resulted at the end of the unit). Since the index on object corresponding to him is accessible to each element XSL, the script can address to internal functions and properties of these elements by means of which necessary actions are carried out.
In the following fragment of the XML-document the element <article> in which the attribute src is used for the task of the address of the file containing the text of clause{article} is defined{determined}.
<articles>
<article src = " http: // server/pages/article.html ">
Bugs report </article>
</articles>
To use this attribute in the target HTML-document, it is necessary to define{determine} the following rule:
<rule>
<target-element type = "article">
<a href = ' = getAttribute ("src") '>
<children/>
</a>
</rule>
After processing this fragment in the target document the element will be placed:
<a href = " http: // server/pages/article.html "
> Bugs report </a>
Performance of instructions
In another way premises{rooms} in the target HTML-document of the information which are growing out of performance of any operations JavaScript - scripts is ispol`zovnaie instructions <eval>;:
<rule>
<element type = "articles">
<target-element type = "article">
</element>
<tr> <td> <eval> childNumber (this) </eval> </td> <td>
<children/>
</td> <tr>
</rule>
The method childNumber in this case returns current number{room} of an affiliated element.
Definition of functions and global variables
Similarly to a tag <SCRIPT> in HTML, the element <define-script> contains functions and definitions of global variables. Usually in the XSL-document one element <define-script>, located right at the beginning is defined{determined}.
<xsl>
<define-script>
<! [CDATA [
var fontSize=12;
function getColor (elem) {
return elem.children.item ("color", 0) .text;
// Returns contents of an affiliated element <color>
}]]>
</define-script>
<rule>
<target-element type = "flower">
<div background-color = " = getColor (this) ";
font-size = " = fontSize ">
<children/>
</div>
</rule>
</xsl>
If to apply these rules to such fragment to the XML-document:
<xml>
<flower>
rose
<color> red </color>
</flower>
, That on output{exit} HTML the document will contain the following elements:
<div background-color = "red"; font-size = "12">
It is necessary to note, that use of global variables in some cases can result in the serious mistakes called by attempts of simultaneous access to them. Therefore it is recommended to use such variables only as constants.
Use Java Script for HTML
Creating a pattern of the HTML-document, you can specify in him practically any elements HTML, including blocks <SCRIPT> inside which it is possible to set any designs Java Script, using for this purpose area CDATA:
<xsl>
<rule>
<root/>
<HTML>
<HEAD>
<SCRIPT LANGUAGE = "JSCRIPT"> <! [CDATA [
var ie4 = ((navigator.appName == " Microsoft Internet
Explorer ") ** (parseInt (navigator.appVersion)> = 4));
function msover () {
if (ie4) {
event.srcElement.style.color = "red";
event.srcElement.style.cursor = "hand";
}
}
function msout () {
if (ie4) {
event.srcElement.style.color = "black";
event.srcElement.style.cursor = "auto";
}
}]]> </SCRIPT>
</HEAD>
<BODY>
<children/>
</BODY>
</HTML>
</rule>
<rule>
<target-element type = "chapter"/>
<DIV id = ' = tagName + formatNumber
(childNumber (this), "1") '
background-color = "marron"
onmouseover = ' = " msover (" + tagName +
formatNumber (childNumber (this), "1") + ")" '
onmouseout = ' = " msout (" + tagName +
formatNumber (childNumber (this), "1") + ")" '
<children/>
</DIV>
</rule>
</xsl>
If to use these rules for the following XML-document:
<contents>
<chapter> Part1 </chapter>
<chapter> Part2 </chapter>
<chapter> Part3 </chapter>
</contents>
That in result we shall receive such HTML-file:
<HTML>
<HEAD>
<SCRIPT LANGUAGE = "JSCRIPT">
var ie4 = ((navigator.appName == " Microsoft Internet
Explorer ") ** (parseInt (navigator.appVersion)> = 4));
function msover () {
if (ie4) {
event.srcElement.style.color = "red";
event.srcElement.style.cursor = "hand";
}
}
function msout () {
if (ie4) {
event.srcElement.style.color = "black";
event.srcElement.style.cursor = "auto";
}
}
</SCRIPT>
</HEAD>
<BODY>
<DIV id = "chapter1" onmouseover = " msover ("chapter1") " '
onmouseout = " msout ("chapter1") " '
Part 1
</DIV>
<DIV id = "chapter2" onmouseover = " msover ("chapter2") " '
onmouseout = " msout ("chapter2") " '
Part 2
</DIV>
<DIV id = "chapter3" onmouseover = " msover ("chapter3") " '
onmouseout = " msout ("chapter3") " '
Part 3
</DIV>
</BODY>
</HTML>
Ajax for Java developers: the Part 2. Cposoby serializations of the data for Ajax
If you develop Web-applications, using JavaScript and XML (Ajax) the main task is a clearing the server of the data for job by the machine of the client with them. In second clause{article} Phillip Makkarti will tell to you about five ways of serialization of the data.
In first clause{article} of this series I have entered construction of blocks Ajax:
* How to use XMLHttpRequest object, to send asynchronous search to the server from Web-page
* How to process and respond on search with help Java servleta, returning the XML-document to the client
* How to use the document of the answer to search of the client for updating a kind of your page
Now I shall continue to discuss bases of development in Ajax, but I shall concentrate that most of all excites many Web-developers in language Java: creation of the data for the client.
The majority of Java-developers apply a pattern Model - Vid-Kontroler (MVC) to the Web-applications. In the usual Web-application the component of a kind (view component) will consist from JSP or, probably, other technology of performances, such as pattern Velocity. Components of this performance update the interface of the user, dynamically make whole new page HTML, replacing previous. However, in the Web-applications written in language Java and with used technology Ajax, the client part of code JavaScript is maximum responsible{crucial} for updating of a kind of page which is seen by the user. It is based on the data coming according to XMLHttpRequest. From the point of view of the server the kind becomes data presentation which he sends according to searches of the client.
In this clause{article} we have concentrated attention to techniques, which you can use to create performances of the Java-objects based on the data. I shall show set of methods which you can use for returning JavaBeans in XML-documents, and you learn{find out} merits and demerits of everyone. You also will see, why XML is not always an output{exit}: transfer of the usual text can be more favourable. At last, I shall acquaint you with recording objects in JSON. JSON allows given to be passed in the form of the circuit serializuemykh data JavaScript which is extremely convenient for job in a code of the client.
About an example
I shall apply one indicative application and some precedents to illustrate to you features of technology and technics{technical equipment} about which it will be told here. Figure 1 shows extremely simple model of the data which illustrates cases of use of an example. The model represents the bill of the buyer in shop working online. The buyer has set of the previous lines of purchases, each of which will consist of several products.
In spite of the fact that XMLHttpRequest does not impose any restrictions on a format used for sending of the data of search, in most cases most preferably it appears to send simply usual parameters of the form, therefore in a course of discussion we shall concentrate attention to search of the server. The search can be also any text format, but, in connection with the coordination of names, at XMLHttpRequest there are built - in opportunities on job with data XML of search. It does{makes} XML by a good choice for Ajax searches by default as to start with it conveniently enough.

|