lxml公司xsi:schemaLocation命名空间URI验证问题

2024-06-26 01:44:36 发布

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

我试图使用lxml.etree来重现CDA QuickStart Guide found here中的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">

我使用的代码如下

^{pr2}$

问题出在nsmap中的schemaLocation条目。lxml似乎正在尝试验证该值并给出错误

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

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


Tags: org错误v3lxmlquickstartguideetreexsd
1条回答
网友
1楼 · 发布于 2024-06-26 01:44:36

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

相关问题 更多 >