C # / XSLT: Linearizing Partially Existing XML

XML input:

<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 Result:

<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 get:

<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>

Comment:

Although unnecessary spaces from text () nodes have been fixed .. There is still indentation in the output XML.

Ideally strip-spaceshould take care of this. on top of it I added below code

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

Still out of luck !! Using

XPathDocument xpathXmlOrig = new XPathDocument(string_xmlInput);in my C # code errors saying strip-space cannot be applied to a document that has already been loaded! Therefore, 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);
+5
source share
2 answers

Is 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 too.

XmlDocument ( outputxml.LoadXml, outputxml.Save), by default provides indentation. Although strXmlOutputit has no indentation.

, . wiki, . ( )

0

- XmlWriterSettings, ? , .

, , XmlWriterSettings, , , .

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

All Articles