My XML document looks like this-
<doc>
<system_data>
<registry_item id="1">
<hive>HKEY_LOCAL_MACHINE</hive>
</registry_item>
<file_item id="2">
<filepath>C:\Windows\System32\msasn1.dll</filepath>
</file_item>
</system_data>
</doc>
I would like to transform it such as “id” attribute value is incremented sequentially e.g :
<doc>
<system_data>
<registry_item id="10">
<hive>HKEY_LOCAL_MACHINE</hive>
</registry_item>
<file_item id="11">
<filepath>C:\Windows\System32\msasn1.dll</filepath>
</file_item>
</system_data>
</doc>
I am using the “identity” design and my xsl looks like this-
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xslt="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="Identity.xsl"/>
<xslt:param name="newID" />
<xsl:template match="//system_data/*">
<xsl:for-each select="@id">
<xsl:attribute name="id">
<xsl:value-of select="number($newID)+1"/>
</xsl:attribute>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I am passing newID value as “10”. How to I select each attribute and increment its value?
Indentity.xsl looks like this-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Whenever you match any node or any attribute -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When I apply xsl, I am getting following error-
An attribute node (id) cannot be created after the children of the containing element
Try changing your template for
match="//system_data/*"to this: