Tuesday, October 23, 2007

XML Schemas
(XML သဒၵါ (၀ါ) XML အဓိပၸါယ္ သတ္မွတ္မွဳ) - အပိုင္း ( ၁ )

Document Type Definition (DTD) ဟာ တခ်ိဳ ့ စြမ္းေဆာင္ခ်က္ေတြကို ရယူေပးဖို ့အတြက္ အကန္ ့အသက္ရိွေပမယ့္ XML ရဲ ့သဒၵါ (Schema) ကို ေဖာ္ၿပဖို ့ အတြက္ တကယ္ကို ေကာင္မြန္တဲ့ နည္းလမ္းတစ္ခုပါပဲ။ DTD ဟာ အေမြဆက္ခံၿခင္း (inheritance)၊ နယ္နိမိတ္သတ္မွတ္္ၿခင္း (scoping) တုိ ့ကို ခြင့္မၿပဳပါ ဘူး။ ဒါအၿပင္ ေဒတာ အမ်ိဳးအစားသတ္မွတ္ၿခင္း (datatyping) ကိုလည္း DTD နဲ ့လုပ္လို ့မရပါ။ DTD က ခြင့္မၿပဳေပမယ့္ ဒီ ကိစၥေတြဟာ XML ဒီဇိုင္းအတြက္ အသံုး၀င္တဲ့ အရာေတြပါ။ ဒါေၾကာင္း XML Schema မွာေတာ့ ဒါေတြကို လုပ္ေဆာင္ႏိုင္ေအာင္ ထည့္သြင္း တည္ေဆာက္္ခဲ့ပါတယ္။

XML Schemas ဆိုတာ သူတို ့ကိုယ္ႏိွက္က well-formed XML ေတြပါပဲ။ ဒါဟာ XML Schemas ရဲ ့ အေတာ္ကို ၾကီးမားတဲ့ အားသာခ်က္တစ္ခုပါပဲ။
ရိုးရွင္းတဲ့ XML Schema တစ္ခုကို ၾကည့္ရေအာင္။

<?xml version=”1.0” encoding=”utf-8” ?>
<xs:schema xmlns:xs = “http://www.w3.org/201/XMLSchema”>
<xs:element name=”Occupation” type=”xs:string”/>
</xs:schema>


ဒါဟာ အလြန္ရိုးရွင္းတဲ့ schema တစ္ခုပါပဲ။ ဒီ schema က Occupation ဆိုတဲ့ element တစ္ခုကို သတ္မွတ္ေပးပါတယ္။ အဲဒီ element ရဲ ့ ေဒတာ အမ်ိဳးအစားက string ပါ။


<?xml version=”1.0” encoding=”utf-8” ?>


ပိုၿပီး အေသးစိတ္ၾကည့္ရေအာင္။ ဒါက XML ေၾကညာတာပါပဲ။ ဒီဟာေလးဟာ ဘယ္ XML ဖိုင္ တိုင္းရဲ ့ အစမွာမဆို ပါေလ့ရိွပါတယ္။ သူက အသံုးၿပဳမယ့္ application ကို XML ဖိုင္ၿဖစ္ေၾကာင္း 1.0 XML Recommendation ေပၚမွာ အေၿခၿပဳထားေၾကာင္း၊ UTF-8 character encoding ကို သံုးထားေၾကာင္း စသည္တိုကို သိေစပါတယ္။ (XML schema ဟာ XML ပဲဆိုတာကို မေမ့ပါနဲ ့ဦး။) ဒီလို တူတဲ့ အတြက္ parser တစ္ခုတည္းကို XML document ကိုေရာ schema အတြက္ကိုေရာ အသံုးၿပဳႏိုင္ပါတယ္။

ထပ္ၾကည့္မယ္ဆိုရင္ ဒီလို ေတြ ့ပါလိမ့္မယ္။

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">


ဒီအေၾကာင္းေလးက xs (အနီေရာင္ႏွင့္ၿပထားသည္။) ဆိုတဲ့ Namespace ကို သတ္မွတ္ေပးပါတယ္။ ဒီမွာသံုးထားတဲ့ xs ဆိုတာ http://www.w3.org/2001/XMLSchema ရဲ ့ xs ပါတဲ့ ။

