Stephen Nutt
Stephen Nutt

Reputation: 3306

XML empty tag being converted to open and closing tag pair

I have a large XML master configuration file that contains details for a a number of inter connected applications. Much of the configuration is common to all applications but some pieces differ, such as IP addresses, database connection strings etc.

I've enclosed the elements specific to a particular application within a conditional element and an attribute is used to define which application the nested elements are used for. This allows me to transform the master configuration file to generate the required application specific config file.

The problem I'm having is the output XML has empty tag elements of the form

<add key="Setting3" value="1.2.3.4" />

changed to

<add key="Setting3" value="1.2.3.4">
</add>

While I understand this should not matter, it makes it harder to diff the resulting XML file with the current app specific files which were all created by hand. Is there something I can do to my xslt that will keep the empty tags unchanged in the results?

My xslt is as follows

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="app" select="''"></xsl:param>
  <xsl:output method="xml" indent="yes" version="1.0" encoding="utf-8"/>

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

  <xsl:template match="conditional">
    <xsl:if test="starts-with(@app,$app))">
      <xsl:copy-of select="*|@*|comment()|processing-instruction()"/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

A sample master config file is as follows

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- Common Settings for all applications -->
  <add key="Setting1" value="1000" />
  <add key="Setting2" value="600" />

  <conditional app="App1">
    <!-- App1 Settings -->
    <add key="Setting3" value="1.2.3.4" />
  </conditional>

  <conditional app="App2">
      <!-- App2 Settings -->
      <add key="Setting3" value="1.3.10.10" />
  </conditional>
</configuration>

Thanks

Edit:

Changing the xml:output as Martin suggested improves the output as the closing tag is now appended to the opening tag on the same line, but I'd still like to get it to keep the empty tag formatting of the source XML file.

So

<add key="Setting3" value="1.2.3.4" />

becomes

<add key="Setting3" value="1.2.3.4"></add>

I am using Microsoft's msxml.exe version 3.0 as follows

msxml.exe master.config config.xslt app=App1 > App1.config
msxml.exe master.config config.xslt app=App2 > App2.config

If there is a better processor I suspect it would be fine for me to switch to any other freely available processor that runs on Windows.

Upvotes: 1

Views: 3239

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167516

I think the problem is not with the XSLT you have shown (although you could try whether using <xsl:output method="xml" indent="no" version="1.0" encoding="utf-8"/> improves things) but rather with the way you run the transformation with your XSLT processor. So consider to tell us which XSLT processor you use and if you have code running the transformation to show us the code.

Upvotes: 1

Related Questions