做网页解析时,将html转成xml格式之后,再利用xpath则可以轻易地截取任何所需要的数据。在使用xpath时,常常会用到其中的一些函数,特别是字符串函数,完整的函数列表可在w3school找到:http://www.w3school.com.cn/xpath/xpath_functions.asp#string)。然而这仅仅是标准xpath里所提供的,dotnet里这只实现了一部分,特别是本标题所提到的正则匹配函数fn:matches(string,pattern),dotnet里竟然不支持。虽然dotnnet里还是能用诸如contains等判断函数,但比起强大的正则表达式,其实现的功能远远不能满足要求。
为了能够更加灵活、方便的匹配字符串,需要给xpath添加自定义的正则匹配函数,幸好dotnet提供了接口,先看看这些基类及接口。
XsltContext
封装可扩展样式表转换语言 (XSLT) 处理器的当前执行上下文,使 XML 路径语言 (XPath) 在 XPath 表达式中解析函数、参数和命名空间。需要定义一个继承XsltContext的子类,实现对自定义函数的调用。
IXsltContextFunction
为在运行库执行期间在可扩展样式表转换语言 (XSLT) 样式表中定义的给定函数提供一个接口。实现该接口的子类定义以及提供自定义函数的功能。
IXsltContextVariable
为在运行库执行期间在样式表中定义的给定变量提供一个接口。其子类实现在调用自定义函数时,参数值的计算。
涉及类比较多,但是在具体实现远没有那么复杂,后续添加其他函数也很方便。
关键代码
public class XpathContext : XsltContext
{
// XsltArgumentList to store my user defined variables
private XsltArgumentList m_ArgList;
// Constructors
public XpathContext()
{ }
public XpathContext(NameTable nt)
: base(nt)
{
}
public XpathContext(NameTable nt, XsltArgumentList argList)
: base(nt)
{
m_ArgList = argList;
}
// Returns the XsltArgumentList that contains custom variable definitions.
public XsltArgumentList ArgList
{
get
{
return m_ArgList;
}
}
// Function to resolve references to my custom functions.
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 "Match":
func = new XPathExtensionFunction("Match", 1, 2, new
XPathResultType[] { XPathResultType.NodeSet, XPathResultType.String }, XPathResultType.Boolean);
break;
// 去除空格
case "Trim":
func = new XPathExtensionFunction("Trim", 1, 1,
new XPathResultType[] { XPathResultType.String }, XPathResultType.String);
break;
default:
throw new ArgumentException("没有定义" + name + "函数");
}
return func;
}
// Function to resolve references to my custom variables.
public override IXsltContextVariable ResolveVariable(string prefix, string name)
{
// Create an instance of an XPathExtensionVariable.
XPathExtensionVariable Var;
Var = new XPathExtensionVariable(name);
return Var;
}
public override int CompareDocument(string baseUri, string nextbaseUri)
{
return 0;
}
public override bool PreserveWhitespace(XPathNavigator node)
{
return true;
}
public override bool Whitespace
{
get
{
return true;
}
}
}
{
// XsltArgumentList to store my user defined variables
private XsltArgumentList m_ArgList;
// Constructors
public XpathContext()
{ }
public XpathContext(NameTable nt)
: base(nt)
{
}
public XpathContext(NameTable nt, XsltArgumentList argList)
: base(nt)
{
m_ArgList = argList;
}
// Returns the XsltArgumentList that contains custom variable definitions.
public XsltArgumentList ArgList
{
get
{
return m_ArgList;
}
}
// Function to resolve references to my custom functions.
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 "Match":
func = new XPathExtensionFunction("Match", 1, 2, new
XPathResultType[] { XPathResultType.NodeSet, XPathResultType.String }, XPathResultType.Boolean);
break;
// 去除空格
case "Trim":
func = new XPathExtensionFunction("Trim", 1, 1,
new XPathResultType[] { XPathResultType.String }, XPathResultType.String);
break;
default:
throw new ArgumentException("没有定义" + name + "函数");
}
return func;
}
// Function to resolve references to my custom variables.
public override IXsltContextVariable ResolveVariable(string prefix, string name)
{
// Create an instance of an XPathExtensionVariable.
XPathExtensionVariable Var;
Var = new XPathExtensionVariable(name);
return Var;
}
public override int CompareDocument(string baseUri, string nextbaseUri)
{
return 0;
}
public override bool PreserveWhitespace(XPathNavigator node)
{
return true;
}
public override bool Whitespace
{
get
{
return true;
}
}
}