Feb 26, 2008

xsl:sort

Normally, this element used with for-each and apply-templates

            

            

Feb 25, 2008

current()

The function returns a node set that has the current node as its only member. For an outermost expression, an expression not occurring within another expression, the current node is always the same as the context node. Thus,


is the same as

However, within square brackets, the current node is usually different from the context node. For example,


processes all elements that have a parent element and a name attribute with value equal to the value of the current node's ref attribute. This is different from


which means the same as
            
and so would process all elements that have a parent element and that have a name attribute and a ref attribute with the same value.

Dynamically adding control which can do async postback in UpdatePanel

Precondition, The control needs to have Id, if not the ajax clientside script can not handle it. Then see the following code.

Dim btn As LinkButton = CType(child, LinkButton)
AddHandler btn.Click, AddressOf ProcessFilter
''me.UpdatePanel1.Triggers.Add(as
'Dim trigger As UpdatePanelControlTrigger = New UpdatePanelControlTrigger
Dim trigger As AsyncPostBackTrigger = New AsyncPostBackTrigger()
trigger.ControlID = btn.UniqueID
trigger.EventName = "Click"
Me.UpdatePanel1.Triggers.Add(trigger)

Feb 24, 2008

defer javascript

The HTML 4 standard defines an attribute of the

generate script tag

            document.write("");  this does not work
            //but you can use document.write("<\/script>");
            

object's member

Sometime, a object's member comes from it's own definition, if it is not defined, will ask check its prototype's definition, same thing apply to the prototype, if prototype object has that definition, that is returned, if not prototype object'check its prototype object, the process continue until prototype object is undefined.

            function User()
            {
            this.sayHi = function(){ alert("hi");};

            }

            User.prototype.sayYes = function()
            {
            alert("Yes");
            }

            User.sayHello = function()
            {
            alert("hello");
            }




            var u = new User();
            alert(User.prototype.sayYes);
            u.sayYes(); //this function comes from User.prototype

            alert(User.prototype.sayHi); //undefined
            u.sayHi();  //this function comes from the object u, in the constructor, sayHi is added to object

            User.sayHello(); //hello
            u.sayHello(); //throw exception
            

Please note the User itself is object created by Function, because alert(User.constructor == Function); is true. So its definition User.sayHello is not defined its prototype, u objects can not use this method.

apply() and call()

v3 defines two methods that are defined for all functions, call( ) and apply( ). These methods allow you to invoke a function as if it were a method of some other object. The first argument to both call( ) and apply( ) is the object on which the function is to be invoked; this argument becomes the value of the this keyword within the body of the function. Any remaining arguments to call( ) are the values that are passed to the function that is invoked. For example, to pass two numbers to the function f( ) and invoke it as if it were a method of the object o, you could use code like this:

f.call(o, 1, 2);
o.m = f;
o.m(1,2);
delete o.m;

The apply( ) method is like the call( ) method, except that the arguments to be passed to the function are specified as an array:

f.apply(o, [1,2]);
var biggest = Math.max.apply(null, array_of_numbers);
//Math.max(array_of_number) is not supported!

Primitive Types and Reference Types

Numbers, boolean values, and the null and undefined types are primitive. Objects, arrays, and functions are reference types.

            var a = 3.14;  // Declare and initialize a variable

            var b = a;     // Copy the variable's value to a new variable

            a = 4;         // Modify the value of the original variable

            alert(b)       // Displays 3.14; the copy has not changed


            var a = [1,2,3];  // Initialize a variable to refer to an array

            var b = a;        // Copy that reference into a new variable

            a[0] = 99;        // Modify the array using the original reference

            alert(b);         // Display the changed array [99,2,3] using the new reference
            

Reference types do not have a fixed size; indeed, some of them can become quite large. As we've already discussed, variables do not directly hold reference values. The value is stored at some other location, and the variables merely hold a reference to that location. Now we need to focus briefly on the actual storage of the value. Since strings, objects, and arrays do not have a fixed size, storage for them must be allocated dynamically, when the size is known. Every time a JavaScript program creates a string, array, or object, the interpreter must allocate memory to store that entity. Whenever memory is dynamically allocated like this, it must eventually be freed up for reuse, or the JavaScript interpreter will use up all the available memory on the system and crash. In languages like C and C++, memory must be freed manually. It is the programmer's responsibility to keep track of all the objects that are created and to destroy them (freeing their memory) when they are no longer needed. This can be an onerous task and is often the source of bugs. Instead of requiring manual deallocation, JavaScript relies on a technique called garbage collection. The JavaScript interpreter is able to detect when an object will never again be used by the program. When it determines that an object is unreachable (i.e., there is no longer any way to refer to it using the variables in the program), it knows that the object is no longer needed and its memory can be reclaimed. Consider the following lines of code, for example:


            var s = "hello";            // Allocate memory for a string

            var u = s.toUpperCase(  );  // Create a new string

            s = u;                      // Overwrite reference to original string

            

After this code runs, the original string "hello" is no longer reachable -- there are no references to it in any variables in the program. The system detects this fact and frees up its storage space for reuse. Garbage collection is automatic and is invisible to the programmer. You can create all the garbage objects you want, and the system will clean up after you! You need to know only enough about garbage collection to trust that it works;

null vs undefined

In javascript, they are different concept.
var d ={};
d.x = null;
alert(d.x); //null
alert(d.y); //undefined

Feb 23, 2008

Javascript notes

Object Creation

When a function used with new, it became a constructor

            function Dog()
            {
            this.name = "Fred";
            this.Bark = function()
            {
            alert("my name is " + this.name);
            };
            }

            var d = new Dog();
            d.Bark();
            

A constructor is a function that construct and return a object, to achieve this result it is not necessary to use new with a function. A little bit of change the function can do that

            function Dog()
            {
            this.name = "Fred";
            this.Bark = function()
            {
            alert("my name is " + this.name);
            };
            //you don't need this, if you don't want to call "var d = Dog()",
            //you can only use "var d = new Dog()"
            //if you want to support both, you need this
            return this;
            }

            var d = Dog()
            // var d = new Dog(); is still supported

            d.Bark();
            
Essentially, no matter whether you use "new" or not the function do the following
            function Dog()
            {
            var d = new Object();
            //or
            // var d = {}
            d.name = "Fred";
            d.Bark = function()
            {
            alert("my name is " + this.name);
            };
            return d;
            }
            var d = Dog()
            // var d = new Dog(); is still supported

            d.Bark();
            

Constructor/Prototype

In javascript, everything is object. event the constructor itself is an object. The instance created the constructor, is also an object. So that is relationship between two. Constructor function is an object, it has a member object with name called "prototype". This member object is a reference to an object, let's call it prototype object. The prototype object has definition of reusable behavior, it has member called "constructor", which reference to constructor. The instance object created by constructor is a different object, it has optional definition of new behavior. And it also has secret link to the prototype object that I just mentioned.

            alert(Dog.constructor); //function Function () { [native code] }
            alert(Dog.prototype); //[object Object]
            alert(Dog.prototype.constructor); //function Dog() {..}
            

prototype inheritance

What is inheritance? It is word to describe an effect, or you can say it is an effect, that is code reuse. In traditional language, this is achieved by classical inheritance. Using new class, we can create new object and its behavior. But in JavaScript, there is no class, every thing is object. But no matter whether it is traditional language, or javascript, object does the job, so creating object is common goal. So how, it turns out that we create new object based on old object, the old object is the prototype of a new object.

            function Dog()
            {
            this.name = "fred";
            return this;
            }
            function BigDog(){}
            BigDog.prototype = new Dog();
            var bd = new BigDog();
            alert(bd.name);

            
The above code to implement the reuse is not elegant, because there is not built-in construct to facilitate this process. It is quite ironic that it is prototype style language, but does not have build-in prototype construct. Douglas Crockford create the following function to support that.
            function object(o)
            {
            function F(){}
            F.prototype = o;
            return new F();
            }

            var bd = object(new Dog());
            alert(bd.name);
            

prototype inheritance style. using prototype keyword vs using closure

There is not classical inheritance in javascript, Douglas Crockford call "using prototype" is like Pseudoclassical Inheritance. Below is style of using prototype keyword

            function Gizmo(id) {
            this.id = id;
            }
            Gizmo.prototype.toString = function () {
            return "gizmo " + this.id;
            };

            function Hoozit(id) {
            this.id = id;
            }
            Hoozit.prototype = new Gizmo();
            Hoozit.prototype.test = function (id) {
            return this.id === id;
            };

            

Below is using the closure

            function symbol(s, p) {
            return {
            id: s,
            lbp: p,
            value: s
            };
            }

            function delim(s) {
            return symbol(s, 0);
            }

            function stmt(s, f) {
            var x = delim(s);
            x.identifier = true;
            x.reserved = true;
            x.fud = f;
            return x;
            }

            function blockstmt(s, f) {
            var x = stmt(s, f);
            x.block = true;
            return x;
            }

            

Generally, closure is more elegant, but is not very straight forward for javascript beginner. In the example above, all the inheritance is achieved by decorating an object in different constructor, since the constructor is in a stack. When top constructor is called, a object is returned from the bottom constructor, and object is modified in each constructor on the way to the top constructor.

Sigleton or static method

The idea is that we want to create an object with method, but we want there is only one instance of the object

            function Dog()
            {
            return {
            name:"fred",
            bark:function()
            {
            alert(this.name);
            }
            }
            }

            var d = new Dog();
            d.bark();

            var dog = function()
            {
            return {
            name:"fred",
            bark:function()
            {
            alert(this.name);
            }
            }
            }();

            dog.bark();

            

object, Function, function

Again in javascript, everything is object. var b = Boolean(), b is an object. Boolean is an object. var o = new Object(), o is an object, Object is an object. I think there are two kind of Object. One has property of prototype, and one that has not, but it can access propotype by obj.constructor.prototype. All object has constructor. Built-in objects such Array, Boolean, Date, Math, Number, Object, RegExp, String they are have prototype. They all have prototype member. Because they have prototype, they can use "new" keyword to create another kind of object, which does have prototype. functions also have property type, they can be used with new as constructor to create other normal objects which have no prototype member. To create function we can use "function" keyword.

            Function.prototype.method = function(name, func) {
            this.prototype[name] = func;
            return this;
            };

            function User(){}
            //this is actually var User = function User() {}
            //this is actually var User = new Function() {}

            alert(typeof Function); //function
            alert(typeof User); //function
            alert(typeof Function.constructor); //function
            alert(typeof User.constructor); //function
            alert(Function.constructor); //function Function(){ [native code] }
            alert(User.constructor); //function Function(){ [native code] }

            alert(User.method == Function.prototype.method); //true
            alert(User.prototype); //[object Object]
            alert(Function.prototype); //in IE it shows function prototype{[native code]}, in firefox it shows function(){}

            User.method("sayhi", function() { alert("hi"); });
            var u = new User();
            u.sayhi(); //alert("hi")

            

The interesting thing is that User.method == Function.prototype.method . Actually, then User is defined, a object let'say temp = new Function() is created, temp has a secret link to the Function.prototype, and the temp object is return. When you call User.method is actually calling temp.method. In our case, the method add member to temp object. When you call var u = new User();, the temp is return. So in essence The temp object has not definition of "method", so it will escalate the call to the Function.prototype object. Please know User.prototype is not equal to Function or Function.prototype. It is always [object Object]. Please note that in Function.prototype.method definition, there is statement, "returns this". This is not necessary, but is necessary for a cascade-style of programming, which is like obj.method1.method2.method3 instead of "obj.method1(); obj.method2(); obj.method3(); Douglas Crockford said, "When I write a method that doesn't need to return a value, I usually have it return this."

Feb 18, 2008

import external style sheet

            
            

            
            

            

asp.net xslt output

XslTransform xslt = new System.Xml.Xsl.XslTransform();             xslt.Load(xslFile);  
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
StringWriter writer = new StringWriter();
XmlTextWriter xmlwriter = new XmlTextWriter(writer);
XsltArgumentList list = new XsltArgumentList();
list.AddParam("Age", "", "108");
xslt.Transform(doc, list, xmlwriter);
xmlwriter.Close();
writer.Close();
Response.Write(writer.ToString());
writer.Dispose();
Response.Flush();
Response.Close();

xslt comparison

Below is an example of comparision.
            
            
            He is male
            
            
            She is a female
            
            
            
If the xpath expression return more than one value, that is a sequence. For example Program/@flag = 'favorite' , Program/@flag has more than one value, XSLT processor goes through each of the flag attributes in turn, if it finds any flag attribute that has the value 'favorite', then the comparison returns true. This expression means if the sequence has any value equals to 'favorite'. If you mean, none of sequence has value of 'favorite', you can not use Program/@flag != 'favorite', (which means any of sequence is not equal to 'favorite'), you should use not(Program/@flag = 'favorite')

ouput as an attribute

We can ouput normal text as . If we want to ouput as attribute, we should use {xpath-expression}. For example
            
,

Template priority

XSLT processor will only ever process one template when you apply templates to a node, so it has to choose one among all matched templates based on the matched priority.