Does SVG support custom data attributes?

In HTML5, elements can have arbitrary metadata stored in XML attributes whose names begin with data- , such as <p data-myid="123456"> . Is this part of the SVG specification?

In practice, this method works great for SVG documents in many places. But I would like to know if this is part of the official SVG specification or not, because the format is young enough that there is still a lot of incompatibility between browsers, especially on mobile devices. Therefore, before I execute the code, I would like to know if I can expect future browsers to converge, supporting this.

I found this post from the working group mailing list saying that they β€œexpect [they] willβ€œ support ”it. Did it become official?

+60
svg web-standards
Mar 20 '13 at 18:57
source share
4 answers

While the other answers are technically correct, they omit the fact that SVG provides an alternative mechanism for data-* . SVG allows you to include any attribute and tag if it does not conflict with existing ones (in other words: you must use namespaces),

To use this (equivalent) mechanism:

  • use mydata:id instead of data-myid , for example: <p mydata:id="123456">
  • make sure you define the namespace in the SVG open tag, for example: <svg xmlns:mydata="http://www.myexample.com/whatever">
+97
Nov 28 '13 at 11:32
source share

The data-* attribute is part of HTML5. This is not a general XML attribute.

Current SVG Recommendation W3C SVG 1.1 (since 2011-08). It does not allow this attribute, since you can check the list of attributes .

the same applies to the SVG 2 Working Draft (from 2012-08). Update (2015) . It seems to be designed to support data-* attributes in SVG 2 (currently still a working draft).

+29
Mar 22 '13 at 21:42
source share

there is a more general mechanism.

svg supports desc elements that may contain arbitrary xml from other namespaces. Bind instances of these elements or child nodes from your own namespace using dependent identifiers or refid attributes.

this is an important part of the specification (5.4) .

+9
Mar 22 '13 at 11:46
source share



All Articles