【问题标题】:Extract an attribute value , Lxml提取属性值,Lxml
【发布时间】:2014-10-07 13:39:35
【问题描述】:

我有以下 Xml 文件:

'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14"><w:body><w:p w:rsidR="00706A37" w:rsidRPr="004A1CE5" w:rsidRDefault="004A1CE5"><w:pPr><w:pStyle w:val="Heading1"/><w:numPr><w:ilvl w:val="12"/><w:numId w:val="0"/></w:numPr><w:rPr><w:sz w:val="28"/><w:szCs w:val="28"/></w:rPr></w:pPr><w:commentRangeStart w:id="0"/><w:r w:rsidRPr="004A1CE5"><w:rPr><w:sz w:val="28"/><w:szCs w:val="28"/></w:rPr><w:t>H</w:t></w:r><w:commentRangeEnd w:id="0"/><w:r w:rsidR="00A23794"><w:rPr><w:rStyle w:val="CommentReference"/> 

我需要在 &lt;w:commentRangeStart&gt; 标签中提取 id 的值。我查看了很多关于 SO 的问题,发现了以下类型:

我试过了: (使用 commentRangeStart 标记遍历每个 p,并检索属性。这没有返回任何内容。

for p in lxml_tree.xpath('.//w:p/commentRangeStart',namespaces = {'w':w}):
    print p.attrib

我尝试了'commentRangeStart[@id]'commentRangeStart/@id 的各种组合,但都没有奏效。我提到了很多问题,其中一个是here
我更喜欢一种遍历每个 p 然后搜索评论标签的方式。喜欢:

for p in lxml_tree.xpath('.//w:p',namespaces = {'w':w}):  
    p.xpath(./w:commentRangeStart/...)

等等……

我的表情怎么了??

【问题讨论】:

    标签: python xml python-2.7 xpath lxml


    【解决方案1】:

    你需要限定命名空间:

    for p in root.xpath('.//w:p/w:commentRangeStart', namespaces={'w':w}):
        print p.attrib
    

    输出:

    {'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}id': '0'}
    

    替代方案:

    for id_ in root.xpath('.//w:p/w:commentRangeStart/@w:id', namespaces={'w': w}):
        print id_
    

    输出:

    0
    

    【讨论】:

    • 我可以用 // 替换多余的 w 吗??
    • @Swordy,如果你替换它,你会得到空元素列表。
    • 你能添加代码进行 2 级迭代吗?我试过这个: for p in lxml_tree.xpath('.//w:p/w', namespaces={'w':w}): for id_ in p.xpath('./w:commentRangeStart/@w: id',namespaces={'w':w}): 打印 id_ 但不打印任何东西..
    • 原因是我需要对每个 p 进行操作,不管它是否有评论标签..
    • @Swordy, asciinema.org/a/12781(顺便说一句,给定的 xml 不完整。所以我添加了结束标签。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2018-07-17
    • 1970-01-01
    • 2011-09-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多