輸入XML:C#/ XSLT:線性化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>
觀察:
雖然從文本()節點不必要的空白已經解決..還有壓痕輸出XML。
理想strip-space
應該照顧它..在它的上面我已經添加下面的代碼
<xsl:template match="text()[../*]"/>
仍然沒有運氣!使用
XPathDocument xpathXmlOrig = new XPathDocument(string_xmlInput);
在我的C#代碼中出錯說出來.. strip-space不能應用於已經加載的文件!!所以我使用的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);
+1爲一個非常全面的數據集。 – 2013-02-28 09:12:07
謝謝你的欣賞.. – Enthusiastic 2013-02-28 09:13:01
對於它的價值,我嘗試過使用'xsltproc'和Saxon(9.1.0.5J)的樣式表,並且在這兩種情況下,輸出都是您的預期輸出。 – 2013-02-28 09:44:09