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

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

為XPath自定義函數(shù)(因?yàn)閄Path1.0的函數(shù)非常有限)

2024-07-21 02:29:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

想要一個(gè)正則表達(dá)式的匹配函數(shù),但是xpath1.0中間沒(méi)有,
只好自己擴(kuò)展一個(gè),在網(wǎng)上搜了一下,有一篇文章不錯(cuò),
http://www.microsoft.com/china/msdn/library/data/xml/addingcustomfunctionstoxpath.mspx?mfr=true
該文章定義了一個(gè)split,一個(gè)replace,不過(guò)就是沒(méi)有match,
只好在它的基礎(chǔ)上,擴(kuò)展一下

仔細(xì)觀察一下代碼,發(fā)現(xiàn)想要擴(kuò)展一個(gè)函數(shù)很簡(jiǎn)單,只要修改這幾段就好了:

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;
        }
 

另外一個(gè)文件xpathextensionvariable.cs其實(shí)和函數(shù)擴(kuò)展沒(méi)有太多的關(guān)系,那是設(shè)置參數(shù)的。

這連個(gè)文件修改好了之后,就可以調(diào)用了:

 

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);
當(dāng)然,要是支持xpath2.0 就好了,xpath2.0這些函數(shù)都是內(nèi)置支持的,可惜目前好像還不支持。

全部的代碼在這里:
http://cleo.cnblogs.com/files/cleo/xpathextfunction.rar

出處:cleo blog

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 延边| 安义县| 拉萨市| 金寨县| 临西县| 莲花县| 冀州市| 社旗县| 巴林左旗| 永德县| 城口县| 吉木萨尔县| 依兰县| 札达县| 富源县| 许昌县| 鄂温| 荔浦县| 辽宁省| 华阴市| 芜湖县| 武邑县| 额尔古纳市| 专栏| 全椒县| 广安市| 芦山县| 张家界市| 华池县| 博客| 本溪| 丹江口市| 荣昌县| 虹口区| 屏边| 广元市| 清水河县| 田阳县| 惠水县| 乌什县| 西盟|