【发布时间】:2019-10-15 04:44:43
【问题描述】:
我正在尝试使用 XSLT 将 XML 转换为 CSV,我正在获取记录,但我需要 CSV 文件中的标题。
例如:
Name EmailAddress
Gaurav Gaur@yopmail.com
Satya Satya@yopmail.com
XML
<?xml version="1.0"?>
<SearchResults>
<Columns>
<Column Label="Name">Name</Column>
<Column Label="Email Addresses">EmailAddress</Column>
</Columns>
<Rows>
<Row>
<Name>Gaurav Joshi</Name>
<EmailAddress>ybenipohe-3888@yopmail.com</EmailAddress>
</Row>
<Row>
<Name>Satya</Name>
<EmailAddress>eqaddoxoqu-8703@yopmail.com</EmailAddress>
</Row>
XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<!-- This transform is used to generate a CSV file.-->
<xsl:output method="text" indent="no" />
<xsl:template match="/">
<xsl:apply-templates select="/SearchResults/Rows/Row" />
</xsl:template>
<xsl:template match="/SearchResults/Rows/Row">
<xsl:text>"</xsl:text>
<xsl:value-of select="position()" />
<xsl:text>"</xsl:text>
<xsl:for-each select="*">
<xsl:text>,"</xsl:text>
<xsl:value-of select="." />
<xsl:text>"</xsl:text>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="/SearchResults/Columns" />
</xsl:stylesheet>
【问题讨论】: