通过使用 lxml 删除特殊字符来更新属性值

getting attribute value updated by removing special characters using lxml

获取 ID 属性并更新其值

for elem in doc.xpath('//@id',namespaces={'leg':'http://www.lexis-nexis.com/glp/leg'}):
                s = str(elem)
                replaced = re.sub(r'([^a-zA-Z0-9\.\_])','',s)
                elem=replaced 

我正在替换值中获取更新值,但 elem 未更新,我正在写入此值的 xml 也未更新。

您可以遍历具有 id 属性的元素,然后更新属性值,如下所示:

for elem in doc.xpath('//*[@id]', namespaces={'leg':'http://www.lexis-nexis.com/glp/leg'}):
    elem.attrib['id'] = re.sub(r'([^a-zA-Z0-9\.\_])', '', elem.attrib['id'])

# don't forget to write changes back to the disk if necessary