KWARC / rust-libxml

Rust wrapper for libxml2
https://crates.io/crates/libxml
MIT License
76 stars 38 forks source link

Help Wanted | Vaidate XPath Syntax Independent of Context #92

Closed bcpeinhardt closed 2 years ago

bcpeinhardt commented 2 years ago

Hello,

I would like to use this library to validate that some xpaths are syntactically correct. I don't really need the context, I just need to be able to catch malformed xpaths, like "//div[text(}="Hello"]". Is this doable in libxml?

Thanks,

dginev commented 2 years ago

@bcpeinhardt recalling from memory, libxml will raise an error if you ask it to create an XPath that isn't using recognized/valid syntax.

So yes, we could extend this wrapper to allow a quick check if an xpath expression is valid by trying to build it. libxml will always require a document context however, at least from what I see in the original docs: http://xmlsoft.org/html/libxml-xpath.html#xmlXPathCtxtCompile

I think adding a new method to the xpath.rs module based on xmlXPathCtxtCompile will get us what you're after. Do you think you can try this out and make a pull request to contribute it to the crate?

bcpeinhardt commented 2 years ago

Yes! Awesome, I'll follow up if I need more info, thanks!

bcpeinhardt commented 2 years ago

What about this function: http://xmlsoft.org/html/libxml-xpath.html#xmlXPathCompile ? It looks like they have the two steps separated out, but I can't really tell what would fail here. Assuming the error checking I need takes place here, would you be alright with a binding to this?

dginev commented 2 years ago

xmlXPathCompile may be an even more appropriate one, but indeed - definitely test what succeeds and what fails there, since I don't know either, at the moment.

The function is already available in the bindgen-generated interface btw:

pub fn xmlXPathCompile(str: *const xmlChar) -> xmlXPathCompExprPtr;

All you ought to need is a high-level wrapper in xpath.rs that uses it appropriately. Basically return false if the pointer is NULL and free the xmlXPathCompExprPtr data and return true if the pointer has a defined target.

bcpeinhardt commented 2 years ago

Awesome thank you!

bcpeinhardt commented 2 years ago

Closing this as I've made a pull request for the functionality discussed.