473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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>lname</LastName>
<MiddleName>mName<MiddleName>
</Name>
<Information>
<Info1>
<Info2> --- level 3
<Info3>asdasd</Info> -- level 4
</Info2>
</Info1>

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


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>lname</LastName>
<MiddleName>mName<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.Depth > maxDepth) {
maxDepth = xmlReader.Depth;
}
Console.WriteLine(
"Node type: {0}, node name: {1}, node depth: {2}.", xmlReader.NodeType,
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:stylesheet 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(ancestor::node())"
data-type="number" order="descending"/>

<xsl:if test="position()=1">
<xsl:value-of select="count(ancestor::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*********@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.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>lname</LastName>
<MiddleName>mName<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
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...
2
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
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:...
8
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...
6
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...
13
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...
0
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...
2
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...
2
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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,...

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.