为XPath自定义函数(因为XPath1.0的函数非常有限)
点击次数:17 次 发布日期:2008-11-26 12:44:16 作者:源代码网
|
源代码网推荐 只好自己扩展一个,在网上搜了一下,有一篇文章不错, 源代码网推荐 http://www.microsoft.com/china/MSDN/library/data/xml/AddingCustomFunctionstoXpath.mspx?mfr=true 源代码网推荐 该文章定义了一个split,一个replace,不过就是没有match, 源代码网推荐 只好在它的基础上,扩展一下 源代码网推荐 源代码网推荐 仔细观察一下代码,发现想要扩展一个函数很简单,只要修改这几段就好了: 源代码网推荐 源代码网推荐 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 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; 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 另外一个文件XPathExtensionVariable.cs其实和函数扩展没有太多的关系,那是设置参数的。 源代码网推荐 源代码网推荐 这连个文件修改好了之后,就可以调用了: 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 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); 源代码网推荐 当然,要是支持XPath2.0 就好了,XPath2.0这些函数都是内置支持的,可惜目前好像还不支持。 源代码网推荐 源代码网推荐 全部的代码在这里: 源代码网推荐 http://cleo.cnblogs.com/Files/cleo/XPathExtFunction.rar 源代码网推荐 源代码网推荐 出处:cleo BLOG 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
