ColdFusion XPath Namespacing Gotchas

If you find yourself with some XML, trying to do an XPath query using the ColdFusion XMLSearch() on it, and the results are always empty, make sure you are not getting caught out by XML namespacing.

For example, take this simple XML:

<MyNode xmlns="">
	<SubNode active="1">Some Data</SubNode>
	<SubNode active="1">More Data</SubNode>

If do a search for the SubNode as follows:

<cfset arr = xmlSearch(xml, "/MyNode/SubNode") />

You will get no results. The reason is the xmlns attribute of the document. Until recently, I used to delete the xmlns attribute from the XML before parsing it as that will work. Not very elegant however. A better approach is to use the following adjusted XPath:

<cfset arr = xmlSearch(xml, "/:MyNode/:SubNode") />

That version ignores the namespacing. I hope that’s helpful. There’s some more info here.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

One Response to ColdFusion XPath Namespacing Gotchas

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s