I’m trying to compose xml elements into each other, and the problem I am having is when there’s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the references to them. (I’m doing this with SVGs to add a little context)
Say I have:
<bar id="foo"/>
<baz ref="url(#foo)"/>
<bar id="abc"/>
<baz ref="asdf:url(#abc)"/>
I’d like a way to automatically turn that into something like:
<bar id="foo_1"/>
<baz ref="url(#foo_1)"/>
<bar id="abc_1"/>
<baz ref="asdf:url(#abc_1)"/>
or something similar.
I can probably write some XSL to do it, but was hoping there’s an easier way.
Thanks!
If you end up using XSLT, you may find the
generate-idfunction useful for generating ids.Here’s a sort of dummy example using XSLT 1.0:
If you don’t like the id’s produced by
generate-id(or if you cannot use it for other reasons — to ensure that you get unique id’s all the nodes need to be processed within the same transformation) you can replace the calls to it with some other logic, like adding a suffix.