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

给你一囗甜甜゛ 提交于 2019-12-25 05:35:14

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/29739320/xsl-to-generate-uuids-for-users-replace-for-their-manager-references-according

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!