5

输入 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 代码:

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

预期输出:

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

我得到了什么:

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

观察:

尽管 text() 节点中不必要的空格已得到纠正.. 输出 XML 中仍然存在缩进。

理想情况下strip-space应该照顾它..在它之上我添加了下面的代码

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

还是没有运气!!的使用

XPathDocument xpathXmlOrig = new XPathDocument(string_xmlInput);在我的 C# 代码错误中说 .. strip-space cannot be applied to document which has been loaded!! 所以我正在使用 XMLReader ..

添加 C# 代码以供参考..

        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

2 回答 2

1

你能浏览一下你的代码,并寻找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

于 2013-02-28T10:46:03.280 回答
0

用过的

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

而不是现有的代码:

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

我想我也理解其中的区别..

XmlDocument( outputxml.LoadXml, outputxml.Save),默认情况下强制缩进..虽然strXmlOutput没有任何缩进..

我得到了这个修复,参考了 Jake Heidt 的答案。将其标记为社区 wiki,因为这不是我自己的答案。此答案也可以使用更有用的信息进行编辑(如果需要)

于 2013-02-28T12:15:24.853 回答