2016-03-21 41 views
0

去除特殊字符获取ID属性和更新其值更新得到属性值使用LXML

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中,我写这个值。

回答

1

您可以通过具有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 
+0

非常感谢。我有当我使用XPath // @ ID不是问题我得到的元素类型以外的对象?对象类型是不同的,这就是为什么我有这个问题。 –