question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SVG attribute namespaces

See original GitHub issue

I’m looking into using D3 to create <svg>s that I can convert to PDF, and I’ve been spending a lot of quality time learning about XML namespaces:

https://developer.mozilla.org/en/SVG/Namespaces_Crash_Course

I’d like to write a patch for D3 to prefix attribute namespaces explicitly, so that they show up in the DOM and exporting back to XML is easier. Quoting from that article,

For setting attributes that have a namespace, it is recommended (but not required) that you also include their prefix in the second argument so that the DOM can later be more easily converted back to XML (if for instance you want to send it back to the server). For example:

elt.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'otherdoc.svg');

Right now to get an explicit prefix one has to tell D3 twice:

div.append("svg").attr("xmlns", "http://www.w3.org/2000/svg")
  .attr("xmlns:xmlns:xlink", "http://www.w3.org/1999/xlink");
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></svg>

Is there anything that would go terribly wrong if we started using explicit prefixes on the attributes? If not, I’ll submit a pull request implementing this (and I’ll patch #272 as well).

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
timiyaycommented, Jun 19, 2016

I am currently in the midst of this issue, and I find the use of .attr() to be unclear.

My use case is using an external <image> tag as a <pattern>, which is use the fill a <path>.

I try using attr() to set the image.href like so:

imageSelection.attr({ 'xlink:href': myUrl })

However, the resulting markup only has <image href="myUrl"> - the xlink namespace is dropped.

While this doesn’t affect the SVG in the browser, it breaks down when trying to do other stuff like:

  • pre-render SVGs on the server, and convert them to JPG / PNG
  • embed the SVGs inside PDFs

D3 being the brilliant library that it is, I have no doubt the functionality I need is in there somewhere. I’m not finding it intuitive, and would love some help from folks around this issue. I could use any working solutions to help improve the docs or something.

UPDATE

The answer is to use a prepending semi-colon, to make it :xlink:href, like so:

imageSelection.attr({ ':xlink:href': myUrl })

Found this on the D3 Google Group - https://groups.google.com/forum/#!topic/d3-js/4pOdrFpgjTo

0reactions
Filcommented, Jul 27, 2017

My use case was making a svg compatible with Inkscape, I needed inkscape:groupmode="layer" inkscape:label="layer name". https://github.com/visionscarto/fonds-de-cartes/commit/a47ef7a5855f9fb4504cb7ad76194f15abfe6dde

Read more comments on GitHub >

github_iconTop Results From Across the Web

Namespaces crash course - SVG: Scalable Vector Graphics
The namespace declaration is provided by the xmlns parameter. This parameter says that the <svg> element and its child elements belong to ...
Read more >
Document Structure — SVG 2
The SVG 2 namespace is http://www.w3.org/2000/svg , which is the same as for earlier versions of SVG. The following are possible ways to...
Read more >
XML Namespaces in CSS — Using SVG with CSS3 and HTML5
You can use namespaced attribute selectors to select custom attributes added by your graphics software, such as layer names. But, remember: The CSS ......
Read more >
Namespace-correct SVG attributes? · Issue #8 · observablehq/htl
Similar to #7, there are a few attributes that have implicit namespaces in SVG. Attribute name, Prefix, Local name, Namespace. xlink:actuate ...
Read more >
What is in the xmlns:serif namespace, and why is it needed (or ...
SVG allows inclusion of elements from foreign namespaces anywhere within the SVG content. In general, the SVG user agent must include the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found