You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
'invalid namespace for attributes' error when adding custom namespaces to svg element #1935
When trying to add a custom namespace as attribute to the svg element, I get the following error: Uncaught NamespaceError: Failed to execute 'setAttributeNS' on 'Element': 'http://www.w3.org/2000/xmlns/' is an invalid namespace for attributes.
I need to add custom namespaces to the svg element to create a valid SVG document that can be used to extract semantic data (e.g. with the W3C RDFa validator)
If namespace is the XMLNS namespace and neither name nor prefix is "xmlns", throw a "NamespaceError" exception.
The problem here is that we always call setAttributeNS(namespace, localName, value), dropping the prefix. In the example above we call setAttributeNS("http://www.w3.org/2000/xmlns/", "ex", "http://example.com/"), triggering the exception.
One option could be to drop the xmlns prefix from d3.ns.qualify. However, we currently drop unsupported prefixes entirely, so the call to setAttribute wouldn’t have the desired effect.
(By the way, for your specific example, you can set the xmlns attributes using the DOM API directly by using selection.node() to retrieve the first Element in the selection. It would be nice to find a solution in D3 if possible though.)
Yes, at the moment I add the namespace with the DOM API. A solution to get this to work with D3 would be great. It would be way cleaner than adding workarounds :)
"By the way, for your specific example, you can set the xmlns attributes using the DOM API directly by using selection.node() to retrieve the first Element in the selection."
I'll share the code I used for workaround that @jasondavies describes:
var svg = d3.select("body").append("svg")
svg.node().setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.w3.org/2000/svg");
svg.node().setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
In case it's helpful for future search engine visitors, here's the stackoverflow question that led me here, along with a bl.ock that shows an example of these attributes hard-coded in an <svg> tag.
Activity
jasondavies commentedon Jul 8, 2014
The XMLNS namespace is a special case, e.g. for setAttributeNS(namespace, name, value):
The problem here is that we always call
setAttributeNS(namespace, localName, value), dropping the prefix. In the example above we callsetAttributeNS("http://www.w3.org/2000/xmlns/", "ex", "http://example.com/"), triggering the exception.One option could be to drop the xmlns prefix from d3.ns.qualify. However, we currently drop unsupported prefixes entirely, so the call to setAttribute wouldn’t have the desired effect.
(By the way, for your specific example, you can set the xmlns attributes using the DOM API directly by using selection.node() to retrieve the first Element in the selection. It would be nice to find a solution in D3 if possible though.)
gopeter commentedon Jul 8, 2014
Yes, at the moment I add the namespace with the DOM API. A solution to get this to work with D3 would be great. It would be way cleaner than adding workarounds :)
mbostock commentedon Feb 7, 2015
So, instead of:
We should be saying:
Is that it?
jasondavies commentedon Feb 8, 2015
Correct!
hugolpz commentedon Mar 2, 2015
Is that implemented ? (would be nice when out)
micahstubbs commentedon Sep 24, 2015
"By the way, for your specific example, you can set the xmlns attributes using the DOM API directly by using selection.node() to retrieve the first Element in the selection."
I'll share the code I used for workaround that @jasondavies describes:
In case it's helpful for future search engine visitors, here's the stackoverflow question that led me here, along with a bl.ock that shows an example of these attributes hard-coded in an
<svg>tag.mbostock commentedon Oct 26, 2015
Seems like XML is a special namespace, too?
But I can’t think of any examples where we use an "xml:…" attribute.
mbostock commentedon Oct 26, 2015
Hmm. Seems like only XMLNS should be treated specially, according to empirical testing. So maybe we’ll start there.
The XMLNS namespace is special.
Fix #1935 - the XMLNS namespace is special.
mbostock commentedon Oct 26, 2015
Fixed in #2591 for 3.5.7.
micahstubbs commentedon Oct 27, 2015
👍
Switch back to setAttribute
Implement SVG support (#13)
Implement SVG support (pkamenarsky#13)