Unique id attribute for every xml node?

Last post 02-04-2008 1:08 AM by werner747. 6 replies.

Sort Posts:

  • Unique id attribute for every xml node?

    01-29-2008, 7:30 AM
    • Member
      1 point Member
    • werner747
    • Member since 01-29-2008, 4:13 AM
    • Posts 23

    Hi,

     I have a windows vb.net form with a treeview control on it. I have built it so that it reads from xml doc, and populate all nodes. I can then insert, delete, and edit nodes. Once I am done, I save the xml file again.

    Just one problem, I need to add a unique attribute to each node that is created, and also add that unique value to another attribute of the same node.  For example: when adding the node:

    <element id=”myelement” uniqueid=”need unique id here” link=”linkpage.aspx?uid= need same unique id here”>

    I have tried using an xslt file with generate-id() function, but I would prefer another way, let’s say when I create the xml node to add, I just need to insert a value to an attribute that will be unique across the entire document.

    As I am using a treeview control, I thought I could use the index property of the treenode, but it is only relative to its parent nodes.

    Is there another property that I can use, to uniquely mark my nodes, or any other suggestions?

     

  • Re: Unique id attribute for every xml node?

    01-29-2008, 8:28 AM
    • Contributor
      3,945 point Contributor
    • Bonekrusher
    • Member since 08-23-2006, 12:30 AM
    • Posts 895

     Hi, I use XSLT to add ids:

     

    eg.

     

    <xsl:template match="*">
    <xsl:copy>
    <xsl:choose>
    <xsl:when test="@*">
    <xsl:copy-of select="@*"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:attribute name="uniqueid"><xsl:value-of select="generate-id()"/></xsl:attribute>
    </xsl:otherwise>
    </xsl:choose>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>
      
    If I was helpful, please mark "answered" so I can get credit. Thanks!
  • Re: Unique id attribute for every xml node?

    01-29-2008, 9:51 AM
    • Member
      1 point Member
    • werner747
    • Member since 01-29-2008, 4:13 AM
    • Posts 23

    Thanks BoneKrusher, but the only problem I have with this is that I cant assign the id's while creating the nodes. If I undestand ot correctly I need to create the enitire document, and the apply the stylesheet. I also have problems with that, as the doc is still referenced, and i get "Cannot lod doc" errors.

    Like I said, I also need to add the same unique id to another attribute that has already been created that already has some value, as in:

    <element id=”myelement” uniqueid=”need unique id here” link=”linkpage.aspx?uid= need same unique id here”>

     

    Any solutions?, or other ideas?

    Thanks

  • Re: Unique id attribute for every xml node?

    01-29-2008, 10:01 AM
    • Contributor
      3,945 point Contributor
    • Bonekrusher
    • Member since 08-23-2006, 12:30 AM
    • Posts 895

     Ok, I understand. What code are you using to create the XML? I have a "Random Number" class 

    Public Class RandomNumber
        Public Function RandomNumber(ByVal MaxNumber As Integer, _
            Optional ByVal MinNumber As Integer = 0) As Integer
    
            'initialize random number generator
            Dim r As New Random(System.DateTime.Now.Millisecond)
    
            'if passed incorrect arguments, swap them
            'can also throw exception or return 0
    
            If MinNumber > MaxNumber Then
                Dim t As Integer = MinNumber
                MinNumber = MaxNumber
                MaxNumber = t
            End If
    
            Return r.Next(MinNumber, MaxNumber)
    
        End Function
    End Class

      that will assign a random number when creating the xml (vb)'

     

    To call it:

      

                Dim getNewId As New RandomNumber
                Dim newID As Integer
                newID = getNewId.RandomNumber(9999, 0)
     
    If I was helpful, please mark "answered" so I can get credit. Thanks!
  • Re: Unique id attribute for every xml node?

    01-30-2008, 3:36 AM
    • Member
      1 point Member
    • werner747
    • Member since 01-29-2008, 4:13 AM
    • Posts 23

    Thanks, that looks good, I will be able to call such a function, but will it always supply a Unique number?

  • Re: Unique id attribute for every xml node?

    01-30-2008, 6:43 AM
    Answer
    • Contributor
      3,945 point Contributor
    • Bonekrusher
    • Member since 08-23-2006, 12:30 AM
    • Posts 895

     Its random, so there is always a slim chance you might get a repeat. To avoid the changes increase the range:

                Dim getNewId As New RandomNumber
    Dim newID As Integer
    newID = getNewId.RandomNumber(999999, 0)

     

    If I was helpful, please mark "answered" so I can get credit. Thanks!
  • Re: Unique id attribute for every xml node?

    02-04-2008, 1:08 AM
    Answer
    • Member
      1 point Member
    • werner747
    • Member since 01-29-2008, 4:13 AM
    • Posts 23

    Thanks. I have come accross it once that a number was duplicated, so I wrote a function to search the entire document, before adding a number, just to make sure the are no duplicates. But you RandomGenerator woks great. Thanks

Page 1 of 1 (7 items)