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:
id
that are also ID declarations? Which ones?#
)? Which ones?Thanks for any hints! :)
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.
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.
The linking chapter of the svg spec, the xml spec and the xml:id spec.
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.