2013-03-21 72 views
1

嘗試轉換這樣的:XSL生成CSV

<list> 
    <entry> 
      <parentFeed> 
        <feedUrl>http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000001.xml</feedUrl> 
         <id>68</id> 
         </parentFeed> 
          <content>Schools will have to put up with problematic pay administered through Novopay for another eight weeks after the Government announced it would persist with the unstable system.Minister responsible for Novopay, Steven Joyce, delayed...</content> 
          <link>http://www.nzherald.co.nz/nz/news/article.cfm?c_id=1&amp;objectid=10872300&amp;ref=rss</link> 
           <title>Novopay: Govt sticks with unstable system</title> 
           <id>55776</id> 
            <published class="sql-timestamp">2013-03-19 03:38:55.0</published> 
            <timestamp>2013-03-19 07:31:16.358 UTC</timestamp> 
            </entry> 
          </list> 

成這樣,使用XSLT:

Title, Link, Date 
Novopay: Govt sticks with unstable system, http://www.nzherald.co.nz/nz/news/article.cfm?c_id=1&objectid=10872300&ref=rss, 2013-03-19 03:38:55.0 

但是儘量爲我可能,一開始我無法擺脫的空行的文件。我的樣式表如下:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:csv="csv:csv" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="text" encoding="UTF-8"/> 
<xsl:template match="/list"> <xsl:for-each select="entry"> 
Title, Link, Date 
<xsl:value-of select="title"/>, <xsl:value-of select="link"/>, <xsl:value-of select="published"/> 
</xsl:for-each></xsl:template> 
</xsl:stylesheet> 

我試圖把在<xsl:text>&#xD;</xsl:text>的建議here他們剝去最後斷行,所以我把它移動到文件的頂部,此時它變成一個無操作。解決方案here實際上增加了一個空白行(這是有道理的,因爲十六進制代碼是換行符,根據ascii manpage)。

作爲解決方法,我一直在使用Java來生成CSV輸出。

但是,我確實覺得XSLT會更快,因爲它旨在將XML轉換爲各種其他格式。類似的XSLT可以完美地生成HTML,RSS和ATOM Feed。

回答

3

你已經完成了,你的邏輯是現貨。然而,你需要採取什麼想法,當你的輸出文本在XSLT所有縮進會影響輸出讓你的XSLT應該是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:csv="csv:csv" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="text" encoding="UTF-8"/> 
<xsl:template match="/list"> <xsl:for-each select="entry">Title, Link, Date 
<xsl:value-of select="title"/>, <xsl:value-of select="link"/>, <xsl:value-of select="published"/> 
<xsl:text> 
</xsl:text> 
</xsl:for-each></xsl:template> 
</xsl:stylesheet> 

運行上面的XSLT,它會很好地工作。

+0

不......現在它不會在每行之後給我換行符。 – hd1 2013-03-21 12:16:27

+0

您可以提供多行的示例輸出嗎? – MMKD 2013-03-21 12:20:32

+0

您的解決方案很近,我會接受並投票,因爲它適用於我的編輯。 – hd1 2013-03-21 12:20:41