Xml-script: how to use your custom classes from it

Published Thu, Oct 26 2006 6:57

Today I've built a simple custom Action so that I could see what is needed to use a custom element from xml-script. So I started with something like this:

Type.registerNamespace( "LA" );
LA.MyAction = function(){
 //code...doesn't really matter
}
//more prototype code which really doesn't matter :)
LA.MyAction.registerClass( "LA.MyAction", Sys.Preview.Action );

It was time to start writing xml-script...Though I really knew it didn't work, I started by trying something like this:

<script type="text/xml-script">
  <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
     <components>
          <button id="bt">
               <click>
                    <MyAction> 
                     ...
                  </MyAction>
              </click>
        </button>
     </components>
   </page>
</script>

Yes, it didn't work because the parser was looking for the MyAction class in the default namespaces added by the AJAX client bits. I though that there should be a way to associate a xml namespace with the namespace used in Javascript where my class was defined. After taking a peek at the previewscript.js, I didn't found any method which registered a namespace. So, I decided to look at the _processPrefixMapping method of the Sys.Preview.MarkupParser to see what was going on during xml namepsace resolution. As i've already said, when you use the default namespaces, you automatically get the namespace list defined by the AJAX bits; If you're using a different namespace, then you must make sure that the value you use for the xml namespace is the same you've used for the code namespace (and please pay attention to case-sensitivity because it's important here). So, that means that in order for my code to be correctly interpreted, you need to do this:

<script type="text/xml-script">
  <page xmlns:script="http://schemas.microsoft.com/xml-script/2005" xmlns:test="LA">
     <components>
          <button id="bt">
               <click>
                    <test:MyAction> 
                     ...
                  </test:MyAction>
              </click>
        </button>
     </components>
   </page>
</script>

And now the parser is happy and able to instantiate my custom action. There are some remarks about this strategy:

  • I understand why they do that, though I'd really prefer to be able to associate a specific xml namespace to a code namespace. In other words, I'd prefer to call a register method so that I could say: "map http://mydomain.com" to  namespace X and namespace Y"
  • If you really want to go this way (ie, use the same name for a code namespace and a xml namespace), then one of things must be done: make element names case sensitive too (currently, you only need to write the name of the class, without paying any attention to capital letters) or remove that restriction from domain registration

So, it looks like today xml-script is still sucking a little bit :) And what do you think?

Filed under: ,

Comments

# Bertrand Le Roy said on Thursday, October 26, 2006 2:39 PM

Believe me, we all wish it could be case-sensitive (it's actually a lot easier to implement). Unfortunately, Safari doesn't let us.

One thing to note is that xml specs require the namespace to be a uri. We don't enforce that but we recommend that you use the javascript protocol: xmlns:test="BLOCKED SCRIPTLA".

# Luis Abreu said on Thursday, October 26, 2006 5:25 PM

hello again.

didn't know that, but then i'm not a mac user and the last time i looked safari only run in mac (and it was a long time ago :) )

so, regarding the javascript protocol, that means that i'd have to register the namespace with: xmlns:test="BLOCKED SCRIPTLA". is this correct or did i got it completly wrong? if i got it correct,  i'm curious on the need to do this since it looks like the method strips the javascript prefix when it finds it...

btw, and since you've mentioned it, why don't you guys add a method that would let us associated a xmln namespace to a code namespace instead of trying to get that automatically?

thanks.

# Luis Abreu said on Sunday, October 29, 2006 3:11 PM

i've just noticed that there's a mistake in my previous comment. i meant: xmlns:test="BLOCKED SCRIPTBLOCKED SPRIPTLA"

# Dmitry Bogatykh said on Tuesday, November 14, 2006 4:29 PM

Thank you very much for this post!

Because of lack of AJAX documentation, it was really helpful!

Leave a Comment

(required) 
(required) 
(optional)
(required)