RE: [xsl] urgent..creating loops in output xml based on input xml values..

Subject: RE: [xsl] urgent..creating loops in output xml based on input xml values..
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 12 Sep 2005 10:28:40 +0100
You haven't grasped a key point about XSLT, which is that it creates the
result as a tree, not as textual XML. That means you need to think about
when to create a <Loop> node on the tree, not about when to write start tags
and end tags.

I describe the solution to this problem using XSLT 2.0 in my paper given at
XML 2004 (see especially page 9):

http://www.idealliance.org/proceedings/xml04/papers/111/mhk-paper.pdf

A solution using 1.0 is possible, but not if you're in a hurry - you need to
take the time to learn some new concepts. (I would be prepared to spend more
time explaining it if you hadn't said it was urgent.)

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: ma he [mailto:mahendranvm@xxxxxxxxxxx] 
> Sent: 12 September 2005 10:15
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] urgent..creating loops in output xml based on 
> input xml values..
> 
> Hi all,
> I am new to the whole xsl world and I have trouble
> creating an xsl. This is urgent and I have less time
> to do research. so plz help me with this asap.
> This is my requirement,
>  
> the input xml will be in this format,
>  
> <root>
>     <name>1</name>
>     <name>2</name>
>     <name>3</name>
>     <name>1</name>
>     <name>2</name>
>     <name>1</name>
> </root>
>  
> The possible values for name are only 1,2 and 3. I
> should supply an xsl which will be used to transform
> this xml into another xml in the following format,
>  
> <root>
>   <Loop>
>         <name>1</name>
>         <Loop>
>             <name>2</name>
>             <Loop>
>                 <name>3</name>
>             </Loop>
>         </Loop>
>     </Loop>
>     <Loop>
>         <name>1</name>
>         <Loop>
>             <name>2<name>
>         </Loop>
>     </Loop>
>     <Loop>
>         <name>1</name>
>   </Loop>  
> </root>
>  
> In the input xml, there will not be a 2 without 1
> preceding it. similarly there wont be a 3 without a 2
> preceding it. But there could be more than one 1 or 2
> or 3, one after the other. Here's another example,
>  
> <root>
>     <name>1</name>
>     <name>2</name>
>     <name>2</name>
> </root>
>  
> Here the output will be
> <root>
>     <Loop>
>         <name>1</name>
>         <Loop>
>             <name>2</name>
>         </Loop>
>         <Loop>
>             <name>2</name>
>         </Loop>
>     </Loop>
> </root>
>  
> I thought of using the following algorithm to
> determine when to open a loop element and when to
> close a loop,
>  
> declare variables L1 = no, L2 = no and L3 = no
> For each <root> in output xml
> {
>     If (name = 1)
>     {
>         If (L3 = yes) then in output xml, close loop
> "</Loop>
>         If (L2 = yes) then in output xml, close loop
> "</Loop>
>         If (L1 = yes) then in output xml, close loop
> "</Loop>
>         Open Loop in output xml "<Loop>"
>     }
>     If (name = 2)
>     {
>         If (L3 = yes) then in output xml, close loop
> "</Loop>
>         If (L2 = yes) then in output xml, close loop
> "</Loop>
>         Open Loop in output xml "<Loop>"
>     }
>     If (name = 3)
>     {
>         If (L3 = yes) then in output xml, close loop
> "</Loop>
>         Open Loop in output xml "<Loop>"
>     }
> }
>  
> But here, since variable values once assigned cannot
> be reassigned, I am not able to implement this algo.
> in xsl. Help me with some other logic  that will be
> able to achieve the same. (all I want here is to do
> the following, there will be an input xml file where
> all elements are in the same level. In the output
> file, I want these elements nested one inside another,
> based on their values.). Thanks in advance for ur
> help.
> -mahe
> 
> 
> 		
> __________________________________________________________ 
> Yahoo! India Matrimony: Find your partner now. Go to 
> http://yahoo.shaadi.com

Current Thread