The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

I don't get XML namespaces by SnappingTurtle2006-04-12 14:12:42
  It's not that hard (... or something ;-) ). by bwkaz 2006-04-12 15:24:52
Basically, the whole namespace standard added two things. First, you can prepend a tag or attribute name or value with a short identifier followed by a colon. (This short identifier must be associated with a URI before or in the tag, by means of an xmlns:shortid="theuri" attribute.)

Second, you can declare a "default" namespace for a tag (and its contents), using the xmlns="theuri" attribute. This makes the tag and all sub-elements (and also all attributes) be "members" of the namespace URI named "theuri".

So you can have something like:

<root xmlns="http://whatever/" xmlns:uf="http://www.userfriendly.org/">
<tag1 attr1="xxxx">yyyyy</tag1>
<uf:tag2 attr2="zzzz">aaaaa</uf:tag2>
</root>

The "root" and "tag1" elements, plus the "attr1" and "attr2" attributes, will be associated with the "http://whatever/" namespace. The "tag2" element will be associated with the "http://www.userfriendly.org/" namespace.

The XML DOM standard also introduced some new methods, e.g. getElementsByTagNameNS (and other *NS functions). This method takes a namespace URI (note: not the short identifier, the full URI) and a tag name, and returns all sub-elements whose tag name and namespace URIs match the criteria you give it.

The point is basically to separate my XML tags from yours from everyone else's, by making them belong to different namespaces. (Convention is to use a hostname that you control, and you can divvy up your namespace even further if you need to by putting more stuff after the / character.)

Also note that the "important" part is the URI, not the short identifier used as a prefix. (I believe you can have several xmlns:xxx attributes that all point at the same namespace, and if you can, then all of them will match a getElementsByTagNameNS call in a namespace-aware DOM.) The exception to this is that you can't use any short identifier prefixes that start with "xml"; these are all reserved for the standard. The standard is where the xml:lang attribute, the xmlns attribute, and xmlns:whatever attributes come from.

The only other confusing part is that an xmlns="" or xmlns:xxx="" attribute can exist on the same tag where it's used, and it takes effect on that entire tag (and all children); this means that you can see a prefix used in the document before it's defined. And elements are not always in the prefix that they appear to be in; you have to search them for attributes whose names start with xmlns to be sure.

(Oh yeah: *Also* note that DTDs are not namespace-aware, so you have to use a fixed namespace prefix if you're trying to follow a DTD. XHTML, for instance, needs to follow this convention. The "default" namespace for all tags is the XHTML namespace URI (which gets declared on the <html> tag), but if you want your XHTML to validate, you can't do stuff like use a prefix. Using an XML Schema definition or similar gets around this, but there is no official XML Schema for XHTML.)
[ Reply ]

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)