Whitespace in schemaLocation Value and an Old Bug

Althoug schemaLocation attribute is not the key, I still like to have it my xml files for completeness/usefulness.  I always copy it from some golden files, since I know I will never type it correctly.  The format of schemaLocation attribute value is worth special attention:

The schemaLocation attribute value consists of one or more pairs of URI references, separated by white space. The first member of each pair is a namespace name, and the second member of the pair is a hint describing where to find an appropriate schema document for that namespace.

So the first part is the namespace value, and doesn't have to be a URL; the second part is the URI of the schema file.  For example,

<foo-jar xmlns="foo.foo.foo" xmlns:bar="bar.bar.bar"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="foo.foo.foo http://foo.foo.foo/foo-jar.xsd
                             bar.bar.bar http://bar.bar.bar/bar.xsd"> 

Since we usually use a URL as namespace value, so 99% of the time, the schemaLocation values appear to be pairs of URL's, and people tend to think so. 

Another misconception is that schemaLocation value was a single URL. I remember years ago there was a bug that basically says, in some xml files the values of schemaLocation erroneously contain whitespace where a "/" should have been used. 

I can't help wondering why schemaLocation value has to be so verbose and confusing, besides historical reasons?  I'm no xml expert, but at least two alternatives I can think of:

 1. Merge schemaLocation attribute with xmlns attribute.  Since schemaLocation is all about 1:1 mapping between namespace and URI, it doesn't make much sense for me to bind them with a separate attribute.  For example, the above example xml using merged attributes would be like:

  <foo-jar xmlns="foo.foo.foo http://foo.foo.foo/foo-jar.xsd"
          xmlns:bar="bar.bar.bar http://bar.bar.bar/bar.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

 This way we make xmlns value to be a pair, with an optional second part.

 2. Preserve schemaLocation attribute, but use the alias name of the namespaces as the first pair, use = between first and second part, and " " to delimit pairs.  Since the default namespace doesn't have an alias, we could use . as its alias.  We could also require that all namespace attributes should be parsed before schemaLocation attrbute.  For example:

  <foo-jar xmlns="foo.foo.foo"          
          xmlns:bar="bar.bar.bar"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation=".=http://foo.foo.foo/foo-jar.xsd bar=http://bar.bar.bar/bar.xsd">

This appears more readable and understandable, at least for me.  Xml files are not only for machines, but also for human readers.  Years ago when people were debating between xml and old-style EDI, xml proponents used to say, look, xml files are human readable.

Here are more resources on XML Namespace.

technorati tags:

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Cheng Fang

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today