Shoobx / xmldiff

A library and command line utility for diffing xml
MIT License
204 stars 52 forks source link

bug: xpath cannot detect None prefix namespace #113

Closed UniMars closed 1 year ago

UniMars commented 1 year ago

When I use xmldiff.patch to execute patching, it use xpath to search the node. Because the nsmap of the tree is like {None:'aaaURL','s':"bbbURL"}, the default namespace prefix is None, so the xpath cannot read it. I cannot use my own namespace as input, nor can i edit the nsmap of the tree or transfer None to '' The error message goes like this:

File "D:\Programs\Code\python\projects\auto_localization.venv\lib\site-packages\xmldiff\patch.py", line 55, in _handle_UpdateTextAfter tree.xpath(action.node, namespaces=self.nsmap)[0].tail = action.text File "src\lxml\etree.pyx", line 1597, in lxml.etree._Element.xpath File "src\lxml\xpath.pxi", line 259, in lxml.etree.XPathElementEvaluator.init File "src\lxml\xpath.pxi", line 131, in lxml.etree._XPathEvaluatorBase.init File "src\lxml\xpath.pxi", line 55, in lxml.etree._XPathContext.init File "src\lxml\extensions.pxi", line 81, in lxml.etree._BaseContext.init TypeError: empty namespace prefix is not supported in XPath

and the xml header is like this:

\<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib">

regebro commented 1 year ago

Yes, this is a known issue in lxml that I currently have no good workaround for. See also #89

UniMars commented 1 year ago

@regebro If you cannot figure out a good solution for now, why not use a temporary patch like offer a nsmap argment to use my own nsmap?

regebro commented 1 year ago

Replacing None with '' doesn't work, I already tried that, btw. It's still empty.

Although now I got another idea, I'll try that.

UniMars commented 1 year ago

@regebro yeah, I also found that, for now, I just delete the none namespace. That's why I advised to allow users to use their own nsmap, at least for some, the none namespace is not required.

UniMars commented 1 year ago

The biggest problem is that i have difficult read the source code of how xpath extract and parse namespace dictionary

regebro commented 1 year ago

Right, that's the same idea I just had. It has other side effects, but I think they will be easily fixable.

On Sat, May 20, 2023 at 9:00 PM noctboat @.***> wrote:

@regebro https://github.com/regebro yeah, I also found that, for now, I just delete the none namespace. That's why I advised to allow users to use their own nsmap, at least for some, the none namespace is not required.

— Reply to this email directly, view it on GitHub https://github.com/Shoobx/xmldiff/issues/113#issuecomment-1555972708, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGIK5FTA2JCS3KTLME7FQDXHEIEFANCNFSM6AAAAAAYI3N3OA . You are receiving this because you were mentioned.Message ID: @.***>

regebro commented 1 year ago

Done

UniMars commented 1 year ago

@regebro It seems you fix it in the formatter, but it doesn't work for the main.batch as it doesn't use formatter at all.

regebro commented 1 year ago

Ah.