I want to add a unique attribute say "ind" to every tag in the xml. How do i do 开发者_运维技巧it using xsl. It need'nt be a sequence number. As long it is unique for every tag it is sufficient.
Take identity transformation, add template for elements in which add attribute with value generated by generate-id().
Something like this?
It also uses a unique namespace for the attribute we are adding so we don't override any existing attributes with ours if they have the same name.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:mycomp="http://www.myuniquenamespace">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()">
<xsl:element name="{local-name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="@*"/>
<xsl:attribute name="someattr" namespace="http://www.myuniquenamespace">
<xsl:value-of select="generate-id()"/>
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@* | text()">
<xsl:copy />
</xsl:template>
</xsl:stylesheet>
Hope this helps you on your way,
精彩评论