• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Find Level of Element in XML

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I need to find the Level number of a Node in a XML file.
I am not able to track that.Can anyone help me out?

for ex:

<root>
<parent1>
<child1>
<child2>
</child2>
</child1>
</parent1>

<parent2>
<child21>
</child21>
</parent2>
<parent3>
</parent3>
</root>

I have to get the level of root as 0 and parent1, parent2 as 1 and child1,child21 as 2 etc..

Thanks in Advance
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Jeya:

I have to get the level of root as 0 and parent1, parent2 as 1 and child1,child21 as 2 etc..

Thanks in Advance


Have you tried position() function in XSLT (if you are using xslt)
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The position() function will not work but if you're using the DOM API, you can write a simple method like this:

Note that I didn't even compile the above, let alone write a unit test for it, so it might need some tweaking.
reply
    Bookmark Topic Watch Topic
  • New Topic