Shoobx / xmldiff

A library and command line utility for diffing xml

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bug: xpath cannot detect None prefix namespace

UniMars opened this issue · comments

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">

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

@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?

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.

@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.

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

@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.