python 命名空间内的 lxml 属性命名空间
python lxml attribute namespaces within namespaces
在我查看的文件上传文档中,xml 文件的开头需要这个特定标记:
<oclcPersonas xmlns="http://worldcat.org/xmlschemas/IDMPersonas-2.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd">
我正在尝试使用 lxml.etree 库来解决这个问题。
我见过的许多示例都有一个初始级别的名称space,用于覆盖 xmlns:xsi
部分的属性:
namespace_map = {
None: persona_namespace,
'xsi': "http://www.w3.org/2001/XMLSchema-instance"}
但是第二部分出现了 2 个问题xsi:schemaLocation
1) 我如何使用 lxml 完成二级名称 space?
2) 如何允许名称spaces 包含 space 而不会收到错误消息 (http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd
)
在您的示例 XML 中,您有:
- 默认命名空间中的根元素
oclcPersonas
,
- 默认命名空间:“http://worldcat.org/xmlschemas/IDMPersonas-2.2”,
- “xsi”命名空间:“http://www.w3.org/2001/XMLSchema-instance”,
- “xsi”命名空间中的“schemaLocation”属性。
要使用 lxml 构建此元素,您需要使用花括号(James Clark 表示法)的完全限定名称。
首先定义一个字典来存储你的命名空间映射:
# your namespaces
p_url = "http://worldcat.org/xmlschemas/IDMPersonas-2.2"
xsi_url = "http://www.w3.org/2001/XMLSchema-instance"
NS = {None: p_url, 'xsi': xsi_url}
为了构建完全限定的命名空间,您可以定义前缀:
# tag prefixes
P = '{' + p_url + '}'
XSI = '{' + xsi_url + '}'
然后您可以定义标签名称和属性:
# tag names and attributes
root_tag = P + 'oclcPersonas'
attrs = {
XSI + 'schemaLocation':
"http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd"
}
可以像下面这样创建根元素:
# element
root = etree.Element(root_tag, attrib=attrs, nsmap=NS)
你应该有你的元素:
print(etree.tounicode(root))
回答你的问题:
2) How do I allow the namespaces to contain a space without receiving an error (http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd
)
这不是命名空间,这是 schemaLocation
属性的值。一个简单的字符串。
在我查看的文件上传文档中,xml 文件的开头需要这个特定标记:
<oclcPersonas xmlns="http://worldcat.org/xmlschemas/IDMPersonas-2.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd">
我正在尝试使用 lxml.etree 库来解决这个问题。
我见过的许多示例都有一个初始级别的名称space,用于覆盖 xmlns:xsi
部分的属性:
namespace_map = {
None: persona_namespace,
'xsi': "http://www.w3.org/2001/XMLSchema-instance"}
但是第二部分出现了 2 个问题xsi:schemaLocation
1) 我如何使用 lxml 完成二级名称 space?
2) 如何允许名称spaces 包含 space 而不会收到错误消息 (http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd
)
在您的示例 XML 中,您有:
- 默认命名空间中的根元素
oclcPersonas
, - 默认命名空间:“http://worldcat.org/xmlschemas/IDMPersonas-2.2”,
- “xsi”命名空间:“http://www.w3.org/2001/XMLSchema-instance”,
- “xsi”命名空间中的“schemaLocation”属性。
要使用 lxml 构建此元素,您需要使用花括号(James Clark 表示法)的完全限定名称。
首先定义一个字典来存储你的命名空间映射:
# your namespaces
p_url = "http://worldcat.org/xmlschemas/IDMPersonas-2.2"
xsi_url = "http://www.w3.org/2001/XMLSchema-instance"
NS = {None: p_url, 'xsi': xsi_url}
为了构建完全限定的命名空间,您可以定义前缀:
# tag prefixes
P = '{' + p_url + '}'
XSI = '{' + xsi_url + '}'
然后您可以定义标签名称和属性:
# tag names and attributes
root_tag = P + 'oclcPersonas'
attrs = {
XSI + 'schemaLocation':
"http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd"
}
可以像下面这样创建根元素:
# element
root = etree.Element(root_tag, attrib=attrs, nsmap=NS)
你应该有你的元素:
print(etree.tounicode(root))
回答你的问题:
2) How do I allow the namespaces to contain a space without receiving an error (
http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd
)
这不是命名空间,这是 schemaLocation
属性的值。一个简单的字符串。