5

我试图用来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">

我正在使用的代码如下

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'})

问题schemaLocation在于nsmap. lxml似乎正在尝试验证该值并给出错误

ValueError: Invalid namespace URI u'urn:hl7-org:v3 CDA.xsd'

我是否schemaLocation错误地指定了值?有没有办法强制lxml接受任何字符串值?还是示例中的值只是为了成为我应该用其他东西替换的占位符?

4

1 回答 1

15

nsmap是前缀到命名空间 URI 的映射。urn:hl7-org:v3 CDA.xsdxsi:schemaLocation属性的有效值,但不是有效的命名空间 URI。

类似问题的解决方案,How to include the namespaces into a xml file using lxmf? ,也在这里工作。用于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',
                            })
于 2017-09-27T15:51:22.867 回答