-->

XMLStarles removes closing tag

2019-08-10 02:30发布

问题:

Having trouble with xmlstar. It's removes empty tags, how to disable this?

input:

<tag1>test1</tag1>
<tag3></tag3>
<tag4>test3</tag4>

output:

<tag1>test1</tag1>
<tag3/>
<tag4>test3</tag4>

And i want the same output so is's not removes the closing quote.

Hope someone has a quick answer.

回答1:

In XML, <a></a> and <a/> (note the /) are semantically identical. Whatever application is consuming the document should not care about this change.

I don't know anything about xmlstarlet in particular, but if its serializer doesn't have an option to produce the more verbose form you may need to postprocess the document.

If you absolutely need to former version, you have four choices: Find an XML serializer which will write out empty elements in that form (there may or may not be an option on the one you're using), modify an XML serializer to write out empty elements in that form, write your own XML serializer (not recommended; it's not very difficult but if you're asking this question there are details which will give you trouble), or postprocess the text after writing it out to change all instances of <a></a> to <a/>.

Actually, there's another possibility. If the reason your other program needs the long form is because it expects HTML, you should be using an HTML serializer rather than an XML serializer.



回答2:

I know it's the same. But the reason why i need is the tool there should reed this has not support for this format of xml. It needs the closing tag.

So I will be interested to here from anyone there have an idea.

The header off the file is. So its defiantly the tool reading the file.

<?xml version="1.0" encoding="iso-8859-1"?>

<project>
  <fileVersion>2</fileVersion>
  <configuration>
    <name>Debug</name>
    <toolchain>
      <name>ARM</name>
    </toolchain>