Posts Tagged XML

Sorting XML with XSLT and ColdFusion

I came across a requirement today, where I had to sort a XML file by one of the attributes in one of its child nodes. I did some searching and came across these excellent posts by Ben Nadel.  Obviously, that gave me some headstart and it was clear that XSLT was the way to go.  I have not used a lot of XSLT in the past so my knowledge is a bit limited in this area.

After reading a couple of articles on the subject and understanding some basic stuff – here is what I came up with. Read the rest of this entry »

Tags: , ,

Removing NameSpaces from XML using XSLT and ColdFusion

If you are using ColdFusion XPath functions to search/parse XML strings, you will probably have come across this one at some point.  Not all, but most of the XML files have namespaces in them e.g. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”. If you use Xpath on a string that contains namespaces, chances are that you will get back the namespaces as well and sometimes you dont want these at all. You just want to deal with the real data. Read the rest of this entry »

Tags: , ,

The Processing Instruction Target Matching “[xX][mM][lL]” is Not Allowed

This post is just a little tip that I think can take a lot of time in trying to solve.

I was building a variable , using cfsavecontent, to process later on and there was absolutely nothing wrong with the code according to me, as below :- Read the rest of this entry »

Tags: ,

Changing MXML string labels via XML

I personally dont like hardcoding things like texts and labels inside any compiled code, reason being , when it needs to be changed for whatever reason, I have to go through the pain of loading the project and making the change and then compiling it again and deploying it again , all for one little change. Its always better to save as many as possible config’s in a separate file (XML being the more appropriate choice). Read the rest of this entry »

Tags: ,

Extracting Links using Xpath

Extracting links from a piece of HTML code is a very common task and any programmer would have come across this requirement at some point. I have always used regular expressions to achieve this and it has always worked for me, no complaints there. However, I was just curious to find some other way to do it.

Here is what I did.

used CFHTTP to get the HTML code.

<cfhttp url="http://www.anujgakhar.com" method="get" />
<cfset myVar = cfhttp.FileContent>

Put it all in a CF XML Object

<cfxml variable="myXml">
<Cfoutput>#ToString(myVar)#</Cfoutput>
</cfxml>

Got all links using Xpath

<cfset allLinks = XmlSearch(myXml, "//*[local-name()='a']")>

Put everything inside a CF query.

<cfset mylinks = QueryNew('title,link')>
<cfloop from = "1" to="#ArrayLen(allLinks)#" index="this">
<cfset queryAddRow(myLinks,1)>
<cfset querySetCell(myLinks, "title", allLinks[this].XmlText) />
<cfset querySetCell(myLinks, "link", allLinks[this].XmlAttributes.href) />
</cfloop>
<cfdump var = "#mylinks#">

And it works! I was delighted to see the results. However, the only condition is that the HTML should be valid HTML or XHTML I must say. Well, nothing special I know but atleast I found out which people dont have valid HTML on their sites! ha!

Tags: ,

Coldfusion XML to Struct

XML parsing in Coldfusion has improved a lot in the last few versions but converting a complex XML object to a Coldfusion structure is still a struggle I beleive. There are a few custom tags out there for doing this but the ones that I have used or know are all only for simple structures, not nested or subnested structures. So I use this little function I and one of my other friends worked on and it works like a charm. Read the rest of this entry »

Tags: ,

Using Xpath with Coldfusion

I find Xpath very powerul for searching through a XML document. I have used it extensively in some of my projects and it might be worth it to post some of the examples here. For this example, I have used the books.xml sample xml file which can be found here.

Following are some examples of what we can do with Xpath in Coldfusion. I have tried to keep the variable names self-explanatory so it should easy to figure out which statement is doing what. Read the rest of this entry »

Tags: , ,

WP SlimStat