[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Merging Two Nodesets .. can it be done?


Subject: Re: [xsl] Merging Two Nodesets .. can it be done?
From: "Wasiq Shaikh" <wasiq911@xxxxxxxxxxx>
Date: Tue, 14 Aug 2007 09:22:36 -0400

Thank you for the reply Abel. I do not have a stylesheet that does (or even attempts) a merge process so I really can't give a sample of anything. Im using XPath1.0/XalanJ 2.7 and yes I can use the node-set function.

The ordering of elements does not matter but the concept of the merge should be consistent, meaning all "like" elements are joined.

Yes, nodes must be on the same level to be comparable (mergeable?). Nodes on any other level with the same likeness should not be merged since they are part of a different node generation. For example:

<X>
  <Z>
     <Z>...

The parent and child <Z> elements should not be merged.

The algorithm you mention is what I was thinking about doing. I know it's quite simple, however, the amount of work the processor needs to do in comparing each and every similar node is expensive. Joining two nodes is fine, but what happens if you have tens, hundreds, or thousands of similar nodes to merge? Then each child of those many nodes needs to be compared and merged as well, and so on and so forth...

I know it can be done in XSL, but can it handle such a process? Or is this the work for procedural programming like Java?

----Original Message Follows----
From: Abel Braaksma <abel.online@xxxxxxxxx>

Hi Wasiq,

Yes, it can be done and should not be too hard.

There are very many answers possible, largely depending on some information you did not yet give:
1. What XSLT version do you use? It makes a large difference whether you do this in 1.0 or 2.0. If 1.0, can use the node-set extension function?
2. Can you define your "merging" more detailed? In your example you also change the order (<aaa> ends up under <Z>), how is that defined?
3. How does your current stylesheet looks like that makes the current nodes?
4. Do you mean that the nodes that need to be merged are siblings? Or are they separate variables / input docs?


You'd understand that the XSLT spec does not yield a specific function for that considering the very wide ways of interpreting what "merging" means.

I think that in general an approach would be something along the following lines, considering you can use XSLT 2.0, or XSLT 1.0 with a node-set extension.

a. Assume your current two node sets (the ones that need to be merged) are siblings and you apply to the root of the first node that need to be merged

b. In the matching template you select everything from the second node-set that is not in the current node set (you can choose to define the level of your match, i.e., identity (not good for you I think), equal names, equal local names).

c. Apply the next ones that have children themselves and do the same there.


Hmm, this is quite basic / rough. The trouble is in getting the selected node sets right. If you supply some more information (preferably with what you currently have in your stylesheet), I can help you further.


Cheers,
-- Abel Braaksma

Wasiq Shaikh wrote:
Quick question .. is there a way to merge two node-sets in XSL that have similar structure?

Example Input: (generated output from XSLT)

<X>
<Y>
<aaa>
<Z>
<bbb>
<ccc>
</Z>
</Y>
<Y>
<ddd>
<eee>
<Z>
<fff>
</Z>
</Y>
</X>

Desired Output: (This is how I would like to get it in the same XSLT process)

<X>
<Y>
<Z>
<bbb>
<ccc>
<fff>
</Z>
<aaa>
<ddd>
<eee>
</Y>
</X>

Notice the Y elements are merged, as well as the Z elements.

Is there any function or algorithm out there that can do this in one shot (or at all)? Or do I have to turn this into a two step process by making the generated output an input do another XSL that does the merge?

Thanks for any insight!

W.S


_________________________________________________________________
Show Your Messenger Buddies How You Really Feel http://www.freemessengeremoticons.ca/?icid=EMENCA122



Current Thread
Keywords