国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

為XPath自定義函數(因為XPath1.0的函數非常有限)

2019-11-18 17:18:31
字體:
來源:轉載
供稿:網友

想要一個正則表達式的匹配函數,但是XPath1.0中間沒有,
只好自己擴展一個,在網上搜了一下,有一篇文章不錯,
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.VEVb.com/Files/cleo/XPathExtFunction.rar

出處:cleo BLOG


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 小金县| 和田市| 开原市| 定日县| 全南县| 新化县| 察哈| 淮安市| 海晏县| 类乌齐县| 漾濞| 日喀则市| 额尔古纳市| 砀山县| 榕江县| 游戏| 肥西县| 将乐县| 怀安县| 晋中市| 昌黎县| 洛隆县| 上栗县| 贡山| 清镇市| 奉贤区| 都江堰市| 应城市| 灵寿县| 信阳市| 嘉峪关市| 滕州市| 靖边县| 灵寿县| 多伦县| 盱眙县| 防城港市| 乳源| 台中县| 浦县| 新和县|