在lxml中关闭一个没有文字的标签(Close a tag with no text in lxml)

编程入门 行业动态 更新时间:2024-10-26 05:28:33
在lxml中关闭一个没有文字的标签(Close a tag with no text in lxml)

我正尝试使用Python和lxml输出一个XML文件

但是,我注意到一件事,如果一个标签没有文本,它不会自动关闭。 一个例子是:

root = etree.Element('document') rootTree = etree.ElementTree(root) firstChild = etree.SubElement(root, 'test')

这个输出是:

<document> <test/> </document

我想要的输出是:

<document> <test> </test> </document>

所以基本上我想关闭一个没有文本但用于属性值的标签。 我怎么做? 而且,这个标签叫什么? 我会谷歌搜索它,但我不知道如何搜索它。

I am trying to output a XML file using Python and lxml

However, I notice one thing that if a tag has no text, it does not close itself. An example of this would be:

root = etree.Element('document') rootTree = etree.ElementTree(root) firstChild = etree.SubElement(root, 'test')

The output of this is:

<document> <test/> </document

I want the output to be:

<document> <test> </test> </document>

So basically I want to close a tag which has no text, but is used to the attribute value. How do I do that? And also, what is such a tag called? I would have Googled it, but I don't know how to search for it.

最满意答案

请注意, <test></test>和<test/>含义完全相同。 你想要的是测试标签实际上有一个包含单行换行的文本。 然而,一个没有文本的空标记通常被写为<test/> ,坚持它显示为<test></test>是没有意义的。

Note that <test></test> and <test/> mean exactly the same thing. What you want is for the test-tag to actually do have a text that consists in a single linebreak. However, an empty tag with no text is usually written as <test/> and it makes very little sense to insist on it to appear as <test></test>.

更多推荐

本文发布于:2023-07-18 04:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154938.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文字   标签   lxml   text   tag

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!