XSL 为用户生成 UUID相应地替换他们的经理参考 [英] XSL to generate UUIDs for users & replace for their manager references accordingly
问题描述
我在下面的 XML 中使用 id
和 managerid
相互引用,许多 id 到一个 managerid
,我需要转换将它们的 id 转换为基于 UUID
的目标 XML 的 id.我正在通过扩展功能使用 Java uuid class
.我对目标 XML 中的 maaping 生成 uuid
到 managerid
感到震惊,任何帮助将不胜感激.
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>
推荐答案
您的问题措辞非常糟糕.如果我理解正确,管理者也是用户.因此,您希望将每个用户的 id
替换为 UUID 并且 - 如果该用户也是经理 - 将相应的 managerid
替换为 <强>相同 UUID.
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>
这篇关于XSL 为用户生成 UUID相应地替换他们的经理参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!