use*_*537 4 python xml lxml xml-namespaces cda
我正在尝试lxml.etree重现此处找到的CDA快速入门指南中的CDA示例.
特别是,我遇到了尝试重新创建此元素的命名空间问题.
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:mif="urn:hl7-org:v3/mif"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:hl7-org:v3 CDA.xsd">
Run Code Online (Sandbox Code Playgroud)
我正在使用的代码如下
root = etree.Element('ClinicalDocument',
nsmap={None: 'urn:hl7-org:v3',
'mif': 'urn:hl7-org:v3/mif',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'{http://www.w3.org/2001/XMLSchema-instance}schemaLocation': 'urn:hl7-org:v3 CDA.xsd'})
Run Code Online (Sandbox Code Playgroud)
问题在于schemaLocation进入nsmap.lxml似乎试图验证该值并给出错误
ValueError: Invalid namespace URI u'urn:hl7-org:v3 CDA.xsd'
Run Code Online (Sandbox Code Playgroud)
我是否schemaLocation错误地指定了值?有没有办法强制lxml接受任何字符串值?或者示例中的值是否只是一个占位符,我应该用其他东西替换?
nsmap是前缀到名称空间URI的映射.urn:hl7-org:v3 CDA.xsd是xsi:schemaLocation属性的有效值,但它不是有效的命名空间URI.
解决类似问题的方法,如何使用lxmf将命名空间包含到xml文件中?,也在这里工作.使用QName创建的xsi:schemaLocation属性.
from lxml import etree
attr_qname = etree.QName("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation")
root = etree.Element('ClinicalDocument',
{attr_qname: 'urn:hl7-org:v3 CDA.xsd'},
nsmap={None: 'urn:hl7-org:v3',
'mif': 'urn:hl7-org:v3/mif',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
})
Run Code Online (Sandbox Code Playgroud)