为XPath自定义函数(因为XPath1.0的函数非常有限)[附源代码下载]
生活随笔
收集整理的這篇文章主要介紹了
为XPath自定义函数(因为XPath1.0的函数非常有限)[附源代码下载]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
想要一個正則表達式的匹配函數,但是XPath1.0中間沒有,
只好自己擴展一個,在網上搜了一下,有一篇文章不錯,
http://www.microsoft.com/china/MSDN/library/data/xml/AddingCustomFunctionstoXpath.mspx?mfr=true
該文章定義了一個split,一個replace,不過就是沒有match,
只好在它的基礎上,擴展一下
仔細觀察一下代碼,發現想要擴展一個函數很簡單,只要修改這幾段就好了:
1:CustomContext.cs
????????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
????????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其實和函數擴展沒有太多的關系,那是設置參數的。
這連個文件修改好了之后,就可以調用了:
????????????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這些函數都是內置支持的,可惜目前好像還不支持。
全部的代碼在這里:
/Files/cleo/XPathExtFunction.rar
總結
以上是生活随笔為你收集整理的为XPath自定义函数(因为XPath1.0的函数非常有限)[附源代码下载]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 以GIS面对崛起的城市群
- 下一篇: 卖东西