Possible bug while implementing recursion
Hey,
Either I am going complete nuts and not seing the most obvious mistake or there is really a bug, hope someone can help. I have a herirachical xml document that I would like to flatten out. I wrote a cfm code and it works perfectly, but when I put them logic in cfscript it does not work. From what I can tell, when recursion returns back, and continues on, the old value in the loop is overwritten (counter value is not what it should be when the state was saved and recursion took place). I don't know how else to explain this. Here is code in cfm and in cfscript, can you see something I am not?
function flattenXML works just fine, but flattenXML2 does not.
[code]
<cfset xmlfile = "/xDocs/TAXONOMY_XMLDOC_131.xml" />
<cfset myDoc = xmlParse(xmlfile) />
<cfset theRootElement = myDoc.XmlRoot>
<cfdump var="#theRootElement.XMLChildren[1]#"/>
<cfset st = flatternXML2(theRootElement, "", structNew())/>
<cfdump var="#st#"/>
<cffunction name="flatternXML" access="private" returntype="struct">
<cfargument name="node" type="xml" required="true">
<cfargument name="str" type="string" required="true">
<cfargument name="lineage" type="struct" required="true" >
<cfset t_name="NONE"/>
<cfif structKeyExists(arguments.node.XmlAttributes, "NAME")>
<cfset t_name = arguments.node.XmlAttributes['NAME']/>
</cfif>
<cfif len(arguments.str)>
<cfset arguments.str &= "; " & left(arguments.node.XmlName, 1) & "_" & t_name/>
<cfelse>
<cfset arguments.str = left(arguments.node.XmlName, 1) & "_" & t_name/>
</cfif>
<cfif ArrayLen(arguments.node.XmlChildren) eq 0>
<!---<cfoutput>#arguments.str#</cfoutput></br>--->
<cfset hash_key = hash(arguments.str, "MD5")/>
<cfif not structKeyExists(arguments.lineage, hash_key)>
<cfset structInsert(arguments.lineage, hash_key, arguments.str)/>
<cfelse>
<cfoutput>duplicate lineage #arguments.str#<br/></cfoutput>
</cfif>
<cfreturn arguments.lineage/>
</cfif>
<cfloop from="1" to="#arraylen(arguments.node.XmlChildren)#" index="i">
<cfset arguments.lineage = flatternXML(arguments.node.XmlChildren, arguments.str, arguments.lineage)/>
</cfloop>
<cfreturn arguments.lineage/>
</cffunction>
<cffunction name="flatternXML2" access="private" returntype="struct">
<cfargument name="node" type="xml" required="true">
<cfargument name="str" type="string" required="true">
<cfargument name="lineage" type="struct" required="true" >
<cfscript>
//set name and prefix, of the current node
t_name = "NONE";
if (structKeyExists(arguments.node.XmlAttributes, "NAME"))
t_name = arguments.node.XmlAttributes['NAME'];
if (len(arguments.str))
arguments.str &= "; " & left(arguments.node.XmlName, 1) & "_" & t_name;
else
arguments.str = left(arguments.node.XmlName, 1) & "_" & t_name;
//recursion end condition
if (arraylen(arguments.node.XmlChildren) eq 0) {
writeoutput(arguments.str & "</br>");
hash_key = hash(arguments.str, "MD5");
if (not structKeyExists(arguments.lineage, hash_key))
structInsert(arguments.lineage, hash_key, arguments.str);
else
writeoutput("duplicate lineage: " & arguments.str & "<br/>");
return(arguments.lineage);
}
for(j=1; j lte arraylen(arguments.node.XmlChildren); j=j+1){
writeoutput("before " & j & "_" & arraylen(arguments.node.XmlChildren) & "<br/>");
arguments.lineage = flatternXML2(arguments.node.XmlChildren
writeoutput("after " & j & "_" & arraylen(arguments.node.XmlChildren) & "<br/>");
}
return(arguments.lineage);
</cfscript>
</cffunction>
[/code]
