Jump to content

Process XML, Language independant


aspnetguy

Recommended Posts

I posted this here because it doesn't relate to a specific language I am looking for an algorythm.I have the following XML

<tree>	<node>		<node/>		<node>			<node/>			<node/>		</node>	</node>	<node/>	<node>		<node>			<node/>			<node>				<node/>				<node/>			</node>		</node>		<node/>		<node>			<node/>			<node/>		</node>	</node></tree>

What I need to do is process this to produce the following result.

<tree>	<node id="1">		<node id="4"/>		<node id="5">			<node id="9"/>			<node id="10"/>		</node>	</node>	<node id="2"/>	<node id="3">		<node id="6">			<node id="11"/>			<node id="12">				<node id="15"/>				<node id="16"/>			</node>		</node>		<node id="7"/>		<node id="8">			<node id="13"/>			<node id="14"/>		</node>	</node></tree>

What is the most efficient way to do this, I need to navigate throught tree in the id order.I have been trying how to think of a way to do this but everything I can think of does quite work.

Link to comment
Share on other sites

Yikes. hehWhat about something like this? Write a function that gets all the top level child elements of an element and places them in an array. This could be done with .nextSibling in javascript (the function presented on this page helped tremendously in a recent project I did). Then, in the same function, loop through that array and call that function again (recursively) on each node that is in your array. You could then have a global counter that increments by one each time an element is found and you could use that counter as your identifier for the elements. If you make sure that each element in the array is processed before the recursion happens, (e.g. maybe use two loops - once to loop through the siblings and assign an id and once again to call the function recursively) the ids should be assigned correctly. This way, the top level elements would have ids 1, 2, and 3. The children of 1 would have ids 4, and 5. The first decendents of 2 would have ids 6, 7, and 8. etc. etc.I hope it helps!

Link to comment
Share on other sites

I can make an XSLT variation (choose whatever language and it's processor to parse..huh).But for that... jesh, could you suggest some non-array logic? XSLT 1.0 doesn't have arrays or anything that can be used like them, so that's not exactly useful.Keep in mind I can still count elements on one level and loop through them as if they are an array. I can also recursively execute the same template on the next level and set parameters for each level. One of the very few things that I can view and act upon in a single loop is the position of the current node (for example, do one thing if the position of the current element is less then N, and another if not).P.S. I've really lost my touch lately.

Link to comment
Share on other sites

But for that... jesh, could you suggest some non-array logic?Keep in mind I can still count elements on one level and loop through them as if they are an array. I can also recursively execute the same template on the next level and set parameters for each level.
I thought about it on my bike ride home and came up with something else. Can you do while loops? Loops that continue until some condition is met? If so, can you also get all the elements of a certain level (like you hint at in your post)? If so, perhaps you could do something like this:Pseudo-code:
level = 1;id_incrementer = 1;while(there are elements at this level){	for each (element in this level)	{		assign the id for the element to id_incrementer		increase id_incrementer by 1	}	increase the level by 1}

Alternatively:

level = 1id_incrementer = 1start:if(there are elements at this level){	for each(element at this level)	{		assign the id for the element to id_incrementer		increase id_incrementer by 1	}	increase the level by 1	goto start}

Link to comment
Share on other sites

increase id_incrementer by 1	}	increase the level by 1	goto start}

This part might actually be the reason I didn't come to a solution at my initial post. I can only initiate parameters for when I start the new level. There and ONLY there. Much like a DOS command. Pass parameters on executing like so (code conceptual and unreal):
start -level $level+1 -id_increment $id_increment+1

And I don't have "while", but the for-each won't start if there are no further elements.Anyhow, I'm not in front of the "drawing board" right now, so I'll see what I can come up with.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...