how to generate unique usernames?

S

sjangity

I am generating xml file that will populate users in a database.

assigned_to contains names of developers.
reporter contains names of quality engineers.

I can't simply just create users filtering by //assigned_to or //
reporter as there will be many DUPLICATES. Why? Well, developers are
also reporters AND usernames are unique in the database.

Sample XML:
-------------
<psychology>
<issue>
<assigned_to>bob</assigned_to>
<reporter>sjangity</reporter>
</issue>
<issue>
<assigned_to>daniel</assigned_to>
<reporter>bob</reporter>
</issue>
</psychology>

How would the xslt look like anyone have any ideas? In the above
sample, I want to be able to create 3 users in total (bob, sjangity,
daniel).

Note: bob is both a developer and a QA contact.

Thanks!
 
M

Martin Honnen

<psychology>
<issue>
<assigned_to>bob</assigned_to>
<reporter>sjangity</reporter>
</issue>
<issue>
<assigned_to>daniel</assigned_to>
<reporter>bob</reporter>
</issue>
</psychology>

How would the xslt look like anyone have any ideas? In the above
sample, I want to be able to create 3 users in total (bob, sjangity,
daniel).


Here is a sample XSLT 1.0 stylesheet:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:eek:utput method="xml" indent="yes"/>

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

<xsl:template match="/">
<users>
<xsl:apply-templates select="psychology/issue/*[generate-id() =
generate-id(key('name', .)[1])]"/>
</users>
</xsl:template>

<xsl:template match="issue/*">
<user><xsl:value-of select="."/></user>
</xsl:template>

</xsl:stylesheet>

XSLT 2.0 solution:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="/">
<users>
<xsl:for-each-group select="psychology/issue/*" group-by=".">
<user>
<xsl:value-of select="current-grouping-key()"/>
</user>
</xsl:for-each-group>
</users>
</xsl:template>

</xsl:stylesheet>
 
S

sjangity

Hi Martin,

I ran the XSLT 1.0 version against the sample xml file mentioned above
and I am getting an empty result post transformation. Are you sure
that does the trick?

Thanks again for your feedback.

/Sandeep

<psychology>
<issue>
<assigned_to>bob</assigned_to>
<reporter>sjangity</reporter>
</issue>
<issue>
<assigned_to>daniel</assigned_to>
<reporter>bob</reporter>
</issue>
</psychology>
How would the xslt look like anyone have any ideas? In the above
sample, I want to be able to create 3 users in total (bob, sjangity,
daniel).

Here is a sample XSLT 1.0 stylesheet:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:eek:utput method="xml" indent="yes"/>

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

<xsl:template match="/">
<users>
<xsl:apply-templates select="psychology/issue/*[generate-id() =
generate-id(key('name', .)[1])]"/>
</users>
</xsl:template>

<xsl:template match="issue/*">
<user><xsl:value-of select="."/></user>
</xsl:template>

</xsl:stylesheet>

XSLT 2.0 solution:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="/">
<users>
<xsl:for-each-group select="psychology/issue/*" group-by=".">
<user>
<xsl:value-of select="current-grouping-key()"/>
</user>
</xsl:for-each-group>
</users>
</xsl:template>

</xsl:stylesheet>
 
G

George Bina

hI Sandeep,

Martin's solutions work ok from my tests so make sure you are testing
exactly with the files XML and XSLT that were posted here. If you
still have problems let us know what processor you are using and how
you are performing the transformation.
One comment will be that the 2.0 solution can be also more easily
encoded as below, using distinct-values instead of for-each-group:

<xsl:for-each select="distinct-values(psychology/issue/*)">
<user><xsl:value-of select="."/></user>
</xsl:for-each>

Best Regards,
George
 
M

Martin Honnen

I ran the XSLT 1.0 version against the sample xml file mentioned above
and I am getting an empty result post transformation. Are you sure
that does the trick?

That stylesheet is fine in my view, result for your sample input is

<users>
<user>bob</user>
<user>sjangity</user>
<user>daniel</user>
</users>

which is what you want I think.
 
S

sqad

Odd, I ran it through Altova XMLSpy editor and didn't see anything
after transformation. I'll investigate.

Thanks, Martin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,007
Messages
2,570,266
Members
46,865
Latest member
AveryHamme

Latest Threads

Top