简体   繁体   English

lxml xsi:schemaLocation名称空间URI验证问题

[英]lxml xsi:schemaLocation namespace URI validation issue

I'm trying to use lxml.etree to reproduce the CDA example found in the CDA QuickStart Guide found here . 我正在尝试使用lxml.etree来重现此处找到的CDA快速入门指南中的CDA示例。

In particular, I'm running into issues with namespaces trying to recreate this element. 特别是,我遇到了尝试重新创建此元素的命名空间问题。

<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">

The code I'm using is as follows 我正在使用的代码如下

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

The problem is with the schemaLocation entry in nsmap . 问题在于nsmapschemaLocation条目。 lxml appears to be trying to validate the value and gives the error lxml似乎试图验证该值并给出错误

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

Am I specifying the schemaLocation value incorrectly? 我是否错误地指定了schemaLocation值? Is there a way to force lxml to accept any string value? 有没有办法强制lxml接受任何字符串值? Or is the value in the example simply intended to be a placeholder that I am supposed to replace with something else? 或者示例中的值是否只是一个占位符,我应该用其他东西替换?

nsmap is a mapping of prefixes to namespace URIs. nsmap是前缀到名称空间URI的映射。 urn:hl7-org:v3 CDA.xsd is a valid value for the xsi:schemaLocation attribute, but it is not a valid namespace URI. urn:hl7-org:v3 CDA.xsdxsi:schemaLocation属性的有效值,但它不是有效的命名空间URI。

The solution to a similar question, How to include the namespaces into a xml file using lxmf? 解决类似问题的方法, 如何使用lxmf将命名空间包含到xml文件中? , works here too. ,也在这里工作。 Use QName to create the xsi:schemaLocation attribute. 使用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',
                            })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM