Skip to content

'invalid namespace for attributes' error when adding custom namespaces to svg element #1935

Closed

Description

@gopeter

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)

Thats how I tried it:

// register custom namespaces
d3.ns.prefix.ex = 'http://example.com/';

// create chart
var chart = d3.select('#chart')
            .append('svg:svg')
            .attr('width', width)
            .attr('version', '1.1')
            .attr('xmlns', 'http://www.w3.org/2000/svg')
            .attr('xmlns:ex', 'http://example.com/');

The result should looks like this:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:ex="http://example.com">

Activity

  1. jasondavies commented on Jul 8, 2014

    @jasondavies
    Contributor

    The XMLNS namespace is a special case, e.g. for setAttributeNS(namespace, name, value):

    1. 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.)

  2. gopeter commented on Jul 8, 2014

    @gopeter
    Author

    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 :)

  3. added this to the 3.x milestone on Oct 10, 2014
  4. added
    bug
    and removed on Feb 7, 2015
  5. modified the milestones: 3.5.x, 3.x on Feb 7, 2015
  6. mbostock commented on Feb 7, 2015

    @mbostock
    Member

    So, instead of:

    element.setAttributeNS("http://www.w3.org/2000/xmlns/", "ex", "http://example.com/");

    We should be saying:

    element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ex", "http://example.com/");

    Is that it?

  7. jasondavies commented on Feb 8, 2015

    @jasondavies
    Contributor

    Correct!

  8. hugolpz commented on Mar 2, 2015

    @hugolpz

    Is that implemented ? (would be nice when out)

  9. micahstubbs commented on Sep 24, 2015

    @micahstubbs
    Contributor

    "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.

  10. modified the milestones: 3.5.7, 3.5.x on Oct 22, 2015
  11. mbostock commented on Oct 26, 2015

    @mbostock
    Member

    Seems like XML is a special namespace, too?

    NAMESPACE_ERR: Raised if the qualifiedName is malformed, if the qualifiedName has a prefix and the namespaceURI is null, if the qualifiedName has a prefix that is "xml" and the namespaceURI is different from "http://www.w3.org/XML/1998/namespace", or if the qualifiedName is "xmlns" and the namespaceURI is different from "http://www.w3.org/2000/xmlns/".

    But I can’t think of any examples where we use an "xml:…" attribute.

  12. mbostock commented on Oct 26, 2015

    @mbostock
    Member

    Hmm. Seems like only XMLNS should be treated specially, according to empirical testing. So maybe we’ll start there.

  13. added 2 commits that reference this issue on Oct 26, 2015
  14. mbostock commented on Oct 26, 2015

    @mbostock
    Member

    Fixed in #2591 for 3.5.7.

  15. micahstubbs commented on Oct 27, 2015

    @micahstubbs
    Contributor

    👍

  16. added a commit that references this issue on Jan 26, 2020
  17. added a commit that references this issue on Jan 30, 2020
  18. added a commit that references this issue on May 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions