House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Get text value from xmlsearch()

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Christophe Maso
12/07/2011 04:09 PM

Is there any way to get the "09-09-2009" string using xmlSearch() for the below xml? <employee>    <startDate>09-09-2009</startDate> </employee> I've been doing something like this, which is a real pain: arrDate = xmlSearch(xml, "//employee/startDate"); strDate = arrDate[1].XmlText; It seems that xmlSearch() must always return an array and is unable to return a string, which makes sense, but using the above code has gotten old, real fast.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Dominic Watson
12/07/2011 04:23 PM

It's getting old in the tooth and could do with some JavaLoader love, but this project can help with that sorta thing: http://betterxml.riaforge.org Dominic On 7 December 2011 21:09, Christophe Maso <zumiel@hotmail.com> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
12/07/2011 04:56 PM

You can use functions to get values: <cfxml variable="test"> <employee>   <startDate>09-09-2009</startDate> </employee> </cfxml> <cfset r = xmlSearch(test, "string(//employee/startDate)")> <cfdump var="#r#"> Docs: http://www.w3schools.com/xpath/xpath_functions.asp#string ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
12/07/2011 05:03 PM

Actually, you want to limit yourself to these - since CF9 is still xpath1 (Zeus is adding xpath2) http://en.wikipedia.org/wiki/XPath_1.0 ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Judah McAuley
12/07/2011 05:07 PM

Yay for XPath 2, that's good news.  Just today I was wrestling with XSLT that needed a Replace function which XPath 2 supports but XPath 1 (for some reason) does not. What kind of language doesn't have a Replace function? Cheers, Judah ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Judah McAuley
12/07/2011 05:06 PM

But what happens if there is more than one node that matches your search?  Searches expect to return one or more results, which is why it returns an array. Trying to turn what may be a complex result into a simple string with no logic seems like an unstable approach, unless I'm misunderstanding the question (which happens plenty). Instead, I'd write a UDF called FirstXMLNodeText or some some such that you pass an XPath value to along with an xml doc and then have it return the XmlText of the first array result returned. What you are doing with the array is correct, in my opinion. If it is too much typing and you are doing it a lot, build a function to do it. Cheers, Judah ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
12/07/2011 05:24 PM

The poster asked about getting a text value, so obviously he was using a xpath search that matched one value. So... don't do it on ones that would return multiple. You can use functions on multiple things though. For example: <cfxml variable="test"> <employee>   <salary>200</salary>   <salary>100</salary> </employee> </cfxml> <cfset r = xmlSearch(test, "sum(//employee/salary)")> <cfdump var="#r#"> ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Judah McAuley
12/07/2011 05:31 PM

Sure, some functions work on multiple nodes and they are awesome for all sorts of things. I just hesitate to use a function that will blow up if more than one node is returned when you can write a function that will always return the first node and therefore not blow up. I've just had two many times where I've written code that assumes that a query/search/whatever will only return one record and it works fine with all my test cases (because there is only supposed to be one record so that's what I put in my test case) and then lo and behold, someone inserts a second matching record and things start throwing errors because of that assumption I made. Cheers, Judah ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
12/07/2011 05:38 PM

I don't know man - if you are parsing xml, you are making some assumptions about the structure. The functions are there just for that reason. Sometimes you need the string value of a node. That's it.It's no more safe/dangerous than any other xpath function. ----- Excess quoted text cut - see Original Post for more -----


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

May 25, 2013

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31   

Designer, Developer and mobile workflow conference