title: How Can I Output Non-Escaped Element Tag In XML?
body: <p>I have a Python script that I have inherited and my issue is that right now I have a chunk of text in a <code>paragraph</code> variable that contains anchor tags. For example:</p>
  
  <pre><code>This is text with a &lt;a href="http://somewebsite.com"&gt;Link&lt;/a&gt; in it.
  </code></pre>
  
  <p>What I'm required to do however is convert the anchor tags to the <code>apxh</code> name space so the above line should look something like this:</p>
  
  <pre><code>This is text with a &lt;apxh:a href="http://somewebsite.com"&gt;Link&lt;/apxh:a&gt; in it.
  </code></pre>
  
  <p>The problem is the way I have it above is outputting:    </p>
  
  <pre><code>This is text with a &amp;lt;apxh:a href=\"http://somewebsite.com;\"&amp;gt;Link Text;&amp;lt;/apxh:a&amp;gt; in it.
  </code></pre>
  
  <p>My guess is that when I'm running the for loop on the <code>paragraph</code>, I need to some how find all anchor tags and text and do something like <code>etree.Element("{%s}a" % nm["apxh"], nsmap=nm)</code> but I'm not really sure</p>
  
  <p><strong>This is the current script:</strong></p>
  
  <pre><code>def get_news_feed(request):
      articles = models.Article.objects.all().filter(distributable = True)
  
      nm = {
              None: "http://www.w3.org/2005/Atom",
              "ap": "http://ap.org/schemas/03/2005/aptypes",
              "apcm": "http://ap.org/schemas/03/2005/apcm",
              "apnm": "http://ap.org/schemas/03/2005/apnm",
              "apxh": "http://www.w3.org/1999/xhtml",
              }
  
      doc = etree.Element("{%s}feed" % nm[None], nsmap=nm)
  
      for article in articles:
          entry = etree.Element("{%s}entry" % nm[None], nsmap=nm)
          content = etree.Element("{%s}content" % nm[None], nsmap=nm)
          content.set("type", "xhtml")
  
          div = etree.Element("{%s}div" % nm["apxh"], nsmap=nm)
          for paragraph in article.body.replace("&amp;amp;", "&amp;").split("\n"):
              par = etree.Element("{%s}p" % nm["apxh"], nsmap=nm)
              par.text = paragraph            
              par.text = paragraph.replace("&lt;a", "&lt;apxh:a")            
              par.text = par.text.replace("&lt;/a", "&lt;/apxh:a")  
              par.text = cleanup_entities(par.text)
              div.append(par)
          content.append(div)
          entry.append(content)
  
          doc.append(entry)
  
      output = etree.tostring(doc, encoding="UTF-8", xml_declaration=True, pretty_print=True)
      return HttpResponse(output, mimetype="application/xhtml+xml")
  </code></pre>
  
  <p><strong>This is how the output should look:</strong></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;feed xmlns:ap="http://ap.org/schemas/03/2005/aptypes" xmlns:apxh="http://www.w3.org/1999/xhtml" xmlns:apnm="http://ap.org/schemas/03/2005/apnm" xmlns:apcm="http://ap.org/schemas/03/2005/apcm" xmlns="http://www.w3.org/2005/Atom"&gt;
    &lt;entry&gt;
      &lt;content type="xhtml"&gt;
        &lt;apxh:div&gt;
          &lt;apxh:p&gt;This is some text&lt;/apxh:p&gt;
          &lt;apxh:p&gt;This is text with a &lt;apxh:a href="http://somewebsite.com"&gt;Link&lt;/apxh:a&gt; in it.&lt;/apxh:p&gt;
          &lt;apxh:p&gt;Theater&lt;/apxh:p&gt;
        &lt;/apxh:div&gt;
      &lt;/content&gt;
    &lt;/entry&gt;
  &lt;/feed&gt;
  </code></pre>
  
  <p><strong>This is how the output currently looks:</strong></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;feed xmlns:ap="http://ap.org/schemas/03/2005/aptypes" xmlns:apxh="http://www.w3.org/1999/xhtml" xmlns:apnm="http://ap.org/schemas/03/2005/apnm" xmlns:apcm="http://ap.org/schemas/03/2005/apcm" xmlns="http://www.w3.org/2005/Atom"&gt;
    &lt;entry&gt;
      &lt;content type="xhtml"&gt;
        &lt;apxh:div&gt;
          &lt;apxh:p&gt;This is some text&lt;/apxh:p&gt;
          &lt;apxh:p&gt;This is text with a &amp;lt;apxh:a href=\"http://somewebsite.com;\"&amp;gt;Link Text;&amp;lt;/apxh:a&amp;gt; in it.&lt;/apxh:p&gt;
          &lt;apxh:p&gt;Theater&lt;/apxh:p&gt;
        &lt;/apxh:div&gt;
      &lt;/content&gt;
    &lt;/entry&gt;
  &lt;/feed&gt;
  </code></pre>