Input XML:
<foobar>
<Comments>
Reported By: L & 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 & 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 & 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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With