New here? Then you may want to subscribe to my rss feed. :)
May 14th, 2007 | Posted in .Net, Development | 1 Comment
Recently, I needed to transform an XML feed using C#. I wrote the XSLT script and it all worked ok.
One of the parts of the page took a TITLE and passed it to a Javascript function which then passed it to a flash file. This one particular record had a single quote, which as all developers know, will break everything within a 3 mile radius.
Trawling the MSDN rainforest led me to discover that you could pass parameters from the C# class which was carrying out the transformation, so I wondered if you could call methods in C# from the XSLT and behold you could.
The key was the AddExtensionObject method.
// Load XML document
XPathDocument myXPathDoc = new XPathDocument("c:/myfile.xml") ;
// Create trans object
XslCompiledTransform myXslTrans = new XslCompiledTransform() ;
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
//load the Xsl
myXslTrans.Load("c:/myfile.xslt") ;
// Create and add XSLT extension
XsltArgumentList argsList = new XsltArgumentList();
myHelperObj myObj = new myHelperObj();
argsList.AddExtensionObject("urn:myObj", myObj);
myXslTrans.Transform(myXPathDoc,argsList, sw);
-
// Load XML document
-
XPathDocument myXPathDoc =
new XPathDocument
("c:/myfile.xml") ;
-
-
// Create trans object
-
XslCompiledTransform myXslTrans =
new XslCompiledTransform
() ;
-
-
StringBuilder sb =
new StringBuilder
();
-
StringWriter sw =
new StringWriter
(sb
);
-
-
//load the Xsl
-
myXslTrans.Load("c:/myfile.xslt") ;
-
-
// Create and add XSLT extension
-
XsltArgumentList argsList =
new XsltArgumentList
();
-
myHelperObj myObj =
new myHelperObj
();
-
argsList.AddExtensionObject("urn:myObj", myObj);
-
myXslTrans.Transform(myXPathDoc,argsList, sw);
Then as part of your xsl stylesheet declaration add:
-
<xsl :stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-
xmlns:myObj="urn:myObj"></xsl>
Whereas before this would throw an error:
-
<a href="#" onclick="return callExternalInterface(’{title}’);"></a>
By adding the XSLT extension object I could now do this:
-
<a href="#" onclick="return callExternalInterface(’{myObj:makeJsSafe(title)}’);"></a>
April 19th, 2007 | Posted in .Net, Development |
A request came In from a client. I was asked to add search functionality to their existing site. Initially the plan was to write long, complicated sql queries with enough joins and sub-selects to make your nose bleed.
Before starting, I wondered if there were any alternative solutions. The site itself is a .Net site with a flash front end and a MySQL database. So naturally, i needed something that i could interface with using C# and MySQL.
Behold, a short google later I came across a free, open source and bloody good search engine called Apache Lucene.
“Apache Lucene is a high-performance, full-featured text search engine library written entirely in Java. It is a technology suitable for nearly any application that requires full-text search, especially cross-platform.”
Fortunately, some very nice and clever people have ported it over to .Net (DotLucene) so I now had a search engine i could use with C# and MySQL.
To top it all, the nice people at DotLucene have also put together a simple tutorial to get you started.
The system has its drawbacks, most notably is the inability to do a wildcard search from the first character. e.g. searching for *ordpress would throw an exception. So you will have to implement workarounds to this and other situations. There is API documentation and a fairly active mailing list to provide help.
I am still relatively new to Lucene so as this project develops i will post any useful nuggets of information to help you along.