1

I am applying an XSL-T file xsltUri to an XML file TargetXmlFile using the XslCompiledTransform class:

XslCompiledTransform xslTransform = new XslCompiledTransform(false);
xslTransform.Load(xsltUri);

using (var outStream = new MemoryStream())
{
    var writer = new StreamWriter(outStream, new UTF8Encoding());
    using (var reader = new XmlTextReader(TargetXmlFileName)
    {
        WhitespaceHandling = WhitespaceHandling.All,
        DtdProcessing = DtdProcessing.Ignore
    })
    {
        xslTransform.Transform(reader, xsltArguments, writer);
    }

    outStream.Position = 0;
    using (FileStream outFile = new FileStream(outputFileName, FileMode.Create))
    {
        outStream.CopyTo(outFile);
    }
}

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <element
    id="1"
    attr1="value11"
    attr2="value12"/>
  <element    id="2"    attr1="value21"    attr2="value22"/>
</root>

Input XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

  <xsl:template match="//element[@id='2']/@attr1">
    <xsl:attribute name="attr1">
      <xsl:value-of select="'newvalue21'"/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

Actual output XML:

<?xml version="1.0" encoding="utf-8"?><root>
  <element id="1" attr1="value11" attr2="value12" />
  <element id="2" attr1="newvalue21" attr2="value22" />
</root>

Desired output XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <element
    id="1"
    attr1="value11"
    attr2="value12"/>
  <element    id="2"    attr1="newvalue21"    attr2="value22"/>
</root>

Question: How can I preserve the whitespace (particularly, line breaks) of the input XML file within the "element" tags in the output XML file? I have experimented with different options, but nothing worked for this case.

Thanks for any hints!

blackcomb
  • 59
  • 6
  • Preserving the whitespace between attributes seems to be impossible with default methods according to [this](http://stackoverflow.com/questions/8853752/preserve-whitespace-between-attributes-in-xslt-transformation?rq=1)... If it is, maybe it is at least possible to avoid having the root element in the same line as the XML Header? – blackcomb Jun 25 '14 at 15:49

2 Answers2

2

This has nothing to do with XSLT. The whitespace you're referring to does not exist in the XML document model, and it cannot be made significant to a conformant XML processor, even with xml:space="preserve". There is no place for it in the DOM, and it will be skipped by the reader; as such there is no way to copy it to the writer. You would have to emit the XML with custom code (in other words, not with an XmlWriter).

harpo
  • 41,820
  • 13
  • 96
  • 131
1

The internal formatting of a tag (whitespace between attributes) is completely ephemeral in XML.

  1. As far as XML documents are concerned, it does not exist.
  2. As far as XML parsers are concerned, it is ignored, because 1). The only exception is that whitespace is illegal immediately after a <.
  3. As far as XML serializers are concerned, they can do what they want, because 1) and 2). Most (if not all) will use a single space character to separate attributes from each other.

So...

  • Don't try to build an application that depends on the source code layout of XML.
  • Since this kind of source code layout in XML is technically irrelevant… get over your OCD. ;)
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    Thank you for the clarification. Btw, I don't need the whitespace for an application, but for (technical) users who might manually edit the XML and typically prefer a readable formatting ;) – blackcomb Jun 26 '14 at 11:29
  • I thought so. But I don't see any option other than post-processing the file, which is possible but probably not worth the effort. – Tomalak Jun 26 '14 at 11:46
  • This is all true, but it is also true that it can make a mess of xml based configuration files that have longer attribute values. – StingyJack Aug 30 '18 at 01:47
  • @StingyJack No, because of 3). Write an XML serializer that does what you prefer. (On the other hand, if you have this problem, chances are that your attributes should actually be elements.) – Tomalak Aug 30 '18 at 06:58