问题描述
我正在尝试将CSV(逗号分隔的文件)转换为XML。为此,我编写了一个XSLT模板,这是我在XSLT的第一次尝试...
I am are trying to convert an CSV (comma separated file) into XML. For this, I am coding an XSLT template and this is my 1st try at XSLT...
CSV示例:
ClaimRef,HandlerRef,ClaimType,Date,Area,SettleDate,ClaimStatus,ClaimantName
1,1/1,Liability,08-12-2013,US,23-05-2014,Closed,Mark
2,1/2,Liability,08-10-2013,UK,23-02-2014,Closed,John
所需的XML输出格式:
<Claims>
<Claim>
<ClaimRef></ClaimRef>
<HandlerRef></HandlerRef>
<ClaimType></ClaimType>
<Date></Date>
<Area></Area>
<SettleDate></SettleDate>
<ClaimStatus></ClaimStatus>
<ClaimantName></ClaimantName>
</Claim>
</Claims>
我使用作为初始开始和来测试结果。但是本文提到了如何获取< row>< elem>等等
的值。
I used http://blogs.msdn.com/b/kaevans/archive/2003/04/17/5780.aspx as initial start and http://xslttest.appspot.com/ to test the results. But this article mentions how to get the values as <row><elem>, etc
.
你可以指导我如何编写一个XSLT来基于示例CSV数据生成上述XML。
Please can you guide me how to code an XSLT to generate above XML based on sample CSV data.
推荐答案
这里有一个XSLT 2.0选项。 ..
Here's an XSLT 2.0 option...
CSV输入(so.csv在 csv-uri
。)
CSV Input (so.csv referenced in the csv-uri
param.)
ClaimRef,HandlerRef,ClaimType,Date,Area,SettleDate,ClaimStatus,ClaimantName
1,1/1,Liability,08-12-2013,US,23-05-2014,Closed,Mark
2,1/2,Liability,08-10-2013,UK,23-02-2014,Closed,John
XSLT 2.0 (使用格式良好的XML文档或样式表本身作为输入或指定 csv2xml
作为初始模板。)
XSLT 2.0 (Use either a well-formed dummy XML doc or the stylesheet itself as input or specify csv2xml
as the initial template.)
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="csv-encoding" as="xs:string" select="'iso-8859-1'"/>
<xsl:param name="csv-uri" as="xs:string" select="'file:///C:/Users/dhaley/Desktop/so.csv'"/>
<xsl:template match="/" name="csv2xml">
<Claims>
<xsl:choose>
<xsl:when test="unparsed-text-available($csv-uri, $csv-encoding)">
<xsl:variable name="csv" select="unparsed-text($csv-uri, $csv-encoding)"/>
<!--Get Header-->
<xsl:variable name="header-tokens" as="xs:string*">
<xsl:analyze-string select="$csv" regex="\r\n?|\n">
<xsl:non-matching-substring>
<xsl:if test="position()=1">
<xsl:copy-of select="tokenize(.,',')"/>
</xsl:if>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:analyze-string select="$csv" regex="\r\n?|\n">
<xsl:non-matching-substring>
<xsl:if test="not(position()=1)">
<Claim>
<xsl:for-each select="tokenize(.,',')">
<xsl:variable name="pos" select="position()"/>
<xsl:element name="{$header-tokens[$pos]}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</Claim>
</xsl:if>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="error">
<xsl:text>Error reading "</xsl:text>
<xsl:value-of select="$csv-uri"/>
<xsl:text>" (encoding "</xsl:text>
<xsl:value-of select="$csv-encoding"/>
<xsl:text>").</xsl:text>
</xsl:variable>
<xsl:message><xsl:value-of select="$error"/></xsl:message>
<xsl:value-of select="$error"/>
</xsl:otherwise>
</xsl:choose>
</Claims>
</xsl:template>
</xsl:stylesheet>
XML输出
<Claims>
<Claim>
<ClaimRef>1</ClaimRef>
<HandlerRef>1/1</HandlerRef>
<ClaimType>Liability</ClaimType>
<Date>08-12-2013</Date>
<Area>US</Area>
<SettleDate>23-05-2014</SettleDate>
<ClaimStatus>Closed</ClaimStatus>
<ClaimantName>Mark</ClaimantName>
</Claim>
<Claim>
<ClaimRef>2</ClaimRef>
<HandlerRef>1/2</HandlerRef>
<ClaimType>Liability</ClaimType>
<Date>08-10-2013</Date>
<Area>UK</Area>
<SettleDate>23-02-2014</SettleDate>
<ClaimStatus>Closed</ClaimStatus>
<ClaimantName>John</ClaimantName>
</Claim>
</Claims>
每个评论更新...
下面是相同的示例,但使用一个变量而不是外部CSV文件。您可以使用此XSLT在支持XSLT 2.0的其他在线测试工具中进行测试。
Here is the same example, but using a variable instead of an external CSV file. You can use this XSLT to test in other online test tools that support XSLT 2.0.
这篇关于XSLT 2.0将CSV转换为XML格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!