Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT Xml output is stripping whitespace from closing slash

The XSLT XML output format is stripping the whitespace before the closing tag

<Import Include="System.Web" /> becomes <Import Include="System.Web"/>

As the XSLT is also removing many nodes in the documents it's applied to, I would like to strip whitespace except in the case of the closing slash.

The xslt is being applied to many xml ms proj files

template.xsl;

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="http://schemas.microsoft.com/developer/msbuild/2003">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <!--<xsl:preserve-space elements="text"/>-->

    <xsl:template match='@*|node()'>
        <xsl:copy>
            <xsl:apply-templates select='@*|node()'/>
        </xsl:copy>
    </xsl:template>
    ... 
    ...
</xsl:stylesheet>
like image 396
Thermionix Avatar asked Oct 08 '22 13:10

Thermionix


2 Answers

This is a serialization detail that is impossible to control from XSLT.

Any reliable way to achieve it would be to run a post-processor on the result of the transformation.

like image 160
Dimitre Novatchev Avatar answered Oct 26 '22 20:10

Dimitre Novatchev


I ended up using gnuwin32 sed to add a space (note the ^> : ^ is required to escape the angled bracket)

sed\sed.exe -i "s/\"\/^>/\" \/>/g" %outfile%
like image 36
Thermionix Avatar answered Oct 26 '22 21:10

Thermionix