Friday, April 08, 2011

XPath - fixed!

As you might have read in my previous post, I'm not too happy about the way the XPath works with namespaces.

To make things better I have created some regular expressions that will allow me to write XPaths the way I want to write them and still have them work with real XPath engines out there. It makes the XPath ignorant towards namespaces that are not defined in my XPath query. Namespaces that are defined in my XPath query will be handled like before.

xpath= "./" + xpath;
xpath = Regex.Replace(xpath,@"/(?[\w]+)\[","/*[local-name()=='${node}' and ");
xpath = Regex.Replace(xpath,@"/(?[\w]+)","/*[local-name()=='${node}']");

PS. I'll get back to the post and tidy the code up later


  1. You might want to add a few tests for the code to make sense.

  2. Hi Ronnie. what the code does is to convert a XPath like this:


    into something like this:

    ./*[local-name()='ArrayOfSObject' ]/*[local-name()='sObject' and sadf='234']/*[local-name()='Initial_distribution_channel__c']

    The result should be that the second ignores the namespaces

  3. That is; pretty neat!