title: How do I replace an element in lxml with a string
body: <p>I'm trying to figure out in lxml and python how to replace an element with a string. </p>
  
  <p>In my experimentation, I have the following code: </p>
  
  <pre><code>from lxml import etree as et
  
  docstring = '&lt;p&gt;The value is permitted only when that includes &lt;xref linkend=\"my linkend\" browsertext=\"something here\" filename=\"A_link.fm\"/&gt;, otherwise the value is reserved.&lt;/p&gt;'
  
  topicroot = et.XML(docstring)
  topicroot2 = et.ElementTree(topicroot) 
  xref = topicroot2.xpath('//*/xref')
  xref_attribute = xref[0].attrib['browsertext']
  
  print href_attribute
  </code></pre>
  
  <p>The result is: 'something here' </p>
  
  <p>This is the browser text attribute I'm looking for in this small sample. But what I can't seem to figure out is how to replace the entire  element with the attribute text I've captured here. </p>
  
  <p>(I do recognize that in my sample I could have multiple xrefs and will need to construct a loop to go through them properly.) </p>
  
  <p>What's the best way to go about doing this?</p>
  
  <p>And for those wondering, I'm having to do this because the link actually goes to a file that doesn't exist because of our different build systems. </p>
  
  <p>Thanks in advance!</p>