5

Input XML:

<foobar>
          <Comments>
            Reported By:   L &amp; A Q  TESTING, TESTED
            Date of TESTING:   Available
            TESTING  unavailable to resolve Test issue.
            Additional  Comments: Comments

            Had to go   into Testing System and change to the correct notification group. Per sup.
          </Comments>
</foobar>

XSLT Code:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="no" omit-xml-declaration="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space()" />
    </xsl:template>
    <xsl:template match="text()[../*]"/>
</xsl:stylesheet>

Expected output:

<foobar><Comments>Reported By: L &amp; A Q TESTING, TESTED Date of TESTING: Available TESTING unavailable to resolve Test issue. Additional Comments: Comments Had to go into Testing System and change to the correct notification group. Per sup.</Comments></foobar>

What I am getting:

<foobar>
  <Comments>Reported By: L &amp; A Q TESTING, TESTED Date of TESTING: Available TESTING unavailable to resolve Test issue. Additional Comments: Comments Had to go into Testing System and change to the correct notification group. Per sup.</Comments>
</foobar>

Observation:

Though unnecessary whitespace from text() nodes has been rectified .. Still there is indentation in output XML.

Ideally strip-space should take care of it.. on top of it I have added below code

<xsl:template match="text()[../*]"/>

Still no luck!! Usage of

XPathDocument xpathXmlOrig = new XPathDocument(string_xmlInput); in my C# code errors out saying .. strip-space cannot be applied to document which has already been loaded!! So I am using XMLReader ..

Adding C# code for reference..

        XslCompiledTransform xslTransform = new XslCompiledTransform();
        string xslinput = "<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:strip-space elements=\"*\"/><xsl:output indent=\"no\" omit-xml-declaration=\"yes\"/><xsl:template match=\"@*|node()\"><xsl:copy><xsl:apply-templates select=\"@*|node()\"/></xsl:copy></xsl:template><xsl:template match=\"text()[not(../*)]\"><xsl:value-of select=\"normalize-space()\" /></xsl:template><xsl:template match=\"text()[../*]\"/></xsl:stylesheet>";

        string strXmlOutput = string.Empty;
        StringWriter swXmlOutput = null;
        MemoryStream objMemoryStream = null;
        swXmlOutput = new StringWriter();
        objMemoryStream = new MemoryStream();

        UTC_Calc obj = new UTC_Calc();
        XsltArgumentList xslArg = new XsltArgumentList();
        ..........
        ........
        XmlReader reader = XmlReader.Create(string_xmlInput, settings);

        XsltSettings xslsettings = new XsltSettings(false, true);
        MemoryStream memStream = new MemoryStream();

        XmlReader rd = XmlReader.Create(new StringReader(xslinput));

        xslTransform.Load(rd);
        xslTransform.Transform(reader, xslArg, objMemoryStream);
        objMemoryStream.Position = 0;
        StreamReader objStreamReader = new StreamReader(objMemoryStream);
        strXmlOutput = objStreamReader.ReadToEnd();
        ..........
        ........

        XmlDocument outputxml = new XmlDocument();
        outputxml.LoadXml(strXmlOutput);
        outputxml.Save(outputfile.FileName);
4
  • 1
    +1 for a pretty comprehensive set of data. Commented Feb 28, 2013 at 9:12
  • Thank you for appreciation.. Commented Feb 28, 2013 at 9:13
  • 1
    For what it's worth, I tried your stylesheet with both xsltproc and Saxon (9.1.0.5J) and in both cases, the output is your expected output. Commented Feb 28, 2013 at 9:44
  • Yes it works on Apache Xalan, Saxon .. even xslt debugging in visual studio .. but not when I invoke in code .. headbanging puzzle Commented Feb 28, 2013 at 9:51

2 Answers 2

1

Could you skim through your code, and look for any XmlWriterSettings that you are giving to your write stream? Double check that it's not using the indented output options.

If theres nothing there like that, perhaps explicitly passing in XmlWriterSettings that declares that no formatting should occur would fix this up.

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = false;
/* .... */

var outWriter = XmlWriter.Create(outputstream, settings);

http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx

1
  • Your suggestion was worthy. I have Posted the actual solution as an answer and accepted it.. Upvoted your answer.. Also have made my answer as wiki so that it can be edited by anyone! (please review it when you have time, if it needs some suggestions) thanks again! Commented Mar 5, 2013 at 5:11
0

Used

TextWriter writer = File.CreateText(outputfile.FileName);
writer.Write(strXmlOutput);
writer.Close();

Instead of existing code:

XmlDocument outputxml = new XmlDocument();
outputxml.LoadXml(strXmlOutput);
outputxml.Save(outputfile.FileName);

I think I understand the difference as well..

XmlDocument (outputxml.LoadXml, outputxml.Save), by default enforces indentation.. though strXmlOutput didn't have any indentation..

I got this fix referring Jake Heidt's answer.. Marking it as community wiki, since it's not my very own answer. Also this answer can be edited with more useful info (if required)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.