Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mntone/xpath
Simplified xpath function generator.
https://github.com/mntone/xpath
Last synced: 8 days ago
JSON representation
Simplified xpath function generator.
- Host: GitHub
- URL: https://github.com/mntone/xpath
- Owner: mntone
- License: mit
- Created: 2015-12-07T03:17:57.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-07T05:22:00.000Z (almost 9 years ago)
- Last Synced: 2024-09-29T17:34:18.207Z (about 1 month ago)
- Language: C#
- Size: 13.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# XPath
[![Build status](https://img.shields.io/appveyor/ci/mntone/XPath/master.svg?style=flat-square)](https://ci.appveyor.com/project/mntone/XPath) [![NuGet](https://img.shields.io/nuget/v/Mntone.XPath.svg?style=flat-square)](https://www.nuget.org/packages/Mntone.XPath/) [![Downloads](https://img.shields.io/nuget/dt/Mntone.XPath.svg?style=flat-square)](https://www.nuget.org/packages/Mntone.XPath/) [![License](https://img.shields.io/github/license/mntone/XPath.svg?style=flat-square)](https://github.com/mntone/XPath/blob/master/LICENSE.txt)
This is simplified xpath function generator for .NET.
## Requirement
- Each parser (e.g. HtmlAgilityPack)
## Usage
In this case, use HtmlAgilityPack together.
1. Install XPath.
2. Create class `XPathRetriveConfig.````csharp
internal sealed class HtmlAgilityXPathRetriveConfig : IXPathRetriveConfig
{
public static HtmlAgilityXPathRetriveConfig Instance { get { return _Instance ?? (_Instance = new HtmlAgilityXPathRetriveConfig()); } }
private static HtmlAgilityXPathRetriveConfig _Instance = null;private HtmlAgilityXPathRetriveConfig() { }
public Expression>> ChildrenGetter => node => node.ChildNodes;
public Expression> TagNameGetter => node => node.Name;
public Expression> TextGetter => node => node.InnerText;
public Expression> AttributeValueGetter => (node, name) => node.GetAttributeValue(name, string.Empty);
}
```3. Compile function and parse.
```csharp
public string GetFirstDivText(string html)
{
var func = XPathExpression.Compile("/html/body/div[0]/text()", HtmlAgilityXPathRetriveConfig.Instance);var doc = new HtmlDocument();
doc.Load(new StringReader(html));var result = func(new[] { doc.DocumentNode }).Single();
return result;
}
```## LICENSE
[MIT License](https://github.com/mntone/XPath/blob/master/LICENSE.txt)
## Author
- mntone
GitHub: https://github.com/mntone
Twitter: https://twitter.com/mntone (posted in Japanese; however, english is ok)