Setting a Default Namespace with the xmlns Attribute
You often know that all the content of a particular
element will come from a particular XML application. For instance,
inside an SVG svg
element, you’re
only likely to find other SVG elements. You can indicate that an
unprefixed element and all its unprefixed descendant
elements belong to a particular namespace by attaching an xmlns
attribute with no prefix to the top
element. For example:
<svg xmlns="http://www.w3.org/2000/svg" width="12cm" height="10cm"> <ellipse rx="110" ry="130" /> <rect x="4cm" y="1cm" width="3cm" height="6cm" /> </svg>
Here, although no elements have any prefixes, the svg
, ellipse
, and rect
elements are in the http://www.w3.org/2000/svg
namespace.
The attributes are a different story. Default namespaces only
apply to elements, not to attributes. Thus, in the previous example,
the width
, height
, rx
, ry
,
x
, and y
attributes are not in any
namespace.
You can change the default namespace within a particular
element by adding an xmlns
attribute to the element. Example
4-4 is an XML document that initially sets the default
namespace to http://www.w3.org/1999/xhtml
for all the
XHTML elements. This namespace declaration applies within most of
the document. However, the svg
element has an xmlns
attribute
that resets the default namespace to http://www.w3.org/2000/svg
for itself and its content. The XLink information is included in attributes, however, so these must be placed in the XLink namespace using explicit prefixes. ...
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.