关于使用 XSLT 或 SAX/DOM/OTHER 将大型嵌套 XML 文档转换为 CSV 的建议?
Advice on converting large nested XML documents to CSV using XSLT or SAX/DOM/OTHER??
正在尝试将 XML 文档转换为 CSV 格式。哪种方法最好?我看到很多人建议使用 XSLT,而其他人建议使用某种解析器。只是想更好地了解使用什么。
需要考虑的几件事是:
假设 XML 文件嵌套了一点(假设深度为 3)。另外,如果我想为 csv 文件包含一个 header,这将是属性名称,但并非所有 parent 节点可能具有相同数量的 children。例如:
<phones>
<phone>
<mobile>555-555-5555</mobile>
<work>231-234-3333</work>
<home>206-444-7777</home>
</phone>
<phone>
<home>999-999-9999</home>
</phone>
</phones>
输出如下:
mobile,work,home
555-555-5555,231-234-3333,206-444-7777
,,999-999-9999
据我所知,XSLT 是将 XML 转换为文本、csv、其他 XML 等的最佳工具
在XSLT中填充headers的问题,可以是如下文字串:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/phones">
<xsl:text>mobile,work,home
</xsl:text>
<xsl:apply-templates select="phone"/>
</xsl:template>
<xsl:template match="phone">
<xsl:value-of select="concat(mobile, ',', work, ',', home, '
')"/>
</xsl:template>
</xsl:stylesheet>
正在尝试将 XML 文档转换为 CSV 格式。哪种方法最好?我看到很多人建议使用 XSLT,而其他人建议使用某种解析器。只是想更好地了解使用什么。 需要考虑的几件事是: 假设 XML 文件嵌套了一点(假设深度为 3)。另外,如果我想为 csv 文件包含一个 header,这将是属性名称,但并非所有 parent 节点可能具有相同数量的 children。例如:
<phones>
<phone>
<mobile>555-555-5555</mobile>
<work>231-234-3333</work>
<home>206-444-7777</home>
</phone>
<phone>
<home>999-999-9999</home>
</phone>
</phones>
输出如下:
mobile,work,home
555-555-5555,231-234-3333,206-444-7777
,,999-999-9999
据我所知,XSLT 是将 XML 转换为文本、csv、其他 XML 等的最佳工具
在XSLT中填充headers的问题,可以是如下文字串:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/phones">
<xsl:text>mobile,work,home
</xsl:text>
<xsl:apply-templates select="phone"/>
</xsl:template>
<xsl:template match="phone">
<xsl:value-of select="concat(mobile, ',', work, ',', home, '
')"/>
</xsl:template>
</xsl:stylesheet>