I have an XML document that I want to generate unique IDs for. Some nodes may already have the attribute in which case this is to be replaced. I want all nodes in the document to have the attribute.
An example document would be
<root>
<anode uid='123'/>
<anode/>
</root>
I am using the following stylesheet
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="*">
<xsl:copy>
<xsl:attribute name="uid">
<xsl:value-of select="generate-id(.)"/>
</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And am getting the following output. This is close to what I want, but how do I prevent the existing ID from being created as a text node?
<root uid="id515559">
<anode uid="id515560">123</anode>
<anode uid="id515562"/>
</root>
I have looked at XSLT: How to change an attribute value during <xsl:copy>? but I couldn't get this to create new attributes.
If it makes a difference I'm using lxml to process the stylesheet.