Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. add element unique Id XSLT

add element unique Id XSLT

Scheduled Pinned Locked Moved XML / XSL
databasexml
2 Posts 1 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mvujica
    wrote on last edited by
    #1

    I am quite new to XSLT and I want to add unique index to elements types to transform something like this:

    <item1>
    <item2>
    <item3></item3>
    <item3></item3>
    </item2>
    <item2>
    <item3></item3>
    <item3></item3>
    </item2>
    </item1>
    <item1>
    </item1>

    into:

    <item1 id="1">
    <item2 id="1">
    <item3 id="1"></item3>
    <item3 id="2"></item3>
    </item2>
    <item2 id="2">
    <item3 id="3"></item3>
    <item3 id="4"></item3>
    </item2>
    </item1>
    <item1 id="2">
    </item1>

    I tried using position() but it counts based on context and not globally on document. Thanx. :^)

    M 1 Reply Last reply
    0
    • M mvujica

      I am quite new to XSLT and I want to add unique index to elements types to transform something like this:

      <item1>
      <item2>
      <item3></item3>
      <item3></item3>
      </item2>
      <item2>
      <item3></item3>
      <item3></item3>
      </item2>
      </item1>
      <item1>
      </item1>

      into:

      <item1 id="1">
      <item2 id="1">
      <item3 id="1"></item3>
      <item3 id="2"></item3>
      </item2>
      <item2 id="2">
      <item3 id="3"></item3>
      <item3 id="4"></item3>
      </item2>
      </item1>
      <item1 id="2">
      </item1>

      I tried using position() but it counts based on context and not globally on document. Thanx. :^)

      M Offline
      M Offline
      mvujica
      wrote on last edited by
      #2

      I have got the answer from daniweb (thanks fpmurphy) :

      1.  <?xml version="1.0"?>
        
      2.  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
        
      3.  <xsl:output method="xml" indent="yes"/>
        
      4.  <xsl:template match="/">
        
      5.  <xsl:apply-templates/>
        
      6.  </xsl:template>
        
      7.  <xsl:template match="\*">
        
      8.  <xsl:variable name="name" select="name()" />
        
      9.  <xsl:element name="{name()}">
        
      10.  <xsl:attribute name="id">
        
      11.  <xsl:value-of select="count(preceding::\*\[name()=$name\]) + 1" />
        
      12.  </xsl:attribute>
        
      13.  <xsl:apply-templates />
        
      14.  </xsl:element>
        
      15.  </xsl:template>
        
      16.  </xsl:stylesheet>
        
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups