Skip to main content
 首页 » 编程设计

python之命名空间错误lxml xpath python

2025年05月04日10yxwkf

我正在将 word 文档转换为 xml 以使用以下代码比较它们:

word = win32com.client.Dispatch('Word.Application') 
wd = word.Documents.Open(inFile) 
# Converts the word infile to xml outfile 
wd.SaveAs(outFile,11) 
wd.Close() 
dom=parse(outFile) 

我得到的 xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<?mso-application progid="Word.Document"?> 
<w:wordDocument w:embeddedObjPresent="no" w:macrosPresent="no" w:ocxPresent="no" xml:space="preserve" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"> 
    <w:ignoreSubtree w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2"/> 
    <w:shapeDefaults> 
        <o:shapedefaults spidmax="1027" v:ext="edit"/> 
        <o:shapelayout v:ext="edit"> 
            <o:idmap data="1" v:ext="edit"/> 
        </o:shapelayout> 
    </w:shapeDefaults> 
    <w:body> 
        <wx:sect> 
            <w:tbl> 
 
            <w:tblGrid> 
                <w:gridCol w:w="200"/> 
                                       ... 
            </w:tblGrid> 
 
                <w:pict> 
                        <v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f"> 
                            <v:stroke joinstyle="miter"/> 
                            <v:formulas> 
                                <v:f eqn="if lineDrawn pixelLineWidth 0"/> 
                                ... 
                            </v:formulas> 
                            <v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f"/> 
                            <o:lock aspectratio="t" v:ext="edit"/> 
                        </v:shapetype> 
                        <v:shape id="Picture" o:spid="_x0000_s1026" style="position:absolute;left:0;text-align:left;margin-left:0;margin-top:0;width:400pt;height:40pt;z-index:1;visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:0;mso-wrap-distance-top:0;mso-wrap-distance-right:0;mso-wrap-distance-bottom:0;mso-position-horizontal:left;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:line" type="#_x0000_t75"> 
                            <v:imagedata o:title="" src="wordml://03000001.png"/> 
                            <w10:wrap anchory="line"/> 
                            <w10:anchorlock/> 
                        </v:shape> 
                </w:pict>  
                                      ... 

我不能使用 xpath 函数(lxml 库) 例如,当我尝试时:

import lxml.etree as et 
tree = et.parse(xmlFile) 
for elt in tree.xpath("//w:gridCol"): 
     elt.getparent().remove(elt) 

我收到以下错误:

 for elt in tree.xpath("//w:gridCol"): 
  File "lxml.etree.pyx", line 2029, in lxml.etree._ElementTree.xpath (src/lxml/lxml.etree.c:45934) 
  File "xpath.pxi", line 379, in lxml.etree.XPathDocumentEvaluator.__call__ (src/lxml/lxml.etree.c:114389) 
  File "xpath.pxi", line 242, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:113063) 
  File "xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src/lxml/lxml.etree.c:112894) 
XPathEvalError: Undefined namespace prefix 

我做了一些研究,我猜这是一个命名空间问题,但我不知道如何解决它?

请您参考如下方法:

在这段代码中:

for elt in tree.xpath("//w:gridCol"): 

w: 不是命名空间;它是一个命名空间 prefix ,它是实际命名空间的有效简写, http://schemas.microsoft.com/office/word/2003/wordml。如果要使用 xpath 方法搜索此命名空间中的元素,则需要为其提供命名空间前缀到命名空间的映射:

tree.xpath("//w:gridCol", namespaces={ 
  'w': 'http://schemas.microsoft.com/office/word/2003/wordml', 
  }) 

另请注意,不要求您使用相同的 命名空间前缀。以下将找到相同的元素:

tree.xpath("//bob:gridCol", namespaces={ 
  'bob': 'http://schemas.microsoft.com/office/word/2003/wordml' 
  })