Samdalton
Samdalton

Reputation: 21

XSL to generate UUIDs for users & replace for their manager references accordingly

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 target XML 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://www.w3.org/1999/XSL/Transform" 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>

Upvotes: 0

Views: 356

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

Your question is very poorly worded. If I understand correctly, managers are also users. Therefore, you want to replace each user's id with a UUID and - if that user is also a manager - replace the corresponding managerid with the same UUID.

This could be accomplished by:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:uuid="java:java.util.UUID">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="new-ids">
    <xsl:for-each select="/userlist/user">
        <new-id old-id="{id}">
            <xsl:value-of select="uuid:randomUUID()"/>
        </new-id>
    </xsl:for-each>
</xsl:variable>

<xsl:key name="new-id" match="new-id" use="@old-id" />

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

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

</xsl:stylesheet>

Upvotes: 1

Related Questions