Skip to content

Latest commit

History

165 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Xsoup

Build Status

XPath selector based on Jsoup.

Get started:

@TestpublicvoidtestSelect() {
Stringhtml = "<html><div><a href='https://github.com'>github.com</a></div>" +
"<table><tr><td>a</td><td>b</td></tr></table></html>";
Documentdocument = Jsoup.parse(html);
Stringresult = Xsoup.compile("//a/@href").evaluate(document).get();
Assert.assertEquals("https://github.com", result);
List<String> list = Xsoup.compile("//tr/td/text()").evaluate(document).list();
Assert.assertEquals("a", list.get(0));
Assert.assertEquals("b", list.get(1));
}

Performance:

Xsoup use Jsoup as HTML parser.

Compare with another most used XPath selector for HTML - HtmlCleaner, Xsoup is much faster:

Normal HTML, size 44KB
XPath: "//a" Run for 2000 times
Environment:Mac Air MD231CH/A CPU: 1.8Ghz Intel Core i5
OperationXsoupHtmlCleaner
parse3,207(ms)7,999(ms)
select95(ms)380(ms)

Syntax supported:

XPath1.0:

NameExpressionSupport
nodenamenodenameyes
immediate parent/yes
parent//yes
attribute[@key=value]yes
nth childtag[n]yes
attribute/@keyyes
wildcard in tagname/*yes
wildcard in attribute/[@*]yes
functionfunction()part
ora | byes since 0.2.0
parent in path. or ..no
predicatesprice>35no
predicates logic@class=a or @class=byes since 0.2.0

Function supported:

In Xsoup, we use some function (maybe not in Standard XPath 1.0):

ExpressionDescriptionStandard XPath
text(n)nth text content of element(0 for all)text() only
allText()text including childrennot support
tidyText()text including children, well formattednot support
html()innerhtml of elementnot support
outerHtml()outerHtml of elementnot support
regex(@attr,expr,group)use regex to extract contentnot support

Extended syntax supported:

These XPath syntax are extended only in Xsoup (for convenience in extracting HTML, refer to Jsoup CSS Selector):

NameExpressionSupport
attribute value not equals[@key!=value]yes
attribute value start with[@key~=value]yes
attribute value end with[@key$=value]yes
attribute value contains[@key*=value]yes
attribute value match regex[@key~=value]yes

License

MIT License, see file LICENSE

Bitdeli Badge

About

When jsoup meets XPath.

Resources

Stars

473 stars

Watchers

42 watching

Forks

Releases

Packages

Used by

Contributors

Languages