Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Why do we use URLs for namespaces?
Why do we use URLs for namespaces? [message #415263] Sat, 08 December 2007 00:16 Go to next message
Jason Henriksen is currently offline Jason HenriksenFriend
Messages: 231
Registered: July 2009
Senior Member
So I've come across a little trick that I really like. I'm going to
share it here to see if it is helpful or perhaps gets a hole poked in
the idea.

I'm using EMF to build an enterprise model. That means about 3000
objects and at least one hundred java packages which translates into an
equivalent number of name spaces.

Here's a tiny bit of sample XML I've just generated showing the
interaction of two of these namespaces. I have a class in a root
package [com.vsp.model.plan] using a class from a sub-package.
[com.vsp.model.plan.soap]

<http://vsp.com/model/plan/soap:PlanCreateResponse
xmlns:http://vsp.com/model/plan/soap="http://vsp.com/model/plan/soap"
>
<responsePlan benefitProviderCode="TEST"/>
</http://vsp.com/model/plan/soap:PlanCreateResponse>

For me, that doesn't look very pretty. I've got to do mental mapping
from http space to java package space and I need to know which to use
when. There's also a lot of slashes, dots and colons for me to mess up.

Then I was struck by the obvious: The namespace can be ANY string!

I did some search and replace work and mapped all of my namespaces so
that "http://vsp.com/model/plan/soap" becomes "com.vsp.model.plan.soap".
I then used tooling so that any time I need an xsd prefix for
disambiguating tags, I also use the package name. All this XSD is going
to come in an SDK anyway so it's not like that URL was ever going to be
valid.

After that my serialized XML looks like this:

<com.vsp.plan.model.soap:PlanCreateResponse
xmlns:com.vsp.plan.model.soap="com.vsp.plan.model.soap"
>
<responsePlan benefitProviderCode="TEST"/>
</com.vsp.plan.model.soap:PlanCreateResponse>


I know it's a small difference, but to me that looks tonnes better.
Plus, my team mates don't have to worry:
"Do I put the url, the package or some randomly determined prefix?"
becomes:
"I just use the package name and it ALWAYS WORKS"


Ahhh... So much nicer!

I then built a tool so that recurses through my XSD and updates the
namespace for the XSD depending on what directory the xsd file is in.
That way I'm sure my XSD is in a directory that matches my namespace
that matches the output java package. Because everything matches all
the time, I have much less confusion about what gets used where.

That XSD update code is pretty trivial but if anyone wants it just let
me know and I'll post it somewhere.

Jason Henriksen
Re: Why do we use URLs for namespaces? [message #415266 is a reply to message #415263] Sat, 08 December 2007 08:17 Go to previous messageGo to next message
Alexandros Karypidis is currently offline Alexandros KarypidisFriend
Messages: 32
Registered: July 2009
Member
Hello Jason,

I don't use XML serialization a lot and generally create models directly
in ecore, so I am not very well versed in this sort of syntax. That
said, I believe that EMF can do this ready for you with a proper
genmodel. The ecore from which you generate code / xsd serializaiton has
2 settings: NsPrefix and NsURL. My understanding is that EMF maps the
namespace URL to the prefix of your choice (usually a short string) to
produce less verbose XML.

jason henriksen wrote:

>
> So I've come across a little trick that I really like. I'm going to
> share it here to see if it is helpful or perhaps gets a hole poked in
> the idea.
>
> I'm using EMF to build an enterprise model. That means about 3000
> objects and at least one hundred java packages which translates into an
> equivalent number of name spaces.
>
> Here's a tiny bit of sample XML I've just generated showing the
> interaction of two of these namespaces. I have a class in a root
> package [com.vsp.model.plan] using a class from a sub-package.
> [com.vsp.model.plan.soap]
>
> <http://vsp.com/model/plan/soap:PlanCreateResponse
> xmlns:http://vsp.com/model/plan/soap="http://vsp.com/model/plan/soap"
> >
> <responsePlan benefitProviderCode="TEST"/>
> </http://vsp.com/model/plan/soap:PlanCreateResponse>
>
> For me, that doesn't look very pretty. I've got to do mental mapping
> from http space to java package space and I need to know which to use
> when. There's also a lot of slashes, dots and colons for me to mess up.
>
> Then I was struck by the obvious: The namespace can be ANY string!
>
> I did some search and replace work and mapped all of my namespaces so
> that "http://vsp.com/model/plan/soap" becomes "com.vsp.model.plan.soap".
> I then used tooling so that any time I need an xsd prefix for
> disambiguating tags, I also use the package name. All this XSD is going
> to come in an SDK anyway so it's not like that URL was ever going to be
> valid.
>
> After that my serialized XML looks like this:
>
> <com.vsp.plan.model.soap:PlanCreateResponse
> xmlns:com.vsp.plan.model.soap="com.vsp.plan.model.soap"
> >
> <responsePlan benefitProviderCode="TEST"/>
> </com.vsp.plan.model.soap:PlanCreateResponse>
>
>
> I know it's a small difference, but to me that looks tonnes better.
> Plus, my team mates don't have to worry:
> "Do I put the url, the package or some randomly determined prefix?"
> becomes:
> "I just use the package name and it ALWAYS WORKS"
>
>
> Ahhh... So much nicer!
>
> I then built a tool so that recurses through my XSD and updates the
> namespace for the XSD depending on what directory the xsd file is in.
> That way I'm sure my XSD is in a directory that matches my namespace
> that matches the output java package. Because everything matches all
> the time, I have much less confusion about what gets used where.
>
> That XSD update code is pretty trivial but if anyone wants it just let
> me know and I'll post it somewhere.
>
> Jason Henriksen
>
>
Re: Why do we use URLs for namespaces? [message #415267 is a reply to message #415266] Sat, 08 December 2007 08:18 Go to previous messageGo to next message
Alexandros Karypidis is currently offline Alexandros KarypidisFriend
Messages: 32
Registered: July 2009
Member
Alexandros Karypidis wrote:

Just noticed, these settings are in the ecore itself, not the generator
model.

> Hello Jason,
>
> I don't use XML serialization a lot and generally create models directly
> in ecore, so I am not very well versed in this sort of syntax. That
> said, I believe that EMF can do this ready for you with a proper
> genmodel. The ecore from which you generate code / xsd serializaiton has
> 2 settings: NsPrefix and NsURL. My understanding is that EMF maps the
> namespace URL to the prefix of your choice (usually a short string) to
> produce less verbose XML.
>
> jason henriksen wrote:
>
>>
>> So I've come across a little trick that I really like. I'm going to
>> share it here to see if it is helpful or perhaps gets a hole poked in
>> the idea.
>>
>> I'm using EMF to build an enterprise model. That means about 3000
>> objects and at least one hundred java packages which translates into
>> an equivalent number of name spaces.
>>
>> Here's a tiny bit of sample XML I've just generated showing the
>> interaction of two of these namespaces. I have a class in a root
>> package [com.vsp.model.plan] using a class from a sub-package.
>> [com.vsp.model.plan.soap]
>>
>> <http://vsp.com/model/plan/soap:PlanCreateResponse
>> xmlns:http://vsp.com/model/plan/soap="http://vsp.com/model/plan/soap"
>> >
>> <responsePlan benefitProviderCode="TEST"/>
>> </http://vsp.com/model/plan/soap:PlanCreateResponse>
>>
>> For me, that doesn't look very pretty. I've got to do mental mapping
>> from http space to java package space and I need to know which to use
>> when. There's also a lot of slashes, dots and colons for me to mess up.
>>
>> Then I was struck by the obvious: The namespace can be ANY string!
>>
>> I did some search and replace work and mapped all of my namespaces so
>> that "http://vsp.com/model/plan/soap" becomes
>> "com.vsp.model.plan.soap". I then used tooling so that any time I
>> need an xsd prefix for disambiguating tags, I also use the package
>> name. All this XSD is going to come in an SDK anyway so it's not like
>> that URL was ever going to be valid.
>>
>> After that my serialized XML looks like this:
>>
>> <com.vsp.plan.model.soap:PlanCreateResponse
>> xmlns:com.vsp.plan.model.soap="com.vsp.plan.model.soap"
>> >
>> <responsePlan benefitProviderCode="TEST"/>
>> </com.vsp.plan.model.soap:PlanCreateResponse>
>>
>>
>> I know it's a small difference, but to me that looks tonnes better.
>> Plus, my team mates don't have to worry:
>> "Do I put the url, the package or some randomly determined prefix?"
>> becomes:
>> "I just use the package name and it ALWAYS WORKS"
>>
>>
>> Ahhh... So much nicer!
>>
>> I then built a tool so that recurses through my XSD and updates the
>> namespace for the XSD depending on what directory the xsd file is in.
>> That way I'm sure my XSD is in a directory that matches my namespace
>> that matches the output java package. Because everything matches all
>> the time, I have much less confusion about what gets used where.
>>
>> That XSD update code is pretty trivial but if anyone wants it just let
>> me know and I'll post it somewhere.
>>
>> Jason Henriksen
>>
>>
Re: Why do we use URLs for namespaces? [message #415268 is a reply to message #415263] Sat, 08 December 2007 12:02 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Jason,

Comments below.

jason henriksen wrote:
>
> So I've come across a little trick that I really like. I'm going to
> share it here to see if it is helpful or perhaps gets a hole poked in
> the idea.
>
> I'm using EMF to build an enterprise model. That means about 3000
> objects and at least one hundred java packages which translates into
> an equivalent number of name spaces.
>
> Here's a tiny bit of sample XML I've just generated showing the
> interaction of two of these namespaces. I have a class in a root
> package [com.vsp.model.plan] using a class from a sub-package.
> [com.vsp.model.plan.soap]
>
> <http://vsp.com/model/plan/soap:PlanCreateResponse
> xmlns:http://vsp.com/model/plan/soap="http://vsp.com/model/plan/soap"
> >
> <responsePlan benefitProviderCode="TEST"/>
> </http://vsp.com/model/plan/soap:PlanCreateResponse>
>
> For me, that doesn't look very pretty. I've got to do mental mapping
> from http space to java package space and I need to know which to use
> when. There's also a lot of slashes, dots and colons for me to mess up.
>
> Then I was struck by the obvious: The namespace can be ANY string!
Pretty much. Though there are characters best avoided, e.g., things
like " ", "#", "?", the former because it needs to be encoded and the
latter two because they have special meaning the in the syntax (to
specify a fragment or query), which I suppose is okay in a namespace but
doesn't seem like a good idea.
>
> I did some search and replace work and mapped all of my namespaces so
> that "http://vsp.com/model/plan/soap" becomes
> "com.vsp.model.plan.soap". I then used tooling so that any time I
> need an xsd prefix for disambiguating tags, I also use the package
> name. All this XSD is going to come in an SDK anyway so it's not like
> that URL was ever going to be valid.
Since qualified Java package names also need to be globally unique,
they're a reasonable for of URI. However your example above is using a
URI directly as the qualifier of an element when in fact you have to
define xmlns:nsPrefix="nsURI". So to me to have
xmlns:com.vsp.model.plan.soap="http://vsp.com/model/plan/soap" and then
use com.vsp.model.plan.soap:PlanCreateResponse seems "good enough".
After all, as Alexandros mentions EPackage.getNsPrefix and
EPackage.getNSURI contain exactly the the data for this mapping...
>
> After that my serialized XML looks like this:
>
> <com.vsp.plan.model.soap:PlanCreateResponse
> xmlns:com.vsp.plan.model.soap="com.vsp.plan.model.soap"
> >
> <responsePlan benefitProviderCode="TEST"/>
> </com.vsp.plan.model.soap:PlanCreateResponse>
>
>
> I know it's a small difference, but to me that looks tonnes better.
> Plus, my team mates don't have to worry:
> "Do I put the url, the package or some randomly determined prefix?"
> becomes:
> "I just use the package name and it ALWAYS WORKS"
>
When I first started working on EMF (before it was called that)
namespace aware parsers had not been around all that long and performed
poorly so the XMI parsing code did its own prefix to package package and
it did that by requiring the prefix to be globally unique without
actually caring what namespace was mapped to the prefix. It was a bit
of a of a faux pas that needed to be fixed but a few remnants of that
still there today (assuming xmi and xsi prefixes have special meaning,
though the xsi thing was recently fixed). At that point, folks noticed
that long prefixes made the serialization quite verbose so folks tended
to start using shorter prefix to produce smaller XML. As far as I'm
concern, variety is the spice of life so whatever turns them on tickles
me pink.
>
> Ahhh... So much nicer!
>
> I then built a tool so that recurses through my XSD and updates the
> namespace for the XSD depending on what directory the xsd file is in.
> That way I'm sure my XSD is in a directory that matches my namespace
> that matches the output java package. Because everything matches all
> the time, I have much less confusion about what gets used where.
There's ecore:package and ecore:nsPrefix that can be used to control the
fully qualified package name for the Java as well as the nsPrefix for
the EPackage...
>
> That XSD update code is pretty trivial but if anyone wants it just let
> me know and I'll post it somewhere.
What you propose sounds like a pretty good style of prefix naming in the
face of 100 prefixes/namespaces.
>
> Jason Henriksen
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Bidirectional reference and CopyCommand understanding...
Next Topic:Installing the examples
Goto Forum:
  


Current Time: Fri Apr 19 23:47:37 GMT 2024

Powered by FUDForum. Page generated in 0.03119 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top