python lxml 我如何在项目名称中使用标签?
python lxml how i use tag in items name?
我需要使用项目的特殊名称构建 xml 文件,这是我当前的代码:
from lxml import etree
import lxml
from lxml.builder import E
wp = E.wp
tmp = wp("title")
print(etree.tostring(tmp))
当前输出是这样的:
b'<wp>title</wp>'
我想成为:
b'<wp:title>title</title:wp>'
我如何创建名称如下的项目:wp:title
?
您将 namespace prefix wp
与标签名称混淆了。名称空间前缀是名称空间 URI 的文档本地名称。 wp:title
要求解析器查找 xmlns:wp="..."
属性以查找名称空间本身(通常是 URL 但任何全局唯一字符串都可以),可以是在标签本身上,也可以在父标签上.这将标签连接到一个唯一值,而不会使标签名称过于冗长而无法输入或阅读。
您需要向元素生成器对象提供命名空间和可选的命名空间映射(将短名称映射到完整命名空间名称)。提供的默认 E
对象没有命名空间或命名空间映射集。我将在这里假设 wp
是 http://wordpress.org/export/1.2/
Wordpress 命名空间,因为这似乎是最有可能的,尽管也可能是您正在尝试发送 Windows Phone notifications.
不要使用默认的 E
元素生成器,而是创建您自己的 ElementMaker
实例并传递一个 namespace
参数来告诉 lxml
什么 URL该元素属于。要在您的元素名称上获得正确的前缀,您还需要给它一个 nsmap
字典,将前缀映射到 URLs:
from lxml.builder import ElementMaker
namespaces = {"wp": "http://wordpress.org/export/1.2/"}
E = ElementMaker(namespace=namespaces["wp"], nsmap=namespaces)
title = E.title("Value of the wp:title tag")
这会生成一个具有正确前缀 和 xmlns:wp
属性的标签:
>>> from lxml.builder import ElementMaker
>>> namespaces = {"wp": "http://wordpress.org/export/1.2/"}
>>> E = ElementMaker(namespace=namespaces["wp"], nsmap=namespaces)
>>> title = E.title("Value of the wp:title tag")
>>> etree.tostring(title, encoding="unicode")
'<wp:title xmlns:wp="http://wordpress.org/export/1.2/">Value of the wp:title tag</wp:title>'
您可以省略 nsmap
值,但您希望在文档的 parent 元素上有这样的映射。在这种情况下,您可能希望为每个需要支持的名称空间创建单独的 ElementMaker
对象,并将 nsmap
名称空间映射放在最外层的元素上。在写出文档时,lxml
然后在整个过程中使用短名称。
例如,创建 Wordpress WXR format document 需要多个命名空间:
from lxml.builder import ElementMaker
namespaces = {
"excerpt": "https://wordpress.org/export/1.2/excerpt/",
"content": "http://purl.org/rss/1.0/modules/content/",
"wfw": "http://wellformedweb.org/CommentAPI/",
"dc": "http://purl.org/dc/elements/1.1/",
"wp": "https://wordpress.org/export/1.2/",
}
RootElement = ElementMaker(nsmap=namespaces)
ExcerptElement = ElementMaker(namespace=namespaces["excerpt"])
ContentElement = ElementMaker(namespace=namespaces["content"])
CommentElement = ElementMaker(namespace=namespaces["wfw"])
DublinCoreElement = ElementMaker(namespace=namespaces["dc"])
ExportElement = ElementMaker(namespace=namespaces["wp"])
然后您将使用
构建文档
doc = RootElement.rss(
RootElement.channel(
ExportElement.wxr_version("1.2"),
# etc. ...
),
version="2.0"
)
当用 etree.tostring(doc, pretty_print=True, encoding="unicode")
漂亮地打印时,会产生:
<rss xmlns:excerpt="https://wordpress.org/export/1.2/excerpt/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="https://wordpress.org/export/1.2/" version="2.0">
<channel>
<wp:wxr_version>1.2</wp:wxr_version>
</channel>
</rss>
注意只有根 <rss>
元素有 xmlns
属性,以及 <wp:wxr_version>
标签如何使用正确的前缀,即使我们只给它命名空间 URI。
举一个不同的例子,如果你正在构建一个 Windows Phone 磁贴通知,它会更简单。毕竟,只有一个命名空间可供使用:
from lxml.builder import ElementMaker
namespaces = {"wp": "WPNotification"}
E = ElementMaker(namespace=namespaces["wp"], nsmap=namespaces)
notification = E.Notification(
E.Tile(
E.BackgroundImage("https://example.com/someimage.png"),
E.Count("42"),
E.Title("The notification title"),
# ...
)
)
产生
<wp:Notification xmlns:wp="WPNotification">
<wp:Tile>
<wp:BackgroundImage>https://example.com/someimage.png</wp:BackgroundImage>
<wp:Count>42</wp:Count>
<wp:Title>The notification title</wp:Title>
</wp:Tile>
</wp:Notification>
只有最外层的元素 <wp:Notification>
现在具有 xmlns:wp
属性。所有其他元素只需要包含 wp:
前缀。
请注意,使用的前缀完全取决于您,甚至可选。命名空间 URI 是跨不同 XML 文档唯一标识元素的真正关键。如果您改为使用 E = ElementMaker(namespace="WPNotification", nsmap={None: "WPNotification"})
,并因此生成了一个带有 <Notification xmlns="WPNotification">
的顶级元素,您仍然拥有一个完全合法的 XML 文档,根据 XML 标准,该文档具有完全相同的意思。
我需要使用项目的特殊名称构建 xml 文件,这是我当前的代码:
from lxml import etree
import lxml
from lxml.builder import E
wp = E.wp
tmp = wp("title")
print(etree.tostring(tmp))
当前输出是这样的:
b'<wp>title</wp>'
我想成为:
b'<wp:title>title</title:wp>'
我如何创建名称如下的项目:wp:title
?
您将 namespace prefix wp
与标签名称混淆了。名称空间前缀是名称空间 URI 的文档本地名称。 wp:title
要求解析器查找 xmlns:wp="..."
属性以查找名称空间本身(通常是 URL 但任何全局唯一字符串都可以),可以是在标签本身上,也可以在父标签上.这将标签连接到一个唯一值,而不会使标签名称过于冗长而无法输入或阅读。
您需要向元素生成器对象提供命名空间和可选的命名空间映射(将短名称映射到完整命名空间名称)。提供的默认 E
对象没有命名空间或命名空间映射集。我将在这里假设 wp
是 http://wordpress.org/export/1.2/
Wordpress 命名空间,因为这似乎是最有可能的,尽管也可能是您正在尝试发送 Windows Phone notifications.
不要使用默认的 E
元素生成器,而是创建您自己的 ElementMaker
实例并传递一个 namespace
参数来告诉 lxml
什么 URL该元素属于。要在您的元素名称上获得正确的前缀,您还需要给它一个 nsmap
字典,将前缀映射到 URLs:
from lxml.builder import ElementMaker
namespaces = {"wp": "http://wordpress.org/export/1.2/"}
E = ElementMaker(namespace=namespaces["wp"], nsmap=namespaces)
title = E.title("Value of the wp:title tag")
这会生成一个具有正确前缀 和 xmlns:wp
属性的标签:
>>> from lxml.builder import ElementMaker
>>> namespaces = {"wp": "http://wordpress.org/export/1.2/"}
>>> E = ElementMaker(namespace=namespaces["wp"], nsmap=namespaces)
>>> title = E.title("Value of the wp:title tag")
>>> etree.tostring(title, encoding="unicode")
'<wp:title xmlns:wp="http://wordpress.org/export/1.2/">Value of the wp:title tag</wp:title>'
您可以省略 nsmap
值,但您希望在文档的 parent 元素上有这样的映射。在这种情况下,您可能希望为每个需要支持的名称空间创建单独的 ElementMaker
对象,并将 nsmap
名称空间映射放在最外层的元素上。在写出文档时,lxml
然后在整个过程中使用短名称。
例如,创建 Wordpress WXR format document 需要多个命名空间:
from lxml.builder import ElementMaker
namespaces = {
"excerpt": "https://wordpress.org/export/1.2/excerpt/",
"content": "http://purl.org/rss/1.0/modules/content/",
"wfw": "http://wellformedweb.org/CommentAPI/",
"dc": "http://purl.org/dc/elements/1.1/",
"wp": "https://wordpress.org/export/1.2/",
}
RootElement = ElementMaker(nsmap=namespaces)
ExcerptElement = ElementMaker(namespace=namespaces["excerpt"])
ContentElement = ElementMaker(namespace=namespaces["content"])
CommentElement = ElementMaker(namespace=namespaces["wfw"])
DublinCoreElement = ElementMaker(namespace=namespaces["dc"])
ExportElement = ElementMaker(namespace=namespaces["wp"])
然后您将使用
构建文档doc = RootElement.rss(
RootElement.channel(
ExportElement.wxr_version("1.2"),
# etc. ...
),
version="2.0"
)
当用 etree.tostring(doc, pretty_print=True, encoding="unicode")
漂亮地打印时,会产生:
<rss xmlns:excerpt="https://wordpress.org/export/1.2/excerpt/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="https://wordpress.org/export/1.2/" version="2.0">
<channel>
<wp:wxr_version>1.2</wp:wxr_version>
</channel>
</rss>
注意只有根 <rss>
元素有 xmlns
属性,以及 <wp:wxr_version>
标签如何使用正确的前缀,即使我们只给它命名空间 URI。
举一个不同的例子,如果你正在构建一个 Windows Phone 磁贴通知,它会更简单。毕竟,只有一个命名空间可供使用:
from lxml.builder import ElementMaker
namespaces = {"wp": "WPNotification"}
E = ElementMaker(namespace=namespaces["wp"], nsmap=namespaces)
notification = E.Notification(
E.Tile(
E.BackgroundImage("https://example.com/someimage.png"),
E.Count("42"),
E.Title("The notification title"),
# ...
)
)
产生
<wp:Notification xmlns:wp="WPNotification">
<wp:Tile>
<wp:BackgroundImage>https://example.com/someimage.png</wp:BackgroundImage>
<wp:Count>42</wp:Count>
<wp:Title>The notification title</wp:Title>
</wp:Tile>
</wp:Notification>
只有最外层的元素 <wp:Notification>
现在具有 xmlns:wp
属性。所有其他元素只需要包含 wp:
前缀。
请注意,使用的前缀完全取决于您,甚至可选。命名空间 URI 是跨不同 XML 文档唯一标识元素的真正关键。如果您改为使用 E = ElementMaker(namespace="WPNotification", nsmap={None: "WPNotification"})
,并因此生成了一个带有 <Notification xmlns="WPNotification">
的顶级元素,您仍然拥有一个完全合法的 XML 文档,根据 XML 标准,该文档具有完全相同的意思。