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="http://www.mydomain.com/MyNode">
	<SubNode active="1">Some Data</SubNode>
	<SubNode active="1">More Data</SubNode>
</MyNode>

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.

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s