开发者

How is XPath evaluation implemented?

开发者 https://www.devze.com 2023-02-17 12:45 出处:网络
How would I write a simple XPath evaluator for an XPath query like: //user[role=\'moderator\']/address/./zipcode

How would I write a simple XPath evaluator for an XPath query like:

//user[role='moderator']/address/./zipcode

?

I'm trying to write an XPath inspired tool for locating files in a directory.

Edit: I should have clarified but I'm not trying to wr开发者_如何学Pythonite a full-fledged XPath implementation. If it supports queries like seen at http://www.w3schools.com/XPath/xpath_syntax.asp, that's sufficient for my purposes. Also, don't worry about the difficulty level. I'm a bit crazy and I'll find a way to pull it off :)


I've migrated my comments to an answer.

You might want to look at this Steps and involvement of implementing a parser (in .Net - and in this case XPath 2.0) It should be said that making a "locate files" parser would be MUCH easier than a XPath parser. But the tools could be useful :)

Instead of making a parser, you could make a XPathNavigator if you use .Net (and fake your filesystem as XML) implementation, and use the internal XPath parser. Here is an example Microsoft have made which works on the filesystem: http://msdn.microsoft.com/en-us/library/aa735772(v=vs.71).aspx

you can use it with XPath expressions like

XPathNavigator navigator = new FileSystemNavigator(@"C:\Program Files");

XPathExpression expression1 = XPathExpression.Compile("/node()");

var res = navigator.Select(expression1);


Depends on how simple it is going to be. You can define a regular expression representing an XPath expression and then extracts each path segment and navigate the tree according to the rules.


I'm inclined to say that if you need to ask the question, you probably shouldn't be attempting this. If you want to go ahead regardless, start by reading some books on compiler writing, especially lexical and syntax analysis. Alternatively, if you're not the kind of person who learns from books, get hold of an open source XPath implementation and take it apart to see how it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号