选择显示字体大小

为xpath自定义函数(因为xpath1.0的函数非常有限)

想要一个正则表达式的匹配函数,但是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.cnblogs.com/files/cleo/xpathextfunction.rar

出处:cleo blog


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons