Link to home
Start Free TrialLog in
Avatar of Dan Schimo
Dan SchimoFlag for United States of America

asked on

ColdFusion to convert a XML in the Structure of Arrays and avoiding the Node levenschtein-dist =0

User generated image
My code is attached which is creating a struct of arrays as the

Related Question

I would like to Avoid the Node which has a  levenschtein-dist =0.

--Dan
<cfset terms = xmlSearch(xmlfile, "/suggestions/term")>
					
					
					<cfset results  = structNew()>
					<cfloop array="#terms#" index="term">
					      <cfset key = term.XmlAttributes.original >
					      <cfset results[key] = []>
					      <cfloop array="#term.xmlChildren#" index="child">
					            <cfif child.xmlName eq "suggestion">
					                  <cfset arrayAppend(results[key], child.xmlText)>
					            </cfif>
					      </cfloop>
					</cfloop>

Open in new window

Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Can you please post the xml document also for testing?
Check this:
<cfset terms = xmlSearch(xmlfile, "/suggestions/term/suggestion[@levenstein-dist!='0']")>

Open in new window

Avatar of Dan Schimo

ASKER

sorry it returns empty struct
<suggestions elapsed-time="PT0.010333S"><term original="dissorder"><suggestion levenschtein-dist="1">Disorder</suggestion><suggestion levenschtein-dist="1">disorder</suggestion><suggestion levenschtein-dist="2">Disorders</suggestion><suggestion levenschtein-dist="2">disorders</suggestion><suggestion levenschtein-dist="2">distorter</suggestion></term><term original="sleep"><suggestion levenschtein-dist="0">Sleep</suggestion><suggestion levenschtein-dist="0">sleep</suggestion><suggestion levenschtein-dist="1">asleep</suggestion><suggestion levenschtein-dist="1">seep</suggestion><suggestion levenschtein-dist="1">sleeps</suggestion></term></suggestions>

Open in new window


its a webservice which is called by cfhttp

the output you are seeing is the <cfdump var="#cfhttp.filecontent#">

-Dan
Show your xml data then I can check for you.
Test also the short notation:
<cfset terms = xmlSearch(xmlfile, "//suggestion[@levenstein-dist!='0']")>

Open in new window

Didn't work either, I posted the XML Data....
OK, I see that Ihave misspelled your lewenSCHtein attribute. Check that.

Zvonko,

I corrected the misspelled levenschtein-dist="1"
and I am getting the array of structs which is not I am looking for...

 I am trying to construct a Struct of Arrays from XML Please refer this was my previous questions.

where a Struct of arrays are constructed.
 I would like to exclude the NODE (struct) where the any of its attributes levenschtein-dist="0"
Extend this one line:
<cfif child.xmlName eq "suggestion" and child.getAttribute("levenschtein-dist") NEQ "0" >

Open in new window

I put the extension and I have got a scrn shot before and after adding the code . also the thing I need to accomplish. sorry if i am unable to express my requirement... User generated image
<cfset terms = xmlSearch(xmlfile, "/suggestions/term")>
					
				<cfset results  = structNew()>
					<cfloop array="#terms#" index="term">
												
					      <cfset key = term.XmlAttributes.original >
						    
					      <cfset results[key] = []>
						    
					      <cfloop array="#term.xmlChildren#" index="child">
					            <cfif child.xmlName eq "suggestion" and child.getAttribute("levenschtein-dist") NEQ "0">
					                  <cfset arrayAppend(results[key], child.xmlText)>
					            </cfif>
					      </cfloop>
					      
					</cfloop>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Zvonko , that did it !