Closed bcpeinhardt closed 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?
Yes! Awesome, I'll follow up if I need more info, thanks!
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?
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.
Awesome thank you!
Closing this as I've made a pull request for the functionality discussed.
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,