简体   繁体   English

Python:在 lxml 中添加命名空间

[英]Python: adding namespaces in lxml

I'm trying to specify a namespace using lxml similar to this example (taken from here ):我正在尝试使用类似于此示例的lxml指定命名空间(取自此处):

<TreeInventory xsi:noNamespaceSchemaLocation="Trees.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</TreeInventory>

I'm not sure how to add the Schema instance to use and also the Schema location.我不确定如何添加要使用的 Schema 实例以及 Schema 位置。 The documentation got me started, by doing something like:文档通过执行以下操作让我开始:

>>> NS = 'http://www.w3.org/2001/XMLSchema-instance'
>>> TREE = '{%s}' % NS
>>> NSMAP = {None: NS}
>>> tree = etree.Element(TREE + 'TreeInventory', nsmap=NSMAP)
>>> etree.tostring(tree, pretty_print=True)
'<TreeInventory xmlns="http://www.w3.org/2001/XMLSchema-instance"/>\n'

I'm not sure how to specify it an instance though, and then also specify a location.我不确定如何将它指定为一个实例,然后还指定一个位置。 It seems like this can be done with the nsmap keyword-arg in etree.Element , but I don't see how.似乎这可以使用nsmap关键字参数来etree.Element ,但我不知道如何。

In some more steps, for clarity:在更多步骤中,为了清楚起见:

>>> NS = 'http://www.w3.org/2001/XMLSchema-instance'

As far as I can see, it is the attribute noNamespaceSchemaLocation that you want namespaced, not the TreeInventory element.据我所知,您想要命名空间的是属性noNamespaceSchemaLocation ,而不是TreeInventory元素。 So:所以:

>>> location_attribute = '{%s}noNamespaceSchemaLocation' % NS    # f-string doesn't work in this case
>>> elem = etree.Element('TreeInventory', attrib={location_attribute: 'Trees.xsd'})
>>> etree.tostring(elem, pretty_print=True)
'<TreeInventory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Trees.xsd"/>\n'

This looks like what you wanted... You could of course also create the element first, without attributes, and then set the attribute, like this:这看起来像你想要的......你当然也可以先创建元素,没有属性,然后设置属性,如下所示:

>>> elem = etree.Element('TreeInventory')
>>> elem.set(location_attribute, 'Trees.xsd')

As for the nsmap parameter: I believe it is only used to define which prefixes to use on serialization.至于nsmap参数:我相信它仅用于定义在序列化时使用哪些前缀。 In this case, it is not needed, because lxml knows the commonly used prefix for the namespace in question is "xsi".在这种情况下,它是不需要的,因为 lxml 知道所讨论的命名空间的常用前缀是“xsi”。 If it were not some well-known namespace, you would probably see prefixes like "ns0", "ns1" etc..., unless you specified which prefix you preferred.如果它不是某个知名的命名空间,您可能会看到诸如“ns0”、“ns1”等前缀,除非您指定了您喜欢的前缀。 (remember: the prefix is not supposed to matter) (记住:前缀不重要)

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

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