XSL to generate UUIDs for it's element and map thier self-referenced ids with generated UUIDs



I have this below xml with id and managerid self referenced to each other with many ids to one managerid and i would need to convert their ids to UUID based ids to the target xml. i'm using java uuid class via extension function. I'm struck with maaping genrated uuid to managerid in targetxml and any help would be greatly appreciated.



<?xml version="1.0" encoding="UTF-8"?>
<userlist>
<user>
<id>1</id>
</user>
<user>
<id>2</id>
<managerid>1</managerid>
</user>
<user>
<id>3</id>
<managerid>1</managerid>
</user>
<user>
<id>4</id>
<managerid>2</managerid>
</user>
<user>
<id>5</id>
<managerid>3</managerid>
</user>
<user>
<id>6</id>
<managerid>1</managerid>
</user>
<user>
<id>7</id>
<managerid>2</managerid>
</user>
<user>
<id>8</id>
<managerid>3</managerid>
</user>
<user>
<id>9</id>
<managerid>3</managerid>
</user>
<user>
<id>10</id>
<managerid>1</managerid>
</user>
</userlist>


XSL:



<xsl:stylesheet version="2.0" xmlns:xsl="http://ift.tt/tCZ8VR" xmlns:uuid="java:java.util.UUID">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>



<xsl:key name="id" match="id" use="."/>

<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="id">
<xsl:variable name="uid" select="uuid:randomUUID()"/>
<xsl:copy>
<xsl:value-of select="$uid"/>
</xsl:copy>
</xsl:template>

<xsl:template match="managerid">
<xsl:copy>
<xsl:value-of select="key('id', .)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

No comments:

Post a Comment