Search code examples
xmllint

Trim whitespace inside tags?


I'm using xmllint to re-format some xml. I notice it seems to leave enclosed text alone. For example, this

<tag>
<p>
<i>
test
</i>
</p>
</tag>

becomes this

<tag>
    <p>
        <i>
test
</i>
    </p>
</tag>

Is there a way to force it to put the text right after the opening tag and follow that immediately with the closing tag? Ideally I'd like something like this:

<tag>
    <p>
        <i>test</i>
    </p>
</tag>

I suppose that amounts to having it ignore leading and trailing whitespace inside tags. I can do it with some preprocessing via regex, but is there any native way?

TIA.


Solution

  • You should be able to use tidy for this:

    echo '<tag>
    <p>
    <i>
    test
    </i>
    </p>
    </tag>' |    tidy -xml -iq
    

    Output:

    <tag>
      <p>
        <i>test</i>
      </p>
    </tag>