0

I want to centralise the xmlns:xlink declaration in my html documents to avoid repeating them when using svg <use> tags with xref attributes.

Instead of

<svg xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 125">
  <use class="ic-3" xlink:href="#ic" x="0" y="0" />
</svg>

I will only need to write

<svg viewBox="0 0 100 125">
  <use class="ic-3" xlink:href="#ic" x="0" y="0" />
</svg>

The problem is that I that I failed in finding a good source for which locations/nodes in the html tree are valid for placing these namespace declarations. I understand that svg nodes are suitable and this W3 Fools article also mentions the document root as valid for XML documents, which I assumed. But is any parent node a valid location for a namespace declaration? Say, can I just put it in a parent div or the body tag? Not asking for best practice, just the legality according to xml.

oligofren
  • 20,744
  • 16
  • 93
  • 180

1 Answers1

1

html does not support namespace declarations in markup so you can just delete them all.

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
  • that's not strictly correct is it? for instance the namespace declaration is required in xhtml, but optional (not unsupported) in html5. – oligofren Feb 08 '17 at 13:01
  • strike that, ref the WHATWG FAQ it is allowed, but does not do anything (hence not supported): https://wiki.whatwg.org/wiki/FAQ#What_is_the_namespace_declaration.3F also http://stackoverflow.com/a/34407652/200987 – oligofren Feb 08 '17 at 13:05
  • and the XLink namespace is included in HTML5, ref W3C: https://www.w3.org/TR/2011/WD-html5-20110405/namespaces.html, so you are indeed correct in saying it can be removed. – oligofren Feb 08 '17 at 13:07
  • 1
    I wrote html, not xhtml in my answer. Pretty much anything is allowed, that doesn't mean it will do anything. – Robert Longson Feb 08 '17 at 13:08
  • But does that mean that namespace prefixes are also implicitly void? Meaning instead of `xlink:href` I can simpli write `href`? – oligofren Feb 08 '17 at 13:12
  • Answering myself: `xlink:` is required as a prefix before `href`s in svg documents. For instance, embedding a `foo will not work. It's as if all the tags live in a different context in the SVG. – oligofren Feb 08 '17 at 13:19
  • @oligofren xlink:href used to be required is being phased out. Firefox and Chome support raw href, Safari does not. Edge may support href but I don't think IE does. – Robert Longson Feb 08 '17 at 13:52