惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
博客园 - 司徒正美
C
Check Point Blog
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
IT之家
IT之家
B
Blog
博客园_首页
量子位
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
J
Java Code Geeks
H
Help Net Security
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
D
DataBreaches.Net
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News

博客园 - 花生!~~

[技术分享] 谈谈网页数据采集中的“结构化”难题:从手动 XPath 到智能识别算法的演进 windows10 .NET http请求 tls1.3问题 微信历史版本下archive Cefsharp开发相关注意 .net PostgreSQL 相关 .net4.0 请求HTTPS出错:未能创建 SSL/TLS 安全通道 c#中的gcAllowVeryLargeObjects和OutOfMemoryException XML hexadecimal value 0x__, is an invalid character 创建窗口句柄时出错(error creating window handle) .net程序和管理员权限的一些事 - 花生!~~ - 博客园 .net下WinDbg使用说明 在.net中修改Webbrowser控件的IE版本 System.Data.SQLite安装的相关问题 TextBox Ctrl+A不能全选的问题 HttpWebRequest 跳转后(301,302)ResponseUri乱码问题 如何用c#本地代码实现与Webbrowser中的JavaScript交互 在sqlite中,如何删除字段? how to drop a column in sqlite 如何在centos下配置redis开机自启动 WinForm中的图表控件Chart
XPath高级用法(冰山一角)
花生!~~ · 2014-08-13 · via 博客园 - 花生!~~

运算符+内置函数

使用XPath选择元素时,使用运算符+内置函数来进行筛选:

.//div[contains(@class,"ec_desc") or contains(@class,"ec_adv_title_desc")]
.//span[@class="ec_site" or @class="ec_adv_site"]

使用c# .net中添加XPath自定义函数

参考:
(http://technet.microsoft.com/zh-cn/magazine/dd567715(VS.100).aspx)
(http://www.cnblogs.com/shenba/archive/2009/12/18/1626898.html)
(http://msdn.microsoft.com/zh-cn/library/ms950806.aspx)

XsltContext,IXsltContextFunction,IXsltContextVariable

        public override IXsltContextFunction ResolveFunction(string prefix,
     string name, XPathResultType[] ArgTypes)
        {
            XPathExtensionFunction func = null;
            // Create an instance of appropriate extension function class.
            switch (name)
            {
                // 匹配正则表达式, XPath1.0没有该方法
                case "IsMatch":
                    func = new XPathExtensionFunction("IsMatch", 2, 2, new
        XPathResultType[] { XPathResultType.String, XPathResultType.String }, XPathResultType.Boolean);
                    break;
                case "Replace":
                    func = new XPathExtensionFunction("Replace", 3, 3, new
        XPathResultType[] { XPathResultType.String, XPathResultType.String, XPathResultType.String }, XPathResultType.String);
                    break;
                // 去除空格
                case "Trim":
                    func = new XPathExtensionFunction("Trim", 1, 1,
                        new XPathResultType[] { XPathResultType.String }, XPathResultType.String);
                    break;
                default:
                    throw new ArgumentException("没有定义" + name + "函数");
            }

            return func;
        }
        // 在运行时调用
        public object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
        {
            // The two custom XPath extension functions
            switch (m_FunctionName)
            {
                case "IsMatch":
                    // 调用正则匹配 参数一为正则表达式
                    return Regex.IsMatch(args[0].ToString(), args[1].ToString());
                case "Replace":
                    // 调用正则匹配 参数一为正则表达式
                    return Regex.Replace(args[0].ToString(), args[1].ToString(),args[2].ToString());
                case "Trim":
                    return docContext.Value.Trim();
                default:
                    throw new ArgumentException("没有定义" + m_FunctionName + "函数");
            }
        }
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XpathContext xpathContext = new XpathContext();

            var nodes = doc.SelectNodes(@"//a[Replace(string(@href),'^.*2009-10.*$','xxx')='xxx']", xpathContext);
            foreach (XmlNode item in nodes)
            {
                Console.WriteLine(item.Attributes["href"].Value);
            }

注意:自定义函数时,引用属性作为参数时,使用string()函数转换一下

string(@href)

XPath其本质就是用来选择*ML元素的,对于自定义函数,应该是用来给选择元素的过程中,提供一个条件,不选,YESNO,所以本质上函数应该都是布尔型的返回值。即使你定义一个函数返回值不是布尔型,比如string,那么你就必须在XPath表达式中进行比较运算,类似 //span[myfun(str)='result']。否则没有任何意义。