mischov / meeseeks

An Elixir library for parsing and extracting data from HTML and XML with CSS or XPath selectors.
MIT License
316 stars 23 forks source link

Prohibit XPath attribute steps outside of predicates #88

Closed mischov closed 4 years ago

mischov commented 4 years ago

PR for issue #85.

Due both to how selection works and how attributes are represented in documents (stored as part of an element, rather than as a separate node) there is no easy way to implement attribute selection, and use of attributes steps are prohibited outside of predicates and will raise an error.

For example, xpath("\\p[@class]") which returns elements with class attributes is allowed, but xpath("\\p\@class") which would return the class attributes themselves is prohibited.

To extract a selected element's attribute use the attr extractor.

Meeseeks.all(doc, xpath("//p[@class]"))
|> Enum.map(&Meeseeks.attr(&1, "class"))