Daryl
Daryl

Reputation: 3343

Changing empty XML elements to elements with self-closing tags

I have a few (~5) large (~1000 lines) XML files with many elements like this:

<Foo Bar="Baz">
</Foo >

I want them all to become like this:

<Foo Bar="Baz" />

Is there a tool or script that will do this for me automatically? Note: some elements aren't empty and shouldn't be changed to self-closing tags; there are only about 5 distinct element names I care about.

Upvotes: 0

Views: 1773

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

If you have xsltproc or another XSLT processor then this should be quite straightforward. Now the example element you show in the question isn't actually empty because it contains a text node (the newline character). But XSLT allows you to specify elements within which whitespace-only text nodes can be ignored. Thus a style sheet like this

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:strip-space elements="Foo Bar Baz"/>

  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
  </xsl:template>
</xsl:stylesheet>

should do what you need. The strip-space tells it which elements should have their whitespace-only text node children stripped, and the template is an identity transform that copies input to output unchanged (after the whitespace stripping, which happens at parse time). Once the elements really are empty the serializer should write them out as self-closed tags.

Upvotes: 1

Related Questions