ဒါကလဲ အေရးၾကီးပါတယ္။ သူက ေနာက္ပိုင္းကၽြန္ေတာ္တို ေၿပာဆိုေဆြးရမယ့္ နယ္နိမိတ္သတ္မွတ္္ၿခင္း (scoping) နဲ ့ ဗားရွင္းသတ္မွတ္ၿခင္း (versioning) တိုကို ၿဖစ္ေစပါတယ္။

<xs:element name="programmer" type="xs:string"/>


ဒါကေတာ့ ကၽြန္ေတာ္တို ့ရဲ ့ Schema ကုိသံုးၿပီး element တစ္ခုကို ေၾကညာလိုက္တာပါပဲ။
ဒီေနရာက xs:element ဆိုတာမွာ xs က အေပၚက ကၽြန္ေတာ္တို ့ေၾကညာခဲ့တဲ Namespace ကိုယူသံုးလိုက္တာပါ။ သူက programmer ဆိုတဲ့ element တစ္ခုကို ေၾကညာေၾကာင္း၊ အဲဒီ element ရဲ ့ ေဒတာအမ်ိဳးအစား က string ၿဖစ္ေၾကာင္းတို ့ကို ေၿပာၿပပါတယ္။ ဒီေနရာမွာ type ကေလးက XML Schema မွာ အေရးၾကီးတဲ့ attribute ေလးပါပဲ။ သူက ေဒတာအမ်ိဳးအစားသတ္မွတ္ၿခင္း (datatyping) ကို လုပ္ေပးတယ္ေလ။

ဆက္ရန္။

ရည္ညြန္း။ ။ Using XML by David Gulbransen, et al.

အာေကလာ(akela)
Oct 23 , 2007 7:51 pm


Tuesday, August 21, 2007

Scope in JavaScript (part 4)
(JavaScript ????????? ????????? ????????) - ??????? ?

(??????? ? ? ? ????? ? ??? ????????????????????????)


.bind() ???????? ? ???


????????????? apply() ? call() ??? ? ?? ? ???????????????????????????? ?????????? .bind() ?????????? ????????? ????? ?????????? function ?? ? ??????(reference) ???? ???? (return) ????????????????????????????? ?????????????? ???????????? ??? ???????????????? ??????? ????????????? ?????????????????


<script type="text/javascript">
var _president = {
_answer: "I am the president"
};
var _secaretary = {
_answer: "I am the secaretary of president"
};

function aboutMe(greetingWord) {
return greetingWord + " " +this._answer;
}

Function.prototype.bind = function(obj) {
var method = this,
temp = function() {
return method.apply(obj, arguments);
};

return temp;
}
var whatTheysays;

whatTheysays = aboutMe.bind (_president, ["Hello My Country,"]); // "Hello My Country, I am the president"
alert(whatTheysays);

whatTheysays = aboutMe.bind (_secretary, ["Hello My Officer,"]); // "Hello My Officer, I am the secretary of president"
alert(whatTheysays);
</script>


???????????? ? bind() ??? Function object ?? ? prototype propety ??? ???????? ?????????????? ???? ???????????? ? ?????????? ? ?????? (method) ??????? ??? ?????????????????????? aboutMe.bind(_president) ????????? ?????? Javascript ? bind() ??????????? execution context ???????? ???????????? ???????? this ??? ???? aboutMe ??? ??????????????? ????? ??? prarmeter ??????? obj ??? _president ?????????????????? ??????????????? :D?

???? ????? ? ???????? method ??? ?????? ??????????? method ????? this ??? ???????? ???????? ???? this ? aboutMe() ??? ????? ???????? ?????????? function ??? ?????????????????? ???????????????????? method ??? scope chain ??????? ???? ???????????? ????????? this ??? ??????? ? ???????? ??????? function ???? ??? ????????? ????????? this ? ???? ????? ???????? (local context) ?????????????? aboutMe() ??????????????? ??????? function ?? ????????? ???????? ??????????????

??????????????????? ?? ????????? temp ? closure (????????????????????? ????????????????) ????? bind() ??? ??????? ? ???? (return ) ?????????? aboutMe ??? _president object ???????? ?????????????????? ?????? ????????? ????? execution context ??? ??????????? ? ??????? ???????????? ???????????????????

