命名空间错误lxml xpath python

use*_*690 1 python xml xpath lxml

我正在将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)
Run Code Online (Sandbox Code Playgroud)

我得到的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> 
                                      ...
Run Code Online (Sandbox Code Playgroud)

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

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

我收到以下错误:

 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
Run Code Online (Sandbox Code Playgroud)

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

lar*_*sks 9

在这段代码中:

for elt in tree.xpath("//w:gridCol"):
Run Code Online (Sandbox Code Playgroud)

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

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

另请注意,不要求使用相同的名称空间前缀.以下内容将找到相同的元素:

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

  • “elt”已经知道名称空间及其映射(请参阅“elt.nsmap”),那么为什么它不使用它们呢? (2认同)
  • 我与同事重新考虑了这一点,我们得出的结论是重用“.nsmap”是不正确的。ns 前缀可以根据实例进行更改,因为 XML 的发送者可以自由选择它们。如果我们使用固定的 ns 前缀编写代码,只要我们还提供一个将该 ns 前缀与命名空间 uri 关联起来的固定映射,就可以了。仅仅依靠“.nsmap”是不够的。 (2认同)