Posts Tagged ‘xPath’

Case-sensitivity with Coldfusion and XML

Sunday, November 14th, 2010

This is really just a note about intermingling a case-insensitive language like Coldfusion with a case-sensitive language like XML. Sometimes I get so used to not having to pay attention to case when something goes wrong, that figuring out a simple issue that does have to do with case-sensitivity really throws me for a loop. The issue I ran into involved using the xmlSearch() function.

I used this function to search on an XML doc and the search kept returning an empty array. For a moment I thought for sure my xPath was incorrect, but further research showed it to be accurate. For some reason I just couldn’t get to a certain node. Of course, the issue turned out to be that I simply didn’t case my xPath correctly. For example my xPath may have looked like “//root/mynode”. The actual node I was trying to get in the XML doc was myNode.

Because I deal so much with Coldfusion I feel my immediate reaction to an xPath issue was that it was either wrong, or there was a misspelling. It really took a few minutes to realize that the path was correct as well as the spelling. What was incorrect was the case. Correcting the case, of course, resolved the issue.

So something to keep in mind, that while working within the world of Coldfusion case is meaningful only in terms of readability and following standards. However, when using Coldfusion with other (possibly case-sensitive) languages like XML we have to switch our brains into a different mode and start troubleshooting with character case in mind.

Using an XPath wildcard

Tuesday, September 28th, 2010

In the case that you need to use XPath to retrieve an XML node, but are missing some piece of information you can use the ‘*’ as a wildcard in the XPath expression. Let’s take for example an application where a request for a certain type of house can be made and a set of XML representing that house and its details is returned. So let’s say the application requests a ‘Ranch House’. The XML returned looks like:

<House>
	<RanchHouse>
		<Floors>1</Floors>
		<Acres>8</Acres>
		<Basement>true</Basement>
	</RanchHouse>
</House>

And if we request a ‘Georgian House’ we would get:

<House>
	<GeorgianHouse>
		<Floors>3</Floors>
		<Acres>3</Acres>
		<Basement>false</Basement>
	</GeorgianHouse>
</House>

Notice that the child node of ‘House’ is different in both cases. While this may not be ideal XML it is valid and may be encountered. So we encounter this and we want to write a single XPath expression that will retrieve the number of floors for any house that is returned to us. In the expression where the the child node of ‘House’ is referenced we can use a wildcard to essentially bypass it and access its child nodes. If using the Coldfusion xmlSearch() function, our expression would look like:

<cfset variables.floors = xmlSearch(variables.myXmlObject, "/:House/*/:Floors") />

Use of the wildcard can go much further than this, but I do love how simple it is to skip over an xml node if its an unknown.