473,587 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find the depth of XML

I have following XML:
How can I find the depth of XML?
in other word max level of the XML

<Root> ----- level 0
<Name>----- level 1
<FirstName>name </FirstName> ---- level 2
<LasName>lnam e</LastName>
<MiddleName>mNa me<MiddleName>
</Name>
<Information>
<Info1>
<Info2> --- level 3
<Info3>asdasd </Info> -- level 4
</Info2>
</Info1>

</Information>
</Root>
Nov 12 '05 #1
2 8436


Raed Sawalha wrote:
I have following XML:
How can I find the depth of XML?
in other word max level of the XML

<Root> ----- level 0
<Name>----- level 1
<FirstName>name </FirstName> ---- level 2
<LasName>lnam e</LastName>
<MiddleName>mNa me<MiddleName>
</Name>
<Information>
<Info1>
<Info2> --- level 3
<Info3>asdasd </Info> -- level 4
</Info2>
</Info1>

</Information>
</Root>


XmlTextReader has a Depth property which you could store, however note
that with that approach the text node inside of the <Info3> element is
on level (or depth) 5:

XmlTextReader xmlReader = new XmlTextReader(@ "test2005060501 .xml");
int maxDepth = 0;
while (xmlReader.Read ()) {
if (xmlReader.Dept h > maxDepth) {
maxDepth = xmlReader.Depth ;
}
Console.WriteLi ne(
"Node type: {0}, node name: {1}, node depth: {2}.", xmlReader.NodeT ype,
xmlReader.Name, xmlReader.Depth );
}
Console.Write(" Maximum depth is: {0}.", maxDepth);

If you do not want to count text nodes or comment nodes you need to
check the NodeType and only change maxDepth for the node types you are
looking for.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
> How can I find the depth of XML?
in other word max level of the XML
Using XSLT 1.0 this can be done in the following way:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="//node()[not(node())]">
<xsl:sort select="count(a ncestor::node() )"
data-type="number" order="descendi ng"/>

<xsl:if test="position( )=1">
<xsl:value-of select="count(a ncestor::node() )"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Using XPath 2.0 one could write a single XPath expression which evaluates to
the maximum depth of the source xml document:

max(//*[not(*)]/count(ancestor: :node()))

And the following XPath 2.0 expression selects all nodes that are at maximum
depth:
//*[not(*)][count(ancestor: :node())
=
max(//*[not(*)]/count(ancestor: :node()))
]

Cheers,
Dimitre Novatchev

"Raed Sawalha" <Ra*********@di scussions.micro soft.com> wrote in message
news:04******** *************** ***********@mic rosoft.com...I have following XML:
How can I find the depth of XML?
in other word max level of the XML

<Root> ----- level 0
<Name>----- level 1
<FirstName>name </FirstName> ---- level 2
<LasName>lnam e</LastName>
<MiddleName>mNa me<MiddleName>
</Name>
<Information>
<Info1>
<Info2> --- level 3
<Info3>asdasd </Info> -- level 4
</Info2>
</Info1>

</Information>
</Root>

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3239
by: Hoagie | last post by:
Hello all. Is it possible to set a new depth of field (View.setBackClipDistance) when using a SimpleUniverse? I've been unable to learn a way to extract the View being used, in order to set these values. Once I am able to set the clip distance -- is there a way to define an infinite depth? I'd like to zoom in and out of an environment without limits, and with no clipping.
2
4160
by: Onwuka Emeka | last post by:
is there a way to reduce or modify the color depth of a jpeg image, i currently need to programatically change the color depth of a jpeg image from 24bits to 8bits. any help would be appreciated
4
967
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD: (there is a WORD in ( my string WORD )) and * WORD * to (find WORD) and * WORD * Should give me the to word between star (star ar not part of string)
8
2911
by: darrel | last post by:
I've decided that instead of doing an XSLT transformation on a file, I might be better off bringing it in as a dataset and having a bit more direct control over it at that point. The question I have is if one can determine the node depth as they import the XML. I could, obviously, just put the node depth in as another xml element at the time of creation, which really isn't a big deal, but thought I'd see if I could do it on the import...
6
7203
by: datamodel | last post by:
Hello I have an XML tree of which you can see a mini-version here: http://paste.uni.cc/11838 (the tree is actually over 30,000 levels deep) How do I count the depth of a given <NODES><NODE????? The problem is I would like to "color" nodes based on their depth in the tree - ie, use alternating colors.
13
3816
by: softwaredoug | last post by:
I can't see to easily find this on google or in a newsgroup Is there a standard function/macro/whatever you can call and determine the distance in a C program how deep one is in the C call stack from main. Something along the lines: int main() {
0
12778
by: jiing | last post by:
Hi all, I want to use sting(the same as Node.Text) to judge if a node exists in TreeView. I've tried several ways, but seems all failed. could anybody help me? Thanks in advance. //My TreeView is defined to has depth =3 only.
2
11246
by: Gentr1 | last post by:
Hi everybody! I am presently working on a Genetic Programming API in python. I have a bit of a problem at the moment... For some specific reasons, I am using nested lists data structure to represent a tree. The first element of a list is always the head of the node, while all others represent the tail of the node. e.g. the nested list , E], ] represent the following tree: http://www.csd.abdn.ac.uk/~mkhoury/tree.jpg I need to be able...
2
3118
by: Man4ish | last post by:
Hi , How we can find the cyclic path and path between two nodes using Depth first search algorithm in Graph Data Structure. Can i get Pseudo code or code doing the same.I will be very much thankful to you.Please Help me out. Manish Gupta
0
7924
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7854
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.