• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

Coldfusion searching xml and displayiong results using cfdump

I have a popup glossary that uses an xml file for the glossary. When a user clicks any word on  apge and if the glossary word exists, the appropiate node in the xml should get generated. I'm having issues passing the url into the xml and outputting the result. Also what should I do if the result isn't shown?

See code below:
javascript to open the window:

<!--

    var theSelection = "";
      var isMacIE;

      isMacIE = ((navigator.appVersion.indexOf("MSIE") != -1) && (navigator.appVersion.indexOf("Windows") == -1)) ? true : false;     // true if we're on ie on mac.

    // Handle double-click from same frame
    function asaDblClickLookup()
    {
        setTimeout("openGlossaryWindow('')", 0);
    }

    // Record highlighted selection on mouseup (on Mac on IE do not delete it if the selection is empty -- IE bug on Mac when a button is clicked, the selection is lost.)
    function asaMouseUpGetSelection()
    {
        var tempSelection = asaGetSelfSelection();
        if ( !isMacIE || tempSelection != "" )
              theSelection = tempSelection;
    }

    // Get highlighted selection
    function asaGetSelfSelection()
    {
        if (document.getSelection)
        {
              return document.getSelection();
        }
        else if (document.selection.createRange)
        {
            var range = document.selection.createRange();
            return range.text;
        }
        return "";
    }

    // Open glossary pane
    function openGlossaryWindow(glossary)
    {
        var term = (asaGetSelfSelection().toString() != "") ? asaGetSelfSelection().toString() : (theSelection != "") ? theSelection : "";
        window.open("/glossaryresults.jsp?glossaryterm=" + escape(term) + "&glossary=" + glossary, "lookUpWin", "width=600,height=500,scrollbars=yes");
          //window.open("/glossaryresults.cfm?glossaryterm=" + escape(term), "lookUpWin", "width=600,height=500,scrollbars=yes");
          //alert("/glossaryresults.cfm?glossaryterm=" + escape(term));
      }

    //This will check the document for selections, and if there are one, lookup the last selected item (if still selected or if IE on Mac)
    function asaDoSearch()
    {
          var term = (asaGetSelfSelection().toString() != "") ? asaGetSelfSelection().toString() : (theSelection != "") ? theSelection : "";
        if ( term != "" )
                window.open("/glossaryresults.cfm?glossaryterm=" + escape(term), "lookUpWin", "width=600,height=500,scrollbars=yes");
            else
                window.open("/glossaryresults.cfm?newSearch=true", "lookUpWin", "width=600,height=500,scrollbars=yes");
    }

    //This will look up the word handed to us.
    function asaDoLookup(term)
    {
          window.open("/glossaryresults.cfm?glossaryterm=" + escape(term), "lookUpWin", "width=600,height=500,scrollbars=yes");
    }

    //This will look up the word handed to us.
    function asaOpenDict(term)
    {
          window.open("/glossaryresults.cfm?newSearch=true", "lookUpWin", "width=600,height=500,scrollbars=yes");
    }

      

if (window.Event)
      document.captureEvents(Event.DBLCLICK);
document.ondblclick = asaDblClickLookup;
document.onmouseup = asaMouseUpGetSelection;

// -->

Now that I have the word, I pass to glossaryresults.jsp that rwads the xml file:

<cfsilent>
<cffile action="read" file="C:\inetpub\wwwroot\glossary.xml" variable="xmlDoc">
<cfset newdoc = XmlParse(xmlDoc)>

<!--- The following search will retun all the glossaryitem nodes where the category is "a"  --->
<!---<cfset SearchByCategory = xmlSearch(MyXMLDoc,'//glossaryitem [@category="#url.category#"]')>
<cfdump var="#SearchByCategory#">--->

<!--- this search will return all the glossaryitem nodes where the term node has the value "Academic Year" --->
<cfset SearchByTerm = xmlSearch(newdoc,'//glossaryitem [term="#url.glossaryterm#"]')>
<cfdump var="#SearchByTerm#">
</cfsilent>
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
      <title>Glossary Terms</title>
      <script>
      function closeDep(){
            if (win && win.open && !win.closed)
                  win.close();
            if (vid && vid.open && !vid.closed)
                  vid.close();
      }


      function resizeMe(resize) {
            if (resize == 2) {
                  self.resizeTo(600,500);
            }
      }
            
      </script>
</head>
<body text="#000000" link="#0000FF" bgcolor="#FFFFFF" vlink="#000000" alink="#FF0000" leftmargin=0 topmargin=0 marginheight=0 marginwidth=0 onLoad="resizeMe('2');">
<cfoutput>
#SearchByTerm#</cfoutput>


</body>
</html>

I get the error:

