how to generate unique usernames?

Discussion in 'XML' started by sjangity@gmail.com, Sep 16, 2007.

  1. Guest

    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!
     
    , Sep 16, 2007
    #1
    1. Advertising

  2. wrote:

    > <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>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 16, 2007
    #2
    1. Advertising

  3. Guest

    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

    On Sep 16, 9:43 am, Martin Honnen <> wrote:
    > wrote:
    > > <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>
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
     
    , Sep 16, 2007
    #3
  4. George Bina Guest

    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
    ---------------------------------------------------------------------
    George Cristian Bina - http://aboutxml.blogspot.com/
    <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
    http://www.oxygenxml.com
     
    George Bina, Sep 17, 2007
    #4
  5. wrote:

    > 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.




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 17, 2007
    #5
  6. sqad Guest

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

    Thanks, Martin.

    On Sep 17, 5:46 am, Martin Honnen <> wrote:
    > wrote:
    > > 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.
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
     
    sqad, Sep 17, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    IIS6 not logging Authenticated Usernames

    =?Utf-8?B?Sm9l?=, Sep 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    528
    Bruce Barker
    Sep 16, 2005
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    444
    Brian Palmer
    Aug 20, 2003
  3. Aaron Whiffin
    Replies:
    0
    Views:
    601
    Aaron Whiffin
    Sep 4, 2003
  4. ToshiBoy
    Replies:
    6
    Views:
    850
    ToshiBoy
    Aug 12, 2008
  5. Token Type
    Replies:
    9
    Views:
    359
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page