title: Including XHTML content when creating ReqIf XML document using pyXB
body: <p>A bit of background: in the scope of a requirements management plugin for Sphinx, I'm looking into ways to export ReqIF XML content. I've found pyreqif, but found that it isn't complete enough to suit our needs at the moment. </p>
  
  <p>I decided to take a look at the Reqif bindings generated by pyXB instead, with the idea that pyXB can do all the grunt work of converting things to and from XML and I just have to worry about adding some convenience functions/classes.</p>
  
  <p>The project can be found here: <a href="https://github.com/bavovanachte/reqif_pyxb_tryout" rel="nofollow noreferrer">https://github.com/bavovanachte/reqif_pyxb_tryout</a></p>
  
  <p>So far it's going great: I've managed to create instances of all the objects and they tie together nicely into an xml document. The only thing I'm having trouble with is the creation of XHTML content. Ideally I'd want to take existing html content and insert that into the tree.
  The naieve approach of doing that caused the xml-unsafe characters to be escaped, so that didn't work.</p>
  
  <p>These are some of my attempts:</p>
  
  <h1>Attempt 1: Passing the xml as a string to the XHTML_CONTENT constructor</h1>
  
  <pre class="lang-py prettyprint-override"><code>xml_string = '''
  &lt;div&gt;
      XY Block Adapter shall translate the Communication to TMN-Block in a bidirectional manner and support all functionalities of a TMN-Block.&lt;br/&gt;
  &lt;/div&gt;'''
  att_value_xhtml = ATTRIBUTE_VALUE_XHTML(definition=text_attribute, THE_VALUE=XHTML_CONTENT(div=xml_string))
  </code></pre>
  
  <h2>Result: Escaped XML content:</h2>
  
  <pre class="lang-xml prettyprint-override"><code>&amp;lt;div&amp;gt;
      XY Block Adapter shall translate the Communication to TMN-Block in a bidirectional manner and support all functionalities of a TMN-Block.&amp;lt;br/&amp;gt;
  &amp;lt;/div&amp;gt;&lt;/ns2:div&gt;
  </code></pre>
  
  <h1>Attempt 2: Passing the xml as a string to the XHTML_CONTENT constructor, with the "_from_xml flag set"</h1>
  
  <pre class="lang-py prettyprint-override"><code>xml_string = '''
  &lt;div&gt;
      XY Block Adapter shall translate the Communication to TMN-Block in a bidirectional manner and support all functionalities of a TMN-Block.&lt;br/&gt;
  &lt;/div&gt;'''
  att_value_xhtml = ATTRIBUTE_VALUE_XHTML(definition=text_attribute, THE_VALUE=XHTML_CONTENT(xml_string, _from_xml=True))
  </code></pre>
  
  <h2>Result: pyXB exception:</h2>
  
  <pre><code> Traceback (most recent call last):
     File "examples/export_test.py", line 105, in &lt;module&gt;
       att_value_xhtml = ATTRIBUTE_VALUE_XHTML(definition=text_attribute, THE_VALUE=XHTML_CONTENT(xml_string, _from_xml=True))
     File "/home/bvn/.pyenv/versions/3.6.10/lib/python3.6/site-packages/pyxb/binding/basis.py", line 2127, in __init__
       self.extend(args, _from_xml=from_xml, _location=location)
     File "/home/bvn/.pyenv/versions/3.6.10/lib/python3.6/site-packages/pyxb/binding/basis.py", line 2612, in extend
       [ self.append(_v, **kw) for _v in value_list ]
     File "/home/bvn/.pyenv/versions/3.6.10/lib/python3.6/site-packages/pyxb/binding/basis.py", line 2612, in &lt;listcomp&gt;
       [ self.append(_v, **kw) for _v in value_list ]
     File "/home/bvn/.pyenv/versions/3.6.10/lib/python3.6/site-packages/pyxb/binding/basis.py", line 2588, in append
       raise pyxb.MixedContentError(self, value, location)
   pyxb.exceptions_.MixedContentError: Invalid non-element content
  </code></pre>
  
  <h1>Attempt number 3 - Passing the xml as a string to the xhtml_div_type constructor, with the "_from_xml flag set", then assigning this class to the div member.</h1>
  
  <pre class="lang-py prettyprint-override"><code>xml_string = '''
  &lt;div&gt;
      XY Block Adapter shall translate the Communication to TMN-Block in a bidirectional manner and support all functionalities of a TMN-Block.&lt;br/&gt;
  &lt;/div&gt;'''
  att_value_xhtml = ATTRIBUTE_VALUE_XHTML(definition=text_attribute, THE_VALUE=XHTML_CONTENT(div=xhtml_div_type(xml_string, _from_xml=True)))
  </code></pre>
  
  <h2>Result: Escaped XML content:</h2>
  
  <pre class="lang-xml prettyprint-override"><code>&amp;lt;div&amp;gt;
      XY Block Adapter shall translate the Communication to TMN-Block in a bidirectional manner and support all functionalities of a TMN-Block.&amp;lt;br/&amp;gt;
  &amp;lt;/div&amp;gt;&lt;/ns2:div&gt;
  </code></pre>
  
  <h1>Attempt number 4 - Converting the string to dom first and using that in the constructor</h1>
  
  <pre class="lang-py prettyprint-override"><code>xml_string = '''
  &lt;div&gt;
      XY Block Adapter shall translate the Communication to TMN-Block in a bidirectional manner and support all functionalities of a TMN-Block.&lt;br/&gt;
  &lt;/div&gt;'''
  dom_content = xml.dom.minidom.parseString('&lt;myxml&gt;Some data&lt;empty/&gt; some more data&lt;/myxml&gt;')
  att_value_xhtml = ATTRIBUTE_VALUE_XHTML(definition=text_attribute, THE_VALUE=XHTML_CONTENT(div=xhtml_div_type(_dom_node=dom_content)))
  </code></pre>
  
  <h2>Result: pyXB exception:</h2>
  
  <pre><code>Unable to convert DOM node empty at [UNAVAILABLE] to binding
  Traceback (most recent call last):
    File "examples/export_test.py", line 130, in &lt;module&gt;
      att_value_xhtml = ATTRIBUTE_VALUE_XHTML(definition=text_attribute, THE_VALUE=XHTML_CONTENT(div=xhtml_div_type(_dom_node=dom_content)))
    File "/home/bvn/.pyenv/versions/3.6.10/lib/python3.6/site-packages/pyxb/binding/basis.py", line 2133, in __init__
      self.extend(dom_node.childNodes[:], fallback_namespace)
    File "/home/bvn/.pyenv/versions/3.6.10/lib/python3.6/site-packages/pyxb/binding/basis.py", line 2612, in extend
      [ self.append(_v, **kw) for _v in value_list ]
    File "/home/bvn/.pyenv/versions/3.6.10/lib/python3.6/site-packages/pyxb/binding/basis.py", line 2612, in &lt;listcomp&gt;
      [ self.append(_v, **kw) for _v in value_list ]
    File "/home/bvn/.pyenv/versions/3.6.10/lib/python3.6/site-packages/pyxb/binding/basis.py", line 2567, in append
      raise pyxb.UnrecognizedContentError(self, self.__automatonConfiguration, value, location)
  pyxb.exceptions_.UnrecognizedContentError: Invalid content empty (expect {http://www.w3.org/1999/xhtml}h1 or {http://www.w3.org/1999/xhtml}h2 or ...
  </code></pre>
  
  <p>What would be the correct way of handling the xhtml content?</p>