<legend id='clgxK'><style id='clgxK'><dir id='clgxK'><q id='clgxK'></q></dir></style></legend>

<i id='clgxK'><tr id='clgxK'><dt id='clgxK'><q id='clgxK'><span id='clgxK'><b id='clgxK'><form id='clgxK'><ins id='clgxK'></ins><ul id='clgxK'></ul><sub id='clgxK'></sub></form><legend id='clgxK'></legend><bdo id='clgxK'><pre id='clgxK'><center id='clgxK'></center></pre></bdo></b><th id='clgxK'></th></span></q></dt></tr></i><div id='clgxK'><tfoot id='clgxK'></tfoot><dl id='clgxK'><fieldset id='clgxK'></fieldset></dl></div>
    1. <tfoot id='clgxK'></tfoot>
        <bdo id='clgxK'></bdo><ul id='clgxK'></ul>

      1. <small id='clgxK'></small><noframes id='clgxK'>

        Python ElementTree 默认命名空间?

        时间:2023-09-10
        • <bdo id='Fvtb7'></bdo><ul id='Fvtb7'></ul>

                <tfoot id='Fvtb7'></tfoot>
              • <i id='Fvtb7'><tr id='Fvtb7'><dt id='Fvtb7'><q id='Fvtb7'><span id='Fvtb7'><b id='Fvtb7'><form id='Fvtb7'><ins id='Fvtb7'></ins><ul id='Fvtb7'></ul><sub id='Fvtb7'></sub></form><legend id='Fvtb7'></legend><bdo id='Fvtb7'><pre id='Fvtb7'><center id='Fvtb7'></center></pre></bdo></b><th id='Fvtb7'></th></span></q></dt></tr></i><div id='Fvtb7'><tfoot id='Fvtb7'></tfoot><dl id='Fvtb7'><fieldset id='Fvtb7'></fieldset></dl></div>

                <legend id='Fvtb7'><style id='Fvtb7'><dir id='Fvtb7'><q id='Fvtb7'></q></dir></style></legend>

              • <small id='Fvtb7'></small><noframes id='Fvtb7'>

                    <tbody id='Fvtb7'></tbody>
                  本文介绍了Python ElementTree 默认命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有没有办法在 python ElementTree 中定义默认/无前缀命名空间?这似乎不起作用...

                  Is there a way to define the default/unprefixed namespace in python ElementTree? This doesn't seem to work...

                  ns = {"":"http://maven.apache.org/POM/4.0.0"}
                  pom = xml.etree.ElementTree.parse("pom.xml")
                  print(pom.findall("version", ns))
                  

                  这也不是:

                  ns = {None:"http://maven.apache.org/POM/4.0.0"}
                  pom = xml.etree.ElementTree.parse("pom.xml")
                  print(pom.findall("version", ns))
                  

                  确实如此,但我必须为每个元素添加前缀:

                  This does, but then I have to prefix every element:

                  ns = {"mvn":"http://maven.apache.org/POM/4.0.0"}
                  pom = xml.etree.ElementTree.parse("pom.xml")
                  print(pom.findall("mvn:version", ns))
                  

                  在 OSX 上使用 Python 3.5.

                  Using Python 3.5 on OSX.

                  如果答案是否",您仍然可以获得赏金 :-).我只是想从一个花了很多时间使用它的人那里得到一个明确的不".

                  if the answer is "no", you can still get the bounty :-). I just want a definitive "no" from someone who's spent a lot of time using it.

                  推荐答案

                  注意:对于 Python 3.8+,请参阅 这个答案.

                  NOTE: for Python 3.8+ please see this answer.

                  没有直接的方法可以透明地处理默认命名空间.正如您已经提到的,为空命名空间分配一个非空名称是一种常见的解决方案:

                  There is no straight-forward way to handle the default namespaces transparently. Assigning the empty namespace a non-empty name is a common solution, as you've already mentioned:

                  ns = {"mvn":"http://maven.apache.org/POM/4.0.0"}
                  pom = xml.etree.ElementTree.parse("pom.xml")
                  print(pom.findall("mvn:version", ns))
                  

                  请注意,lxml.etree 不允许显式使用空命名空间.你会得到:

                  Note that lxml.etree does not allow the use of empty namespaces explicitly. You would get:

                  ValueError:ElementPath 中不支持空的命名空间前缀

                  ValueError: empty namespace prefix is not supported in ElementPath


                  您可以通过 在加载 XML 输入数据时删除默认命名空间定义:

                  import xml.etree.ElementTree as ET
                  import re
                   
                  with open("pom.xml") as f:
                      xmlstring = f.read()
                   
                  # Remove the default namespace definition (xmlns="http://some/namespace")
                  xmlstring = re.sub(r'sxmlns="[^"]+"', '', xmlstring, count=1)
                   
                  pom = ET.fromstring(xmlstring) 
                  print(pom.findall("version"))
                  

                  这篇关于Python ElementTree 默认命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:lxml:将命名空间添加到输入文件 下一篇:如何找到“进口名称"Python中的任何包?

                  相关文章

                  最新文章

                        <bdo id='G8TQ3'></bdo><ul id='G8TQ3'></ul>

                      <small id='G8TQ3'></small><noframes id='G8TQ3'>

                      <legend id='G8TQ3'><style id='G8TQ3'><dir id='G8TQ3'><q id='G8TQ3'></q></dir></style></legend>

                    1. <i id='G8TQ3'><tr id='G8TQ3'><dt id='G8TQ3'><q id='G8TQ3'><span id='G8TQ3'><b id='G8TQ3'><form id='G8TQ3'><ins id='G8TQ3'></ins><ul id='G8TQ3'></ul><sub id='G8TQ3'></sub></form><legend id='G8TQ3'></legend><bdo id='G8TQ3'><pre id='G8TQ3'><center id='G8TQ3'></center></pre></bdo></b><th id='G8TQ3'></th></span></q></dt></tr></i><div id='G8TQ3'><tfoot id='G8TQ3'></tfoot><dl id='G8TQ3'><fieldset id='G8TQ3'></fieldset></dl></div>
                      <tfoot id='G8TQ3'></tfoot>