Error Occurred While Processing Request  
Complex object types cannot be converted to simple values.  
The expression has requested a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values.
The most likely cause of the error is that you are trying to use a complex value as a simple one. For example, you might be trying to use a query variable in a <CFIF> tag. This was possible in ColdFusion 2.0 but creates an error in later versions.
 
 
The error occurred in C:\Inetpub\wwwroot\glossaryresults.cfm: line 36
 
34 : <body text="#000000" link="#0000FF" bgcolor="#FFFFFF" vlink="#000000" alink="#FF0000" leftmargin=0 topmargin=0 marginheight=0 marginwidth=0 onLoad="resizeMe('2');">
35 : <cfoutput>
36 : #SearchByTerm#</cfoutput>
37 :
38 :

 
 
WHat am I doing wrong. Any ideas. I want to know what do do as well if there are no results. How would I include if the contains function in the lookup?
0
asaworker
Asked:
asaworker
  • 6
  • 5
  • 2
1 Solution
 
Scott BennettCommented:
are your url variables encoded? if so you will want to use urldecode() like

<cfset SearchByTerm = xmlSearch(newdoc,'//glossaryitem [term="#urldecode(url.glossaryterm)#"]')>
0
 
73SpyderCommented:
Have you tried using wddx to pass the xml across?
 on sending page:
<cfwddx action = "cfml2wddx" input = #variables.xml# output = "var2pass">

on receving page:

<cfwddx action = "wddx2cfml" input = #url.var2pass# output = "variables.xmlagain">

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
asaworkerAuthor Commented:
Sbennet how do you use the the contains feature in the:
<cfset SearchByTerm = xmlSearch(newdoc,'//glossaryitem [term="#urldecode(url.glossaryterm)#"]')>

I am getting results now but they are an empty array with the green box.
0
 
Scott BennettCommented:
The results might be case sensitive. Try making sure the case is exactly the same as the value of the <term> node you are searching for.

If it is case sensitive, then In your directory XML make it so that the values of the term node are always upper case. then use:

<cfset SearchByTerm = xmlSearch(newdoc,'//glossaryitem [term="#ucase(trim(urldecode(url.glossaryterm)))#"]')>

You can also do the same thing in lower case if you want just use the lcase() function instead of the ucase().
0
 
Scott BennettCommented:
the "contains" syntax would be like:

<cfset SearchByTerm = xmlSearch(MyXMLDoc,'//glossaryitem [contains(term,"Academic")]')>
0
 
asaworkerAuthor Commented:
It looks like my javascript is adding some whiteapce when i double click a word. In the statement abovge how do I remove that space at end and i'm still geting blank results
0
 
Scott BennettCommented:

Use trim to remove the extra spaces:

<cfset SearchByTerm = xmlSearch(MyXMLDoc,'//glossaryitem [contains(term,"#trim(urldecode(url.glossaryterm))#")]')>

I also did some testing and the search is case sensitive so you will need to make sure the values of the <term> nodes are the same case as the search string you are using like I described earlier.
0
 
asaworkerAuthor Commented:
That will be a problem since we can't guarantee the word they click on is in the right format. If we do put all the words in the glossary in Uppercase, I could add another node that has the correct way it should be displayed. example xml below:

<?xml version="1.0" encoding="UTF-8"?>
                              <glossary>
                                    <glossaryitem category="a">
                                          <anchorname>academic</anchorname>
      <searchterm>ACADEMIC YEAR</searchterm>                                    <term>Academic Year</term>
                                          <definition>A period of time defined for credit hour schools as a minimum of 30 weeks instructional time in which a full time student is expected to complete at least 24 semester or trimester hours, or 36 quarter hours. For a clock hour school the period of time is a minimuim of 26 weeks in which a full time student is expected to complete at least 900 clock hours.</definition>
                                    </glossaryitem>

Would that fix the problem?
0
 
Scott BennettCommented:
yes that would be perfect, then you would just do:

<cfset SearchByTerm = xmlSearch(MyXMLDoc,'//glossaryitem [contains(searchterm,"#ucase(trim(urldecode(url.glossaryterm)))#")]')>
0
 
Scott BennettCommented:
you can also use "starts-with" instead of "contains" which may or may not be better depending on your needs

<cfset SearchByTerm = xmlSearch(MyXMLDoc,'//glossaryitem [starts-with(searchterm,"#ucase(trim(urldecode(url.glossaryterm)))#")]')>

that will only return results that start with the search term.
0
 
asaworkerAuthor Commented:
That's good to know, but the term selected should be anywhere in the glossary node
0
 
asaworkerAuthor Commented:
Ok new edit. Now that I have the results in a tree view of the nodes, how do I extract them into html.. using xsl or some other cf way?
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now