I want a regular expression matching function, but there is no such function in XPath 1.0.
I had no choice but to expand one myself, and after searching on the Internet, there was a good article.
http://www.microsoft.com/china/MSDN/library/data/xml/AddingCustomFunctionstoXpath.mspx?mfr=true
This article defines a split and a replace, but there is no match.
I had to expand it on the basis of it. After
carefully observing the code, I found that it is very simple to extend a function, just modify these paragraphs:
1: CustomContext.cs
// Function to resolve references to my custom functions.
public override IXsltContextFunction ResolveFunction(string prefix,
string name, XPathResultType[] ArgTypes)
{
XPathRegExtensionFunction func = null;
// Create an instance of appropriate extension function class.
switch(name)
{
case "Match":
//Usage
// myFunctions:Matches(string source, string Regex_pattern) returns Boolean
func = new XPathRegExtensionFunction("Match", 2, 2, new
XPathResultType[] {XPathResultType.String, XPathResultType.String}
, XPathResultType.Boolean );
break;
case "Split":
//Usage
// myFunctions:Split(string source, string Regex_pattern, int n) returns string
func = new XPathRegExtensionFunction("Split", 3, 3, new
XPathResultType[] {XPathResultType.String, XPathResultType.String,
XPathResultType.Number}, XPathResultType.String);
break;
case "Replace":
//Usage
// myFunctions:Replace(string source, string Regex_pattern, string replacement_string) returns string
func = new XPathRegExtensionFunction("Replace", 3, 3, new
XPathResultType[] {XPathResultType.String, XPathResultType.String,
XPathResultType.String}, XPathResultType.String);
break;
}
return func;
}
2: XPathRegExtensionFunction.cs
// This method is invoked at run time to execute the user defined function.
public object Invoke(XsltContext xsltContext, object[] args,
XPathNavigator docContext)
{
Regex r;
string str = null;
// The two custom XPath extension functions
switch(m_FunctionName)
{
case "Match":
r = new Regex(args[1].ToString());
MatchCollection m = r.Matches(args[0].ToString());
if (m.Count == 0)
{
return false;
}
else
{
return true;
}
break;
case "Split":
r = new Regex(args[1].ToString());
string[] s1 = r.Split(args[0].ToString());
int n = Convert.ToInt32(args[2]);
if (s1.Length < n)
str = "";
else
str = s1[n - 1];
break;
case "Replace":
r = new Regex(args[1].ToString());
string s2 = r.Replace(args[0].ToString(), args[2].ToString());
str = s2;
break;
}
return (object)str;
}
Another file, XPathExtensionVariable.cs, actually has nothing to do with function expansion. It is used to set parameters.
After these two files have been modified, you can call:
query = navigator.Compile("xdUtil:Match(9,'\d')");
CustomContext cntxt = new CustomContext();
// Add a namespace definition for myFunctions prefix.
cntxt.AddNamespace("xdUtil", " http://myXPathExtensionFunctions ");
query.SetContext(cntxt);
Evaluate(query, navigator);
Of course, it would be great if it supports XPath2.0. These functions of XPath2.0 are all built-in supported, but unfortunately it does not seem to be supported yet.
The full code is here: