输入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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
预期产量:
<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>
Run Code Online (Sandbox Code Playgroud)
我得到了什么:
<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>
Run Code Online (Sandbox Code Playgroud)
观察:
尽管来自text()节点的不必要的空格已被纠正.但输出XML中仍有缩进.
理想情况下strip-space
应该照顾它..在它之上我添加了下面的代码
<xsl:template match="text()[../*]"/>
Run Code Online (Sandbox Code Playgroud)
仍然没有运气!用法
XPathDocument xpathXmlOrig = new XPathDocument(string_xmlInput);
在我的C#代码错误中说..条带空间不能应用于已经加载的文件!所以我使用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);
Run Code Online (Sandbox Code Playgroud)
用过的
TextWriter writer = File.CreateText(outputfile.FileName);
writer.Write(strXmlOutput);
writer.Close();
Run Code Online (Sandbox Code Playgroud)
而不是现有的代码:
XmlDocument outputxml = new XmlDocument();
outputxml.LoadXml(strXmlOutput);
outputxml.Save(outputfile.FileName);
Run Code Online (Sandbox Code Playgroud)
我想我也明白其中的区别..
XmlDocument ( outputxml.LoadXml, outputxml.Save
) 默认情况下强制缩进.. 尽管strXmlOutput
没有任何缩进..
我参考了 Jake Heidt 的答案得到了这个修复。将其标记为社区维基,因为这不是我自己的答案。此外,可以使用更多有用的信息来编辑此答案(如果需要)