使用pyXB创建ReqIf XML文档时包含XHTML内容

2024-05-15 14:59:38 发布

您现在位置:Python中文网/ 问答频道 /正文

一点背景:在Sphinx需求管理插件的范围内,我正在研究导出ReqIF XML内容的方法。我找到了pyreqif,但发现它目前还不足以满足我们的需要

我决定研究一下pyXB生成的Reqif绑定,因为pyXB可以完成所有与XML之间的转换工作,我只需要担心添加一些方便的函数/类

可以在此处找到该项目:https://github.com/bavovanachte/reqif_pyxb_tryout

到目前为止,一切进展顺利:我已经成功地创建了所有对象的实例,它们很好地结合在一起,形成了一个xml文档。我唯一遇到的问题是XHTML内容的创建。理想情况下,我希望获取现有的html内容并将其插入到树中。 naieve的这种做法导致xml不安全字符被转义,因此无法工作

以下是我的一些尝试:

尝试1:将xml作为字符串传递给XHTML_内容构造函数

xml_string = '''
<div>
    XY Block Adapter shall translate the Communication to TMN-Block in a bidirectional manner and support all functionalities of a TMN-Block.<br/>
</div>'''
att_value_xhtml = ATTRIBUTE_VALUE_XHTML(definition=text_attribute, THE_VALUE=XHTML_CONTENT(div=xml_string))

结果:转义的XML内容:

&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;</ns2:div>

尝试2:将xml作为字符串传递给XHTML_内容构造函数,并设置“_from_xml标志”

xml_string = '''
<div>
    XY Block Adapter shall translate the Communication to TMN-Block in a bidirectional manner and support all functionalities of a TMN-Block.<br/>
</div>'''
att_value_xhtml = ATTRIBUTE_VALUE_XHTML(definition=text_attribute, THE_VALUE=XHTML_CONTENT(xml_string, _from_xml=True))

结果:pyXB异常:

 Traceback (most recent call last):
   File "examples/export_test.py", line 105, in <module>
     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 <listcomp>
     [ 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

尝试3-将xml作为字符串传递给xhtml_div_类型构造函数,并带有“_from_xml flag set”,然后将该类分配给div成员

xml_string = '''
<div>
    XY Block Adapter shall translate the Communication to TMN-Block in a bidirectional manner and support all functionalities of a TMN-Block.<br/>
</div>'''
att_value_xhtml = ATTRIBUTE_VALUE_XHTML(definition=text_attribute, THE_VALUE=XHTML_CONTENT(div=xhtml_div_type(xml_string, _from_xml=True)))

结果:转义的XML内容:

&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;</ns2:div>

尝试4-首先将字符串转换为dom并在构造函数中使用它

xml_string = '''
<div>
    XY Block Adapter shall translate the Communication to TMN-Block in a bidirectional manner and support all functionalities of a TMN-Block.<br/>
</div>'''
dom_content = xml.dom.minidom.parseString('<myxml>Some data<empty/> some more data</myxml>')
att_value_xhtml = ATTRIBUTE_VALUE_XHTML(definition=text_attribute, THE_VALUE=XHTML_CONTENT(div=xhtml_div_type(_dom_node=dom_content)))

结果:pyXB异常:

Unable to convert DOM node empty at [UNAVAILABLE] to binding
Traceback (most recent call last):
  File "examples/export_test.py", line 130, in <module>
    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 <listcomp>
    [ 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 ...

处理xhtml内容的正确方法是什么


Tags: inpyselfdivvaluelinexmlblock