Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / September 2007



Tip: Looking for answers? Try searching our database.

XMLParse mislead by XSI/XMLNS/schemaLocation?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Castaway77 - 05 Sep 2007 14:23 GMT
I seem to have a problem with XMLParse and the XSI bits of my XML in myt code
below.

When I take out these 3 lines

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.opentravel.org/OTA/2003/05"
xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05
OTA_DestActivityResRQ.xsd"

the code does what I expect it to do. I get a struct with four name value
pairs.

But when the lines are in (as they should be as per the OTA spec) the struct
is of zero length. It seems XMLParse is being confused in some way by these
lines.

I have no idea what the problem is? Any XML gurus out there got a clue? I
realize I could kludge a solution by blatting the offending lines from the
incoming XML before parsing it but I would like understand what in the 3 lines
is causing the problem.

Regards,

Sean

<cfsavecontent variable="OTA_DestActivityResRQ"><?xml version="1.0"
encoding="UTF-8"?>
<OTA_DestActivityResRQ
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.opentravel.org/OTA/2003/05"
xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05
OTA_DestActivityResRQ.xsd">
    <Stuff>
    </Stuff>
    <DestActivityReservation>
        <DestActivityItems>
            <Item ItemCode="1-2-1" Quantity="4" SelectedDate="2007-12-25"/>
            <Item ItemCode="3-2-2" Quantity="3" SelectedDate="2007-12-25"/>
            <Item ItemCode="4-2-3" Quantity="2" SelectedDate="2007-12-25"/>
            <Item ItemCode="6-3-4" Quantity="1" SelectedDate="2007-12-25"/>
        </DestActivityItems>
    </DestActivityReservation>
</OTA_DestActivityResRQ>
</cfsavecontent>

<cfset xmlinput = XMLParse(OTA_DestActivityResRQ)>
<cfset XSearch = XMLSearch(xmlinput,
"/OTA_DestActivityResRQ/DestActivityReservation/DestActivityItems/Item/@ItemCode
")>
<cfset LoopVal = ArrayLen(XSearch)>

<cfoutput>#LoopVal#</cfoutput>

<cfset booking_service = StructNew()>
<cfloop from="1" to="#LoopVal#" index="i">
    <cfset itemcode =
xmlinput.OTA_DestActivityResRQ.DestActivityReservation.DestActivityItems.Item[i]
.XmlAttributes["ItemCode"]>
    <cfset itemqty =
xmlinput.OTA_DestActivityResRQ.DestActivityReservation.DestActivityItems.Item[i]
.XmlAttributes["Quantity"]>
    <cfset temp = StructInsert(booking_service, itemcode, itemqty)>
</cfloop>

<cfdump var="#booking_service#">
Grizzly9279 - 05 Sep 2007 16:27 GMT
You've stumbled upon a long-standing "known issue" that has plauged CF's
xmlSearch function for a long time now.  Often times nested namespaces in XML
documents cause xmlSearch to fail (or not match).

To get around this, we often just use a quick regex replace statement to trim
out name spaces from the entire xml string (before calling xml-parse).

Example:
<cfset xmlResponse = reReplace( xmlResponse, '[ ]*xmlns=\"[^\"]*\"', "", "all"
)>
Castaway77 - 06 Sep 2007 03:00 GMT
Thanks for that Grizzly9279.

Problem solved.

Regards,

Sean
cf_eilloc - 06 Sep 2007 15:16 GMT
Removing all namespaces could have detrimental effects later on in your logic.  

Since there is more than one namespace in the XML document you supplied, you
must qualify all elements when performing an XMLSearch with their namespace,
even if it is the default.

Original:

"/OTA_DestActivityResRQ/DestActivityReservation/DestActivityItems/Item/@ItemCode
"

Modified:

"/:OTA_DestActivityResRQ/:DestActivityReservation/:DestActivityItems/:Item/@Item
Code"

Please note the colon in the example above to indicate that the element
searched upon is in the default namspace.

The code supplied now returns a structure with 4 keys as expected when
applying the modified XPath expression.
Grizzly9279 - 06 Sep 2007 15:54 GMT
A very good point cf_eilloc.  That approach works fine when you're dealing with
only one namespace (such was the case here), but when you encountered nested
namespaces (2 or more deep), you're pretty much screwed and are forced to strip
them all out if you want to search the document.

So in general practice, I prefer to simply strip out namespaces altogether
unless there is a specific need or purpose of including them in my search.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.