SVG — a default namespace, a borrowed prefix, and mixing¶
Scalable Vector Graphics is the vocabulary your browser draws. It is the gentlest possible introduction to real-world namespaces because you can see the result, and because it shows three patterns in one small file:
- a default namespace (the SVG elements carry no prefix),
- a borrowed namespace (
xlink:from another spec, for linking), and - mixing — an SVG fragment dropped straight into an HTML page.
A small logo¶
rect,linearGradient,textand the rest carry no prefix — they live in the default namespace declared byxmlns="http://www.w3.org/2000/svg"on the root. Every descendant inherits it.xlink:hrefis not an SVG attribute.xlinkis a separate W3C spec (XLink) for expressing links; SVG 1.1 borrows it.url(#g)above is an internal reference written CSS-style;xlink:href="#g"is the XML way.<a>here is the SVG anchor element (in the SVG namespace), not the HTML one — same local name, different namespace. This is exactly why namespaces exist.
Two ways to reference, one document
Notice the file uses both fill="url(#g)" (a functional IRI reference, the
CSS heritage) and xlink:href="#g" (the XML heritage). SVG is a meeting point
of the web's two cultures, and its attribute set shows the seams.
The schema side¶
SVG was historically defined by a DTD, and later RELAX NG — not primarily
XSD — but XSD renderings exist and are useful for seeing the shape. Here is a
representative fragment for <rect>, rendered with unxml --xsd:
schema http://www.w3.org/2000/svg (elementFormDefault=qualified)
xmlns = http://www.w3.org/2000/svg
ns xlink = http://www.w3.org/1999/xlink
import http://www.w3.org/1999/xlink from xlink.xsd # (1)!
type LengthType
restriction xs:string
pattern [-+]?[0-9]*\.?[0-9]+(px|pt|em|%)?
attributeGroup PresentationAttrs # (2)!
@fill : xs:string
@stroke : xs:string
element rect
@x : LengthType
@y : LengthType
@width : LengthType (required)
@height : LengthType (required)
attributeGroup ref PresentationAttrs
@ref xlink:href # (3)!
- The schema
imports the XLink namespace from a separate schema document — the XSD-level counterpart of thexmlns:xlinkdeclaration in the instance. - Presentation properties (
fill,stroke, …) are bundled into an attribute group and reused across dozens of element types — the same DRY mechanism you saw in Modular schemas. @ref xlink:hrefpulls in an attribute declared in another namespace. The schema can only do this because it imported that namespace above.
If LengthType's pattern looks familiar, it is the
regular-expression facet from the XSD
chapter, doing real work: constraining width="120" and width="50%" while
rejecting width="wide".
Mixing: SVG inside HTML¶
The payoff. Modern HTML lets you write SVG inline, with no xmlns at all:
<p>Our logo: <svg viewBox="0 0 16 16" width="16" height="16">
<circle cx="8" cy="8" r="7" fill="indigo"/>
</svg> looks like this.</p>
In an HTML document, the parser knows that <svg> switches the children into
the SVG namespace automatically — the namespace is implied by the element name.
But the moment you treat the same file as XML (XHTML, or any XML toolchain),
that magic is gone and you must declare the namespace explicitly, because an XML
parser has no special knowledge of "svg". This split — implied in the HTML
parser, explicit in XML — is the single most common source of "it renders in the
browser but my XPath finds nothing" confusion.
Querying namespaced SVG with XPath¶
An unprefixed name means no namespace
//circle matches nothing in a namespaced SVG document — circle is in
the SVG namespace, and an unprefixed name in XPath 1.0 means "no namespace".
You must bind a prefix (say s) to http://www.w3.org/2000/svg and ask for
//s:circle. This is the same rule the
XPath chapter introduced, biting in a
real document.
Things to note¶
- A default namespace lets a whole subtree go prefix-free — convenient for authoring, but every name is still in that namespace.
- A vocabulary can borrow another (
xlink) instead of reinventing linking. - The same local name (
<a>) means different things in different namespaces — the original motivation for the whole mechanism. - "It works in the browser" relies on the HTML parser's namespace shortcuts; XML tools need the declarations spelled out.
Next: SOAP and WSDL, where namespaces stop being convenient and become a contract between machines.