We help IT Professionals succeed at work.

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

Dan Schimo
Dan Schimo used Ask the Experts™
on
levenschtein-dist
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ZvonkoSystems architect
Top Expert 2006

Commented:
Can you please post the xml document also for testing?
ZvonkoSystems architect
Top Expert 2006

Commented:
Check this:
<cfset terms = xmlSearch(xmlfile, "/suggestions/term/suggestion[@levenstein-dist!='0']")>

Open in new window

Dan SchimoAnalyst

Author

Commented:
sorry it returns empty struct
Dan SchimoAnalyst

Author

Commented:
<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
ZvonkoSystems architect
Top Expert 2006

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

Dan SchimoAnalyst

Author

Commented:
Didn't work either, I posted the XML Data....
ZvonkoSystems architect
Top Expert 2006

Commented:
OK, I see that Ihave misspelled your lewenSCHtein attribute. Check that.

Dan SchimoAnalyst

Author

Commented:
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"
ZvonkoSystems architect
Top Expert 2006

Commented:
Extend this one line:
<cfif child.xmlName eq "suggestion" and child.getAttribute("levenschtein-dist") NEQ "0" >

Open in new window

Dan SchimoAnalyst

Author

Commented:
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... Avoid levenstiten dist
<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

Systems architect
Top Expert 2006
Commented:
You want only those struct keys that do not have childs with zero, right?
Take this:
<cfset results  = structNew()>
          <cfloop array="#terms#" index="term">
            <cfset zeroLev = xmlSearch(term, "*[@levenschtein-dist='0']")>
            <cfif ArrayLen(zeroLev) EQ 0 >
                <cfset key = term.XmlAttributes.original >
                <cfset results[key] = []>
                <cfloop array="#term.xmlChildren#" index="child">
                      <cfset arrayAppend(results[key], child.xmlText)>
                </cfloop>
            </cfif>
          </cfloop>

Open in new window

Dan SchimoAnalyst

Author

Commented:
Thanks Zvonko , that did it !