|
|
(2 intermediate revisions by one other user not shown) |
Line 1: |
Line 1: |
| == XML namespaces ==
| | See the [http://developer.mozilla.org/en/docs/XML Mozilla Developer Center] for info on XML namespaces. |
| | | [[Category:Redirects]] |
| XML namespaces provide a way to distinguish duplicate element and attribute names. Duplicates can occur when an XML document contains elements and attributes from two or more different XML schemas (or DTDs).
| |
| | |
| | |
| An XML namespace is identified by an unique name (called a URI, not a URL, even though it can look like a URL). An URI is any string, although most people choose a URL-based URI because URLs are an easy way to '''hope''' for uniqueness. Although there's nothing preventing someone else from using the namespace http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul, it's fairly unlikely anyone would choose that accidentally. Even if they did accidentally choose it, they may not define the same elements as XUL anyway (e.g., <textbox/>) in their schema/DTD.
| |
| | |
| | |
| Any element type or attribute name in an XML namespace can be uniquely identified by its XML namespace and its "local name".
| |
| | |
| | |
| For example, <xul:textbox/> uses a namespace named "xul" and a local name "textbox.". This distinguishes it from, for example, <ihoss:textbox/> which might occur in the same document. The xul and ihoss namespaces must be defined at the top of the XML document in which they are used, like so:
| |
| | |
| | |
| <ihoss:some-element xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:ihoss="ihoss-is-cool">
| |
| <xul:textbox id="foo" value="bar"/>
| |
| <ihoss:textbox favorite-food="pancakes"/>
| |
| </ihoss:some-element>
| |
| | |
| | |
| Notice I've mixed two <textboxes/> in the same document. The only way to distinguish that they have different meanings is with namespaces.
| |
| | |
| | |
| There's only one other thing to know: "default namespace". Every XML document can have one "default namespace", and this is used with XUL documents all the time. In XUL documents, you'll usually see this:
| |
| | |
| | |
| <window
| |
| id="foo"
| |
| '''xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"'''>
| |
| ...
| |
| ...
| |
| </window>
| |
| | |
| and in XHTML documents, you'll see this:
| |
| | |
| <html '''xmlns="http://www.w3.org/1999/xhtml"'''>
| |
| ...
| |
| ...
| |
| </html>
| |
| | |
| There is a very subtle difference here than before. Before I wrote xmlns''':xul'''="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" but here the ''':xul''' piece is omitted. This signifies to the XML parser that http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul is the default namespace for the document, and that any element or attribute without a namespace (i.e., no prefixed colon) belongs to the default namespace. That's why we can write the shorthand <textbox/> instead of <xul:textbox/> all the time (although the latter is just as correct when not using http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul as the default namespace). So, in other words, a default namespace permits a kind of short-hand to be used throughout the document.
| |