Usage of IDs / IDREFs in SVG documents

508 views Asked by At

I want to combine several SVG fragments from different source documents into one result document. To avoid ID conflicts, I'll have to "namespace" the IDs of each source document by prefixing them with a unique string.

I can use XPath to find all ID declarations within the SVG documents (e.g. //@id). I could substitute the IDs either by directly manipulating the DOM or by doing some regular expression wizardry (or a combination of both). As a side note: I'm doing all this with PHP, and speed / performance doesn't matter that much, so regex replacements are just fine.

So far, I'm treating any attribute with a local name of id as an ID declaration, like e.g.

<clipPath id="a">...</clipPath>

and I succeed in replacing corresponding IDREFs of this syntax (i.e. when preceeded by a #):

<g clip-path="url(#a)">...</g>

Now these are my questions:

  1. Are there any other attributes in SVG other than those with a local name of id that are also ID declarations? Which ones?
  2. Are there any other syntaxes of IDREFs in SVG other than the above one (preceeding #)? Which ones?
  3. Are there any resources about IDs / IDREFs in SVG I should be aware of?
  4. Are there any potential conflicts other than duplicate IDs when merging SVG documents?

Thanks for any hints! :)

1

There are 1 answers

3
Erik Dahlström On BEST ANSWER
  1. In theory it's possible to say that any one particular attribute is of type ID with a DOCTYPE declaration. See the XML specification. However, only one ID attribute per element is allowed (validity constraint). And in any case, if you do add a custom ID attribute to SVG like that I'd argue that it's not really SVG content any longer.

    The other possiblity is xml:id, which is discouraged. See SVG Tiny 1.2.

  2. What do you mean? The syntax inside the url(...) is an IRI, so it can be e.g http://example.com/example.svg#foo. It is usually just a fragment, but not always.

  3. The linking chapter of the svg spec, the xml spec and the xml:id spec.

  4. Certain CSS selectors may only work in one particular svg document (it depends). If you change ids when merging two documents, then the stylesheet(s) may need to be updated too to take that into account.