c# /XSLT:线性化XML部分工作代码
本文关键字:工作 代码 XML 线性化 XSLT | 更新日期: 2023-09-27 18:01:36
输入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代码:<?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 & 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 & 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#代码错误说…条带空间不能应用于已经加载的文档!!所以我使用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);
你可以浏览你的代码,并寻找任何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 Used
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
没有任何缩进…
我得到了这个修复参考杰克Heidt的答案。把它标记为社区维基,因为它不是我自己的答案。此外,这个答案可以编辑更多有用的信息(如果需要)