XSL Transformation with recursive elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmsr1782
    New Member
    • Jul 2012
    • 1

    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":

    Code:
        <A>
        	<B>
        		<F id="1">
        			<J/>
        			<K/>
        			<Child_Fs>
        				<F id="1.1">
        					<J/>
        					<K/>
        					<Child_Fs>
        						<F id="1.1.1">
        							<J/>
        							<K/>
        							<Child_Fs>
        									...
        							</Child_Fs>
        						</F>
        						<F id="1.1.2">
        							...
        						</F>
        						<F id="1.1.3">
        							...
        						</F>
        						<F id="1.1.4">
        							...
        						</F>
        						.
        						.
        						.
        					</Child_Fs>
        				</F>
        				<F id="1.2">
        					...
        				</F>
        				<F id="1.3">
        					...
        				</F>
        				<F id="1.4">
        					...
        				</F>
        				.
        				.
        				.
        			</Child_Fs>
        		</F>
        		<F id="2">
        			...
        		</F>
        		<F id="3">
        			...
        		</F>
        	    <F id="4">
        			...
        		</F>
        		.
        		.
        		.
        		<G/>
        		<H/>
        		<I/>
        	</B>
        	<C/>
        	<D/>
        	<E/>
        </A>

    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:

    Code:
        <A>
        	<B>
        		<F id="1">
        			<J/>
        			<K/>
        			<Child_Fs>
        				<F id="1.1">
        					<J/>
        					<K/>
        					<Child_Fs>
        					</Child_Fs>
        				</F>
        				<F id="1.1.1">
        					<J/>
        					<K/>
        					<Child_Fs>
        					</Child_Fs>
        				</F>
        				...
        				<F id="1.1.2">
        					...
        				</F>
        				<F id="1.1.3">
        					...
        				</F>
        				<F id="1.1.4">
        					...
        				</F>
        				.
        				.
        				.
        				<F id="1.2">
        					...
        				</F>
        				<F id="1.3">
        					...
        				</F>
        				<F id="1.4">
        					...
        				</F>
        				.
        				.
        				.
        			</Child_Fs>
        		</F>
        		<F id="2">
        			...
        		</F>
        		<F id="3">
        			...
        		</F>
        	    <F id="4">
        			...
        		</F>
        		.
        		.
        		.
        		<G/>
        		<H/>
        		<I/>
        	</B>
        	<C/>
        	<D/>
        	<E/>
        </A>
    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.
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    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.

    Comment

    Working...