Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/XSLT: Linearizing XML partially working code

Tags:

c#

xml

xslt

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);
like image 224
Enthusiastic Avatar asked Nov 13 '22 10:11

Enthusiastic


1 Answers

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

like image 134
Jake H Avatar answered Nov 14 '22 23:11

Jake H