2013-02-28 32 views
5

輸入XML:C#/ XSLT:線性化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> 

觀察:

雖然從文本()節點不必要的空白已經解決..還有壓痕輸出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

+1爲一個非常全面的數據集。 – 2013-02-28 09:12:07

+0

謝謝你的欣賞.. – Enthusiastic 2013-02-28 09:13:01

+1

對於它的價值,我嘗試過使用'xsltproc'和Saxon(9.1.0.5J)的樣式表,並且在這兩種情況下,輸出都是您的預期輸出。 – 2013-02-28 09:44:09

回答

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沒有任何壓痕..

我得到這個修復指傑克Heidt的答案.. 將其標記爲社區維基,因爲它不是我自己的答案。此外,這個答案可以編輯更多有用的信息(如果需要)

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

+0

感謝您的建議..正在處理它.. – Enthusiastic 2013-02-28 11:02:41

+0

您的建議值得。我已經發布了實際的解決方案作爲答案,並接受了它.. Upvoted你的答案..也已經使我的答案作爲維基,以便它可以由任何人編輯! (請在有時間的情況下查看它,如果需要一些建議)再次感謝! – Enthusiastic 2013-03-05 05:11:28