Re: [Saxdotnet-devel] StartElement, when URI is not present
Brought to you by:
jeffrafter,
kwaclaw
From: Karl W. <ka...@wa...> - 2004-12-07 20:06:52
|
> Jeff Rafter wrote: > > > We have been discussing the various possibilities of enforcing a rule > > about what to do when a URI is not present. For example consider this: > > > > <foo></foo> > > > > What should the StartElement call consist of? > > > > QName : "foo" > > LocalName : "foo" > > Uri : null or string.Empty? > > > This really, really, really needs to be the empty string, unless null > works very differently in C# than in Java. It's just incredibly useful > to be able to assume the namespace URI (and other arguments to this > method) are never null. I see this again and again in my SAX work. API > quality wise, null should be avoided here. I can understand this, but you would usually check the Uri anyway, since you want to know if the name has a namespace. So you would get used to "if (uri == null)" fairly quickly. In general, using a special value to indicate presence or absence of the value is more fragile than using a separate indicator. Most general, this should be a separate boolean flag, as some types (integers, booleans) do not provide the extra level of indirection that allows the use of null. In this case however, we have a reference type (C# lingo). > > > I am not saying that I am against String.Empty, but from > > a programming point of view it is simply a weaker choice. > > > > - null indicates absence better than picking a special value. > > - what if an empty string ever got a special meaning for URIs? > > It already has a special meaning for URIs and namespace URIs. There's > approximately zero chance of this changing. Somewhat confusing is that RFC 2396 assigns some semantics to empty URIs: <quote> 4.2. Same-document References A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document, and a reference containing only a fragment identifier is a reference to the identified fragment of that document. Traversal of such a reference should not result in an additional retrieval action. However, if the URI reference occurs in a context that is always intended to result in a new request, as in the case of HTML's FORM element, then an empty URI reference represents the base URI of the current document and should be replaced by that URI when transformed into a request. </quote> > > XML-wise these two elements are equivalent: > > <data /> > <data xmlns="" /> > > Unless you're prepared to use null for both of them, which I think would > be harder for implementers, you need to pick the empty string here. xmlns="" is just syntax for null, as it means that there is no default namespace (where one might have been before), it doesn't mean that the default namespace has an empty string as URI (which is not a valid URI). > Unlike SQL, XML namespaces do not distinguish between the empty string > and no value. Namespaces in XML says, "The default namespace can be set > to the empty string. This has the same effect, within the scope of the > declaration, of there being no default namespace." > See above. Karl |