1

试图转换这个:

<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>的建议进行输入,它删除了最后一个换行符,所以我将它移到了文件的顶部,此时它变成了无操作。这里的解决方案实际上添加了一个空行(这是有道理的,因为十六进制代码是换行符,根据ascii 手册页)。

作为一种解决方法,我一直在使用 Java 来生成 CSV 输出。

但是,我确实觉得 XSLT 会快很多,因为它旨在将 XML 转换为各种其他格式。类似的 XSLT 可以完美地生成 HTML、RSS 和 ATOM 提要。

4

1 回答 1

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,它将完美运行。

于 2013-03-21T12:09:08.503 回答