简体   繁体   English

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

[英]getting attribute value updated by removing special characters using lxml

Getting ID attribute and updating its value 获取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 

I am getting updated value in value replaced but elem is not updated neither the xml in which i am writing this value. 我正在替换值中获取更新的值,但elem既未更新我在其中写入此值的xml也未更新。

You can iterate through elements that have id attributes instead, and then update the attribute value, like so : 您可以改为遍历具有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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM