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

Re: [xsl] XPath and recursion


Subject: Re: [xsl] XPath and recursion
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Thu, 23 Oct 2003 00:52:54 -0700 (PDT)

> I've got an xml-file, something like this:
> 
> <?xml version="1.0" encoding="UTF-8"?> 
> <document> 
> 	<node value1="DB Objects"> 
> 		<node value1="Andreas"> 
> 			<node value1="Peter"> 
> 				<node value1="Kempten"/> 
> 				<node value1="Ulperting"/> 
> 			</node> 
> 			<node value1="Muahhh"/> 
> 			<node value1="Christoph"> 
> 				<node value1="Ulperting"/> 
> 				<node value1="FrankyBoy"> 
> 					<node value1="Munich"/> 
> 				</node> 
> 			</node> 
> 		</node> 
> 		<node value1="Christoph"> 
> 			<node value1="Munich"/> 
> 			<node value1="FrankyBoy"> 
> 				<node value1="Kempten"/> 
> 			</node> 
> 		</node> 
> 		<node value1="Frank"/> 
> 	</node> 
> 	<node value1="Tables"> 
> 		<node value1="Andreas"> 
> 			<node value1="Peter"> 
> 				<node value1="Munich"/> 
> 				<node value1="Ulperting"/> 
> 			</node> 
> 		</node> 
> 		...
> 	</node> 
> </document> 
> 
> And I want to get an output like this after the xsl transformation:
> 
> aux1 = insFld(foldersTree, gFld("DB-Objects"))
>     aux2 = insFld(aux1, gFld("Andreas",))
> 		aux3 = insFld(aux2, gFld("Peter"))
> 			aux4 = insFld(aux3, gFld("Kempten"))
> 			aux4 = insFld(aux3, gFld("Ulperting"))
>   		aux3 = insFld(aux2, gFld("Muahhh"))
> 		aux3 = insFld(aux2, gFld("Christoph"))
> 			aux4 = insFld(aux3, gFld("FrankyBoy"))
> 				aux5 = insFld(aux4, gFld("Munich"))
> 			        aux5 = insFld(aux4, gFld("FrankyBoy"))
> 					aux6 = insFld(aux5, gFld
> ("Kempten"))
> 					aux6 = insFld(aux5, gFld
> ("Kempten"))
> 	aux2 = insFld(aux1, gFld("Christoph"))
> 	aux2 = insFld(aux1, gFld("Frank"))
> 		aux3 = insFld(aux2, gFld("Peter"))
> 			aux4 = insFld(aux3, gFld("Ulperting"))
>   		aux3 = insFld(aux2, gFld("Muahhh"))
> 		aux3 = insFld(aux2, gFld("Christoph"))
> 			aux4 = insFld(aux3, gFld("Ulperting"))
> 			aux4 = insFld(aux3, gFld("FrankyBoy"))
> 				aux5 = insFld(aux4, gFld("Munich"))
> aux1 = insFld(foldersTree, gFld("Tables"))
>     aux2 = insFld(aux1, gFld("Andreas",))
> 		aux3 = insFld(aux2, gFld("Peter"))
> 			aux4 = insFld(aux3, gFld("Kempten"))
> 		aux3 = insFld(aux2, gFld("Christoph"))
> 			aux4 = insFld(aux3, gFld("Munich"))
> 			aux4 = insFld(aux3, gFld("Ulperting"))
> 
> The idea behind that:
> if /document has at least a child, write:
>    aux1 = insFld(foldersTreeview, gFld( "<xsl:value-of 
> select="@value1"/> "));
> if the child got at least another child,write:
>    aux2 = insFld(aux1, gFld("<xsl:value-of select="@value1"/> ");
> if the child of the child got at least another child,write:
>    aux3 = insFld(aux2, gFld("<xsl:value-of select="@value1"/> ");
> and so on....
> 
> I tried to solve this problem for a long time now, but falways failed. 
> Also the numbering of "aux", the deeper i get into the node-tree, is 
> impossibly for me. Have no clue how I would do that.

This transformation:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
 <xsl:output method="text"/>
  
  <xsl:variable name="vQ">"</xsl:variable>
  
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="node">
    <xsl:variable name="vDepth" select="count(ancestor::node) + 1"/>
    <xsl:value-of 
    select="concat('aux', $vDepth, ' = insFld(')"/>

    <xsl:choose>
      <xsl:when test="$vDepth = 1">foldersTree</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat('aux', $vDepth - 1)"/>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:value-of select="concat(', gFld(', $vQ, @value1, $vQ, '))')"/>
    
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

when applied on your source.xml:

<document> 
	<node value1="DB Objects"> 
		<node value1="Andreas"> 
			<node value1="Peter"> 
				<node value1="Kempten"/> 
				<node value1="Ulperting"/> 
			</node> 
			<node value1="Muahhh"/> 
			<node value1="Christoph"> 
				<node value1="Ulperting"/> 
				<node value1="FrankyBoy"> 
					<node value1="Munich"/> 
				</node> 
			</node> 
		</node> 
		<node value1="Christoph"> 
			<node value1="Munich"/> 
			<node value1="FrankyBoy"> 
				<node value1="Kempten"/> 
			</node> 
		</node> 
		<node value1="Frank"/> 
	</node> 
	<node value1="Tables"> 
		<node value1="Andreas"> 
			<node value1="Peter"> 
				<node value1="Munich"/> 
				<node value1="Ulperting"/> 
			</node> 
		</node> 
	</node> 
</document> 

produces the wanted result:


 
	aux1 = insFld(foldersTree, gFld("DB Objects")) 
		aux2 = insFld(aux1, gFld("Andreas")) 
			aux3 = insFld(aux2, gFld("Peter")) 
				aux4 = insFld(aux3, gFld("Kempten")) 
				aux4 = insFld(aux3, gFld("Ulperting")) 
			 
			aux3 = insFld(aux2, gFld("Muahhh")) 
			aux3 = insFld(aux2, gFld("Christoph")) 
				aux4 = insFld(aux3, gFld("Ulperting")) 
				aux4 = insFld(aux3, gFld("FrankyBoy")) 
					aux5 = insFld(aux4, gFld("Munich")) 
				 
			 
		 
		aux2 = insFld(aux1, gFld("Christoph")) 
			aux3 = insFld(aux2, gFld("Munich")) 
			aux3 = insFld(aux2, gFld("FrankyBoy")) 
				aux4 = insFld(aux3, gFld("Kempten")) 
			 
		 
		aux2 = insFld(aux1, gFld("Frank")) 
	 
	aux1 = insFld(foldersTree, gFld("Tables")) 
		aux2 = insFld(aux1, gFld("Andreas")) 
			aux3 = insFld(aux2, gFld("Peter")) 
				aux4 = insFld(aux3, gFld("Munich")) 
				aux4 = insFld(aux3, gFld("Ulperting")) 





 


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords
xsl