我有以下user
个XML节点,这些节点具有id
和managerid
关系,彼此自引用,其中有id
个到managerid
个user
我需要为目标XML生成基于UUID的ID。但是我已经得到了以下解决方案来为他们生成UUID。我得到了具有相同id
和managerid
的重复<?xml version="1.0" encoding="UTF-8"?>
<userlist>
<user>
<id>1</id>
</user>
<user>
<id>2</id>
<managerid>1</managerid>
</user>
<user>
<id>2</id>
<managerid>1</managerid>
</user>
<user>
<id>2</id>
<managerid>1</managerid>
</user>
<user>
<id>3</id>
<managerid>1</managerid>
</user>
<user>
<id>3</id>
<managerid>1</managerid>
</user>
<user>
<id>4</id>
<managerid>2</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>9</id>
<managerid>3</managerid>
</user>
<user>
<id>10</id>
<managerid>1</managerid>
</user>
<user>
<id>10</id>
<managerid>1</managerid>
</user>
个节点。我只需要选择一个用户节点。我坚持在目标XML中将生成的uuid映射到managerid,我们将非常感谢任何帮助。
<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>
如果输入XML没有重复项,则下面的XSL可以正常工作。
<?xml version='1.0' encoding='UTF-8' ?>
<userlist>
<user>
<id>2fde0216-c017-4a2c-8372-5a0fd338a4ac</id>
</user>
<user>
<id>06d183b9-7dc6-4078-a77f-b32fb526fa61 01a9fc38-2f32-459d-88d9-
32cbde8bc570 87622536-eb87-408b-bb31-f3599d4fb286</id>
<managerid>2fde0216-c017-4a2c-8372-5a0fd338a4ac</managerid>
</user>
<user>
<id>06d183b9-7dc6-4078-a77f-b32fb526fa61 01a9fc38-2f32-459d-88d9-32cbde8bc570 87622536-eb87-408b-bb31-f3599d4fb286</id>
<managerid>2fde0216-c017-4a2c-8372-5a0fd338a4ac</managerid>
</user>
<user>
<id>06d183b9-7dc6-4078-a77f-b32fb526fa61 01a9fc38-2f32-459d-88d9-32cbde8bc570 87622536-eb87-408b-bb31-f3599d4fb286</id>
<managerid>2fde0216-c017-4a2c-8372-5a0fd338a4ac</managerid>
</user>
<user>
<id>3a4785d7-a878-46cb-96ff-772d127fd292 fa972787-47d3-405e-acb3-12fa1540caff</id>
<managerid>2fde0216-c017-4a2c-8372-5a0fd338a4ac</managerid>
</user>
<user>
<id>3a4785d7-a878-46cb-96ff-772d127fd292 fa972787-47d3-405e-acb3-12fa1540caff</id>
<managerid>2fde0216-c017-4a2c-8372-5a0fd338a4ac</managerid>
</user>
<user>
<id>e075096d-87b7-400c-b8ab-d9e02d8496cf e5381986-c788-4c1a-9d78-e965676da26c</id>
<managerid>06d183b9-7dc6-4078-a77f-b32fb526fa61 01a9fc38-2f32-459d-88d9-32cbde8bc570 87622536-eb87-408b-bb31-f3599d4fb286</managerid>
</user>
<user>
<id>e075096d-87b7-400c-b8ab-d9e02d8496cf e5381986-c788-4c1a-9d78-e965676da26c</id>
<managerid>06d183b9-7dc6-4078-a77f-b32fb526fa61 01a9fc38-2f32-459d-88d9-32cbde8bc570 87622536-eb87-408b-bb31-f3599d4fb286</managerid>
</user>
<user>
<id>7347f53a-0853-4995-810f-9c59b8cbb01f</id>
<managerid>3a4785d7-a878-46cb-96ff-772d127fd292 fa972787-47d3-405e-acb3-12fa1540caff</managerid>
</user>
<user>
<id>c2925611-f04e-4bc1-a887-9c117f933433</id>
<managerid>2fde0216-c017-4a2c-8372-5a0fd338a4ac</managerid>
</user>
<user>
<id>c843fe6e-399a-4f8c-9efc-8b864cd87a4c</id>
<managerid>06d183b9-7dc6-4078-a77f-b32fb526fa61 01a9fc38-2f32-459d-88d9-32cbde8bc570 87622536-eb87-408b-bb31-f3599d4fb286</managerid>
</user>
<user>
<id>e778eb2e-8989-4e9d-a0b3-4ee3b25cce07</id>
<managerid>3a4785d7-a878-46cb-96ff-772d127fd292 fa972787-47d3-405e-acb3-12fa1540caff</managerid>
</user>
<user>
<id>68cf6663-90b9-465a-b30d-a6305d715b64 de7cb144-a63f-4302-9ea5-60ff4646bd75</id>
<managerid>3a4785d7-a878-46cb-96ff-772d127fd292 fa972787-47d3-405e-acb3-12fa1540caff</managerid>
</user>
<user>
<id>68cf6663-90b9-465a-b30d-a6305d715b64 de7cb144-a63f-4302-9ea5-60ff4646bd75</id>
<managerid>3a4785d7-a878-46cb-96ff-772d127fd292 fa972787-47d3-405e-acb3-12fa1540caff</managerid>
</user>
<user>
<id>a9bb4d6c-534a-489b-b2ea-6c31346356eb 33220966-dfee-476b-bc50-6fc6e9fd28ab</id>
<managerid>2fde0216-c017-4a2c-8372-5a0fd338a4ac</managerid>
</user>
<user>
<id>a9bb4d6c-534a-489b-b2ea-6c31346356eb 33220966-dfee-476b-bc50-6fc6e9fd28ab</id>
<managerid>2fde0216-c017-4a2c-8372-5a0fd338a4ac</managerid>
</user>
</userlist>
当前输出:
exporting: {
buttons: {
contextButton: {
align: 'center',
x: 50
}
}
},
答案 0 :(得分:0)
上一个回答:XSL to generate UUIDs for users & replace for their manager references accordingly
怎么样:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:uuid="java:java.util.UUID"
exclude-result-prefixes="uuid">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- remove duplicates + generate a UUID for each distinct user -->
<xsl:variable name="new-list">
<xsl:for-each-group select="/userlist/user" group-by="id">
<user new-id="{uuid:randomUUID()}">
<xsl:copy-of select="current-group()[1]/*"/>
</user>
</xsl:for-each-group>
</xsl:variable>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/userlist">
<xsl:copy>
<xsl:apply-templates select="$new-list"/>
</xsl:copy>
</xsl:template>
<!-- replace existing ids with corresponding UUIDs -->
<xsl:key name="user-by-old-id" match="user" use="id" />
<xsl:template match="id | managerid">
<xsl:copy>
<xsl:value-of select="key('user-by-old-id', .)/@new-id"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@new-id"/>
</xsl:stylesheet>