icarus-consulting / Yaapii.Xml

Query XML with XPath, transform XML with XSL
MIT License
4 stars 2 forks source link

XMLPatched doesn't work with XML namespaces #79

Open aveXcaesar opened 5 months ago

aveXcaesar commented 5 months ago

Expected Behavior

Can use XMLPatched with a XML that has XML namespace.

Actual Behavior

Addressing a XML namespace in the Xpath Directive fails.

Steps to reproduce the behavior

The log given by the failure.

Yaapii.Xambly.Error.ImpossibleModificationException : Exception at dir 1: XPATH "/n0:Uos/DataContainer/Part[@uid='Pa132']/Name" (invalid XPath expr '/n0:Uos/DataContainer/Part[@uid='Pa132']/Name' (Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.))

Mention any other details that might be useful

Failing example:

var part =
                new XMLPatched(
                    new XMLWithNamespaceResolver(),
                    new Directives()
                    .Xpath("/n0:Uos/DataContainer/Part[@uid='Pa__132']/Name")
                );
aveXcaesar commented 5 months ago

My first idea to solve the bug is to inject a namespace resolver in XMLPatched. But I don't like the solution because a resolver was already injected to the XML that needs to be patched.

Situation: I have a XML in my code that needs to work with XPath expressions. I need to inject a namespace resolver to the XML to work with it. Later in the code I need the XMLPatchd object and the resolver again.

It would be nice to have access to the already injected resolver but the current interface design doesn't exposes the resolver. Should we adjust the interface(s)? What do you think?

Meerownymous commented 3 weeks ago

I would just test if the xpath contains a namespace prefix and if yes, generate a namespaceresolver based on the xml document's namespaces. Interface adjustment is unnecessary. The resolvers can simply be passed on to new objects through a private or public constructor by the original object.