命名空间错误 lxml xpath python

6 投票
1 回答
9879 浏览
提问于 2025-04-18 08:34

我正在把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

我查了一些资料,猜测可能是命名空间的问题,但我不知道该怎么解决?

1 个回答

12

在这段代码中:

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

w: 不是一个命名空间;它是一个命名空间的 前缀,实际上是对真正的命名空间的简写,那个真正的命名空间是 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'
  })

撰写回答