尝试使用XSLT将XML数据转换为CSV。行用逗号分隔,但有些数据有双引号。我使用以下代码进行转换,但它没有正确处理数据,尤其是带引号的行。
这是我的样本数据
<Add>
<Rowinfo>
<LocatorD>Dwelling </LocatorD>
<LName> shark </LName>
<L>1</L>
<AArea>Abesinia Passage</AArea>
</Rowinfo>
当XSL应用于上述数据时,它会产生
LocatorDesignator,LocatorName, Locator , Thoroughfare , AddressArea
Dwelling , shark , 1 , Abesinia Passage,
Shop 01-Feb,Shop , 1 , Casenapes Square , ,
但是预期的结果是产生
LocatorDesignator,LocatorName,Locator, Thoroughfare , AddressArea
Dwelling , shark , 1 , Abesinia Passage ,
Shop 01-Feb , Shop , 1 , Casenapes Square ,
换句话说,当您将其作为CSV文件打开时
而不是单独的列,例如:
LocatorDesignator | LocatorName
Shop 01-Feb,shop |
而不是
LocatorDesignator| LocatorName
Shop 01-Feb | Shop
答案 0 :(得分:0)
这个XSLT 1.0样式表...
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="*/Rowinfo[1]/*" mode="heading" />
<xsl:value-of select="'
'" />
<xsl:apply-templates select="*/Rowinfo" />
</xsl:template>
<xsl:template match="Rowinfo/*" mode="heading" >
<xsl:value-of select="local-name()" />
<xsl:if test="position() != last()">
<xsl:value-of select="','" />
</xsl:if>
</xsl:template>
<xsl:template match="Rowinfo">
<xsl:variable name="line-with-extra-comma">
<xsl:for-each select="*">
<xsl:variable name="col-name" select="local-name()" />
<xsl:if test="../../Rowinfo[1]/*[local-name() = $col-name]">
<xsl:call-template name="csv-encode" />
<xsl:value-of select="','" />
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="concat(
substring($line-with-extra-comma, 1,
string-length($line-with-extra-comma) - 1),
'
')" />
</xsl:template>
<xsl:template name="escape-value">
<xsl:param name="text" />
<xsl:choose>
<xsl:when test="contains($text,'"')">
<xsl:value-of select="concat( substring-before($text,'"'), '""')" />
<xsl:call-template name="escape-value">
<xsl:with-param name="text" select="substring-after($text,'"')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="csv-encode">
<xsl:choose>
<xsl:when test="contains(.,',') or starts-with(.,'"')">
<xsl:value-of select="'"'" />
<xsl:call-template name="escape-value">
<xsl:with-param name="text" select="text()" />
</xsl:call-template>
<xsl:value-of select="'"'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
...将采用此输入文档......
<Address>
<Rowinfo>
<LocatorDesignator>Dwelling </LocatorDesignator>
<LocatorName> shark </LocatorName>
<Locator>1</Locator>
<AddressArea>Abesinia Passage</AddressArea>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>"Shop 01-Feb</LocatorDesignator>
<LocatorName>"Shop</LocatorName>
<Locator>1</Locator>
<Thoroughfare>Casenapes Square</Thoroughfare>
<AddressArea/>
</Rowinfo>
</Address>
...并将其转换为此csv输出...
LocatorDesignator,LocatorName,Locator,AddressArea
Dwelling , shark ,1,Abesinia Passage
"""Shop 01-Feb","""Shop",1,
我认为: