IntroductionXPath Expressions are a powerful tool, however there are some limitations. Some things that you may need to do are not available yet in the specification, or you may need to do something slightly different from the specification, or there just isn't any way to do it except for using custom code. XPath Functions are a means to providing extra functionality to solve the problems that are not covered by any other means. For this discussion, we will focus on the problem of performing case-insensitive searches in XPath expressions. The technique of creating and using custom functions and variables can certainly be applied to extend your XPath expression as necessary to solve other problems. The ProblemXML is a case sensitive language, and although this can be a good thing, sometimes it provides for frustration. Validating the XML can be used to ensure proper formatting yet sometimes this is not possible, either because there is no schema available or you may not have control of the XML format, you just get what you get and have to make it work. When attempting to select nodes using a XPath expression, there is a difference between “*//Address[@type = ‘home’]” and “*//Address[@type = ‘Home’]”. Given the XML snippet below, only one node would be returned for either of these queries. But what if you need to find all the nodes, regardless of casing? One way to do this type of matching would be to iterate through a node list and filter the results as below:XPathDocument doc = new XPathDocument("Test.xml");
XPathNavigator nav = doc.CreateNavigator();XPathNodeIterator nodes = nav.Select("*//Address");
foreach(XPathNavigator node in nodes)
{
string attr = node.GetAttribute("type", "");
if(attr.ToLower() == "home" )
{
// Do something...
}
}This is inefficient and cumbersome since you must retrieve the nodes, iterate through them, and filter out the ones that don't match. A better way would be to filter the results that are returned in the first place. XsltContextThe .NET Framework supports the ability to add custom functions to your XPath expressions by specifying a XsltContext. This abstract class provides a context for the XSLT processor to resolve any functions, variables and namespaces used in the XPath expression. When deriving a class from XsltContext you must implement four methods and one property. The two most important methods are ResolveFunction and ResolveVariable. Although they have a purpose and usage during my explorations I have not found a usage for the remaining methods and properties. public override IXsltContextFunction ResolveFunction(string prefix, string name, XPathResultType[] ArgTypes)
public override IXsltContextVariable ResolveVariable(string prefix, string name) Read more: Codeproject
XPathNavigator nav = doc.CreateNavigator();XPathNodeIterator nodes = nav.Select("*//Address");
foreach(XPathNavigator node in nodes)
{
string attr = node.GetAttribute("type", "");
if(attr.ToLower() == "home" )
{
// Do something...
}
}This is inefficient and cumbersome since you must retrieve the nodes, iterate through them, and filter out the ones that don't match. A better way would be to filter the results that are returned in the first place. XsltContextThe .NET Framework supports the ability to add custom functions to your XPath expressions by specifying a XsltContext. This abstract class provides a context for the XSLT processor to resolve any functions, variables and namespaces used in the XPath expression. When deriving a class from XsltContext you must implement four methods and one property. The two most important methods are ResolveFunction and ResolveVariable. Although they have a purpose and usage during my explorations I have not found a usage for the remaining methods and properties. public override IXsltContextFunction ResolveFunction(string prefix, string name, XPathResultType[] ArgTypes)
public override IXsltContextVariable ResolveVariable(string prefix, string name) Read more: Codeproject
0 comments:
Post a Comment