For XPath custom functions (because XPath1.0 functions are very limited)

Source: Internet
Author: User
Tags functions query regular expression return split tostring xpath
Function

Want a matching function for a regular expression, but there is no XPath1.0 between the
Have to expand one by themselves, search the Internet a bit, there is a good article,
Http://www.microsoft.com/china/MSDN/library/data/xml/AddingCustomFunctionstoXpath.mspx?mfr=true
The article defines a split, a replace, but there is no match,
Had to expand on it on the basis of

With a closer look at the code, it's easy to extend a function, as long as you change these paragraphs:

1:customcontext.cs


Function to resolve references to my custom functions.
public override IXsltContextFunction resolvefunction (string prefix,
String name, xpathresulttype[] argtypes)
{
Xpathregexextensionfunction 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 Xpathregexextensionfunction ("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 Xpathregexextensionfunction ("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 Xpathregexextensionfunction ("Replace", 3, 3, new
Xpathresulttype[] {xpathresulttype.string, xpathresulttype.string,
Xpathresulttype.string}, xpathresulttype.string);
Break
}
return func;
}

2:xpathregexextensionfunction.cs


This are 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 does not have much relationship with the function extension, which is set the parameter.

Once this file has 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 nice to support XPath2.0, XPath2.0 These functions are built-in support, but it seems not yet supported.

All the code is here:
Http://cleo.cnblogs.com/Files/cleo/XPathExtFunction.rar

Source: Cleo BLOG



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.