??? ???????????? ? event handler ????? ????????????? ??????????????????? :D ? ????? ???????? ????? event handler ??? ????? ????????????????


<script type="text/javascript">

var the_button;
function aboutMe(answer)
{
this._answer = answer;
this.tell = function() {
alert(this._answer);
}
}
Function.prototype.bind = function(obj) {
//alert(this + 'this in bind');
//alert(arguments[0].the_answer);
var method = this,
temp = function() {
return method.apply(obj, arguments);
};

return temp;
}

function addhandler()
{
var _president = new aboutMe(“I am the president.”),
var the_button = document.getElementById('thebutton');
the_button.onclick = _ president.tell();
//alert(this + ' this in addhandler');
the_button.onclick = _president.tell.bind(_president);
}
</script>

……
……

<BODY onload="javascript:addhandler();" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF" BACKGROUND="?">
<button ID="theButton">Click Me</button>
</BODY>
</HTML>


??? html ??? body ?? ? onload ??? addhandler() ??? ???? ?????????????????????????

????? ?????????????????????????.?

?????????????????? ????????


??????(akela)
Augest21, 2007 12:11 pm

Saturday, August 18, 2007

Scope in JavaScript (part 3)
(JavaScript မွာရိွေသာ နယ္နိမိတ္ သေဘာတရား) - အပိုင္း ၃

(အပိုင္း ၁ နဲ ့ ၂ ကို အရင္ဖတ္ၾကည့္ေစခ်င္ပါသည္။)

Context မ်ားကို .apply() နဲ ့ .call() မတ္သတ္(method) မ်ားသံုးၿပီး ကိုင္တြယ္ၿခင္း

တကယ္ေတာ့ ကၽြန္ေတာ္တို ့လို ခ်င္ခဲ့တာဟာ object ရဲ ့ မတ္သတ္(method) ကို events(အဗင့္) လိုမ်ိဳး၊ setTimeout() လို မ်ိဳး ကိုကိုင္တြယ္ဖို ့ သံုးတဲ့အခါက်ရင္ အဲဒီ method(မတ္သတ္)ကို သူရဲ ့ native context(မူလ ကြန္းတက္) ထဲမွာပဲ အလုပ္လုပ္(run) ေစခ်င္တာပါ။ လူသိနည္းတယ္လို ့ ဆိုႏိုင္တဲ့ မတ္သတ္ ၂ ခုရိွပါတယ္။ apply() နဲ ့ call() ပါ။ ဒီ မတ္သတ္ႏွစ္ခုကို သံုးၿခင္းအားၿဖင့္ ကၽြန္ေတာ္ တို ့ ဟာ this ရဲ ့ default value အေပၚကို လႊမ္းၿပီး(override) ေရးႏိုင္ပါတယ္။

<script type="text/javascript">
var _president = {
_answer: "I am the president"
};
var _secretary = {
_answer: "I am the secretary of president"
};
function aboutMe(greetingWord)
{

return greetingWord + " " +this._answer;
}
var whatTheysays;
whatTheysays = aboutMe.call(_president, "Hello My Country,");
alert(whatTheysays);
whatTheysays = aboutMe.call(_secretary, "Hello My Officer,");
alert(whatTheysays);
</script>


ဘယ္လိုလဲ :D ။ အလုပ္လုပ္တယ္ဟုတ္။ :P။

ဒီဥပမာေလးမွာဆိုရင္ ကၽြန္ေတာ္တို ့ president object ရယ္ secretary object ရယ္ဆိုၿပီး object ႏွစ္ခု ဖန္တီးလိုက္ပါတယ္။ သူတို တစ္ခုစီမွာ _answer ဆိုတဲ့ property ကို နာမည္ (name) အတူတူေပးလိုက္ပါတယ္။ ၿပီးေတာ့ aboutMe() ဆိုတဲ့ function တစ္ခုကိုဖန္တီး လုိက္ပါတယ္။အဲဒီ function က parameter တစ္ခုကိုလက္ခံပါတယ္။ greetingWord ပါ။ အဲဒီ function ထဲမွာ greetingWord ရယ္၊ သူဘယ္သူလဲဆိုတာကိုေၿပာတဲ့ စာအစဥ္အတန္း(string) (this._answre) ရယ္ကိုေပါင္းၿပီးၿပပါတယ္။ သတိထားမိလား.. this က call() ကပိုလိုက္တဲ့ object ေပၚမူတည္ၿပီးေၿပာင္းလဲသြားပါတယ္။ president ဆိုရင္ this.answer ဟာ _president ရဲ ့ _answer ၿဖစ္သြားၿပီး၊ _secretary ဆိုရင္ _secretary ရဲ ့ _answer ၿဖစ္သြားပါတယ္။ ဟုတ္။ ဒါဘာေၾကာင့္လဲ ဆိုေတာ့ call() မတ္သတ္(method) ေၾကာင့္ၿဖစ္ပါတယ္။ ပံုမွန္သာ aboutMe() ကို ေခၚတယ္ဆိုရင္ အားလံုးဟာ undified ပါပဲ။ ဘာလို ့ ဆိုေတာ့ global object ၿဖစ္တဲ့ window မွာ _answer ဆိုတဲ့ property မရိွပါဘူး။ ( ကၽြန္ေတာ္တို ့ က window object မွာ ကိုယ္ဖသာ ဒီ _answer ကို မဖန္တီးထားဘူးခဲ့ရင္ မရိွဘူးလုိ ့ ေၿပာတာပါ။) ဒီ call() မတ္သတ္ေလးက ကၽြန္ေတာ္တို ့လိုခ်င္ေနတာနဲ ့ အားလံုးကြက္တိပဲမဟုတ္လား။ :D ။

apply() က call() နဲ ့တူူတူပါပဲ။ အားလံုး တပံုစံပဲ အလုပ္လုပ္ပါတယ္။ တစ္ခုပဲကြဲတယ္။ဘာလဲဆိုေတာ့ apply() က arguments (parameters) ေတြကို array အေနနဲ ့လက္ခံပါတယ္။ ဒီ apply() မတ္သတ္က function တစ္ခုဟာ parameters ေတြကို ဘယ္ႏွစ္ခုလက္ ခံမယ္ဆိုတာကို ေသခ်ာမသိတဲ့ အခါမ်ိဳးမွာ အလြန္အသံုး၀င္ပါတယ္။

<script type="text/javascript">
.........
.........
whatTheysays = aboutMe.apply (_president, ["Hello My Country,"]);
alert(whatTheysays);

whatTheysays = aboutMe.apply (_secretary, ["Hello My Officer,"]);
alert(whatTheysays);
</script>


apply() နဲ ့ call() တိုဟာ သူဟာနဲ ့သူ အလြန္ အသံုး၀င္ပါတယ္။ ဒါေပမယ္ ဒီ ႏွစ္ခုဟာ တကယ္ေတာ့ ကၽြန္ေတာ္တို ့လိုခ်င္တာရဲ ့ လမ္းတ ၀တ္မွာပဲရိွေနပါေသးတယ္။ event handler အေနနဲ ့သံုးမယ္ဆိုရင္ေလ။ ဒီကုဒ္ေလးေတြကို ၾကည့္လိုက္ပါဦး။

function addhandler() 
{
var _president = new aboutMe(“I am the president.”),
var the_button = document.getElementById('thebutton');
the_button.onclick = _ president.tell.call(_president);
}


စမ္းၾကည့္ၿပီးၿပီ ဟုတ္။ ၿပသာနာက call() ေရာ apply() က ခ်က္ၿခင္း အလုပ္လုပ္(execute) လုပ္တာပါပဲ။ event handler အေနလုပ္တဲ့ အခါ event ကို ရိုတ္ထုတ္ (fire) လိုက္တဲ့ အခ်ိန္မွ မတ္သတ္(method) ကို အလုပ္လုပ္(Run) တာမဟုတ္ပဲ ခ်က္ၿခင္းအလုပ္လုပ္ တာက ၿပသာနာပါ။ ဒီအတြက္ ေနာက္ထပ္လူ သိနည္းတဲ့ function တစ္ခုရိွေနပါတယ္။ အလြန္အသံုး၀င္တဲ့ function ပါ။

ဆက္ရန္။

အာေကလာ(akela)
Augest 18, 2007 11:21 pm