468,483 Members | 2,683 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,483 developers. It's quick & easy.

XSL Transformation with recursive elements

Hello,

I have an XML with approximately the following structure, the relevant characteristic here the recursive relation of the element pair "F" and "Child_Fs". "Child_Fs" can contain any number of "F" and "F" can contain only one "Child_Fs":

Expand|Select|Wrap|Line Numbers
  1.     <A>
  2.         <B>
  3.             <F id="1">
  4.                 <J/>
  5.                 <K/>
  6.                 <Child_Fs>
  7.                     <F id="1.1">
  8.                         <J/>
  9.                         <K/>
  10.                         <Child_Fs>
  11.                             <F id="1.1.1">
  12.                                 <J/>
  13.                                 <K/>
  14.                                 <Child_Fs>
  15.                                         ...
  16.                                 </Child_Fs>
  17.                             </F>
  18.                             <F id="1.1.2">
  19.                                 ...
  20.                             </F>
  21.                             <F id="1.1.3">
  22.                                 ...
  23.                             </F>
  24.                             <F id="1.1.4">
  25.                                 ...
  26.                             </F>
  27.                             .
  28.                             .
  29.                             .
  30.                         </Child_Fs>
  31.                     </F>
  32.                     <F id="1.2">
  33.                         ...
  34.                     </F>
  35.                     <F id="1.3">
  36.                         ...
  37.                     </F>
  38.                     <F id="1.4">
  39.                         ...
  40.                     </F>
  41.                     .
  42.                     .
  43.                     .
  44.                 </Child_Fs>
  45.             </F>
  46.             <F id="2">
  47.                 ...
  48.             </F>
  49.             <F id="3">
  50.                 ...
  51.             </F>
  52.             <F id="4">
  53.                 ...
  54.             </F>
  55.             .
  56.             .
  57.             .
  58.             <G/>
  59.             <H/>
  60.             <I/>
  61.         </B>
  62.         <C/>
  63.         <D/>
  64.         <E/>
  65.     </A>
  66.  

My actual XML doesn't contains IDs, I just wrote them in this example for ilustration purposes.

So what I would like to get after the transformations is the following XML, in which all "F" elements are children of their corresponding highest "F" ancestor. Meaning that the maximal depth for an F element should be of only two occurrances. The other important requirement here is to keep all data (attributes and text inclusive) intact, it is just a relocation operation:

Expand|Select|Wrap|Line Numbers
  1.     <A>
  2.         <B>
  3.             <F id="1">
  4.                 <J/>
  5.                 <K/>
  6.                 <Child_Fs>
  7.                     <F id="1.1">
  8.                         <J/>
  9.                         <K/>
  10.                         <Child_Fs>
  11.                         </Child_Fs>
  12.                     </F>
  13.                     <F id="1.1.1">
  14.                         <J/>
  15.                         <K/>
  16.                         <Child_Fs>
  17.                         </Child_Fs>
  18.                     </F>
  19.                     ...
  20.                     <F id="1.1.2">
  21.                         ...
  22.                     </F>
  23.                     <F id="1.1.3">
  24.                         ...
  25.                     </F>
  26.                     <F id="1.1.4">
  27.                         ...
  28.                     </F>
  29.                     .
  30.                     .
  31.                     .
  32.                     <F id="1.2">
  33.                         ...
  34.                     </F>
  35.                     <F id="1.3">
  36.                         ...
  37.                     </F>
  38.                     <F id="1.4">
  39.                         ...
  40.                     </F>
  41.                     .
  42.                     .
  43.                     .
  44.                 </Child_Fs>
  45.             </F>
  46.             <F id="2">
  47.                 ...
  48.             </F>
  49.             <F id="3">
  50.                 ...
  51.             </F>
  52.             <F id="4">
  53.                 ...
  54.             </F>
  55.             .
  56.             .
  57.             .
  58.             <G/>
  59.             <H/>
  60.             <I/>
  61.         </B>
  62.         <C/>
  63.         <D/>
  64.         <E/>
  65.     </A>
  66.  
I would appreciate it a lot if anyone could give me a hint on this. Till now I've not been able to come up with a correct XSL Stylesheet.

Many thanks in advance.
Jul 16 '12 #1
1 1714
Anas Mosaad
185 128KB
Hint: In a for-each loop or a template that matches your Child_Fs you can use \\F to indicate all F node under the Child_Fs node.
Jan 6 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by bearophileHUGS | last post: by
4 posts views Thread by Kevin Dean | last post: by
4 posts views Thread by Mike Conmackie | last post: by
1 post views Thread by walexand | last post: by
2 posts views Thread by rankam | last post: by
1 post views Thread by Paul Guz | last post: by
5 posts views Thread by monmonja | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.