2016-09-21 87 views
3

我试图使用lxml.etree来重现CDA QuickStart Guide found here中的CDA示例。lxml xsi:schemaLocation名称空间URI验证问题

特别是,我遇到了命名空间试图重新创建这个元素的问题。

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

的问题是,在nsmapschemaLocation条目。 lxml似乎试图验证的值,并给出了错误

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

我是否指定schemaLocation值不正确?有没有办法强制lxml接受任何字符串值?或者,这个例子中的值是否仅仅是一个占位符,我应该用其他东西代替?

回答

2

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', 
          }) 
+0

感谢您挖掘它,我放弃了寻找答案。 – user3419537