Sunday, January 20, 2008

Glimmerized XML Namespaces

In a previous post, I mentioned a new Glimmer DSL for authoring XML documents.

XML support has been expanded further now to cover namespaces.

Take a look at this XML example:

<body>
  <bundle:label key="contact.firstName" >
    <ajax:text id="firstName" />
  </bundle:label>
  <bundle:label key="contact.lastName" >
    <ajax:text id="lastName" />
  </bundle:label>
</body>

It utilizes elements from two different namespaces: bundle and ajax.

Here is how to author that with the Glimmerized syntax:

body {
  bundle.label(:key => "contact.firstName") {
    ajax.text(:id => "firstName")
  }
  bundle.label(:key => "contact.lastName") {
    ajax.text(:id => "lastName")
  }
}

Intuitive and simple, isn't it?

Now, look at the following XML document:

<body>
  <domain:label key="contact.firstName" >
    <domain:text id="firstName" />
  </domain:label>
  <domain:label key="contact.lastName" >
    <domain:text id="lastName" />
  </domain:label>
</body>

Since namespaced elements come from the same namespace, things can be simplified with the Glimmerized syntax by utilizing the namespace wrapper:

body {
  namespace("domain") {
    label(:key => "contact.firstName") {
      text(:id => "firstName")
    }
    label(:key => "contact.lastName") {
      text(:id => "lastName")
    }
  }
}

So, namespaces are now officially supported by the Glimmerized XML syntax. There is more to it though, so check out the next post about it.

2 comments:

Ed Merks said...

Where's the xmlns:domain declaration? You seem to be calling the prefix a namespace. Why does the namespace wrapper not wrap both the label elements? Why is there a ":" in front of key and id? To denote that it has null namespace? I thought glimmer was supposed to be minimalistic. Is the > after the "=" for an attribute value really needed? Are the parentheses needed? How do you handle escapes for a " within a quoted string? What about mixed text or elements where the body is a simple type value? Inquiring minds have lots of questions. :-P

Will you give special status to the XML Schema instance namespace particularly for xsi:type since the value of that is a QName that represents the derived type of the instance that follows?

Annas "Andy" Maleh said...

Thanks for your questions and feedback Ed. The more the better. :)

Where's the xmlns:domain declaration?

This was addressed in my next blog post.

You seem to be calling the prefix a namespace.

My focus initially was on supporting the syntax that generates the prefix.

Why does the namespace wrapper not wrap both the label elements?

Actually, wrapping both label elements was exactly what I was demonstrating. Look closely at how both labels are wrapped by:
namespace("domain") {...}

Why is there a ":" in front of key and id? To denote that it has null namespace?

Because they are Ruby symbols. It is a common idiom in Ruby to use symbols as hash map keys.

I thought glimmer was supposed to be minimalistic. Is the > after the "=" for an attribute value really needed?

=> is used to map values to keys with Ruby hash maps. This is how named parameters are typically passed to methods in Ruby. Glimmer is written in Ruby as an internal DSL, so it relies on Ruby syntax. Fortunately, Ruby's syntax is so flexible that DSLs specified with it look almost like external DSLs. The good thing about that is you get both the power of an imperative Object-Oriented language and the expressiveness of a declarative DSL.

Are the parentheses needed?

Nope, they can be left off. It's just a matter of preference. Some people find that having them makes attributes stand out more clearly.

How do you handle escapes for a " within a quoted string?

Either escape " with a backslash (e.g. "John "\Beer Meister\" Molson") or use single-quotes around the string (e.g. 'This string contains a " character' )

What about mixed text or elements where the body is a simple type value?

Secret! :-P Actually, that will be the topic of my next blog post.

Inquiring minds have lots of questions. :-P

Thank you, these are the kinds of questions that are so great to have in open-source communities.

Will you give special status to the XML Schema instance namespace particularly for xsi:type since the value of that is a QName that represents the derived type of the instance that follows?

You bet I am provided I continue getting great input from you to assist in getting the implementation right. ;)