0

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 Answers1

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

Community
  • 1
  • 1
Erik Dahlström
  • 59,452
  • 12
  • 120
  • 139
  • 2. You will also need to handle `xlink:href` references, such as found in `` and other elements. For example, see: http://www.w3.org/TR/SVG/struct.html#ImageElement – Paul LeBeau Aug 20 '13 at 14:44
  • Thanks @Erik for your answer, which definitely covers some interesting points, but partly seems a little too much of a general XML lecture with respect to my specific question. Therefore, please allow me to reword: Are there any other ID-IDREF-reference patterns within one and the same SVG document other than the quoted one ([id/xml:id attribute](http://www.w3.org/TR/SVGTiny12/struct.html#IDAttribute) + IRI reference (with the latter only being relevant if it references the same document), and other than CSS selectors) that could be of relevance when merging several SVG documents? Thanks! – Joschi Kuphal Aug 31 '13 at 22:06
  • You're right, @BigBadaboom, `xlink:href` attributes carry IRI references as well, so these are relevant as well as long as they reference the very same document. Thanks for the hint! – Joschi Kuphal Aug 31 '13 at 22:11