使用XSLT实现XML到CSV的转换

使用XSLT实现XML到CSV的转换,xml,csv,xslt,export-to-csv,xmltocsv,Xml,Csv,Xslt,Export To Csv,Xmltocsv,我正在尝试将我的xml输入转换为csv输出,详情如下 输入文件: <Customer> <item> <CustomerID>100000069</CustomerID> <CustomerGroup>EX</CustomerGroup> <CustomerName>Test Mehmet</CustomerName> <CustomerSt

我正在尝试将我的xml输入转换为csv输出,详情如下

输入文件:

<Customer>
   <item>
      <CustomerID>100000069</CustomerID>
      <CustomerGroup>EX</CustomerGroup>
      <CustomerName>Test Mehmet</CustomerName>
      <CustomerStreet>Street</CustomerStreet>
      <HouseNumber>123</HouseNumber>
      <CustomerCity>Ismaning</CustomerCity>
      <CustomerZip></CustomerZip>
      <CustomerCountry>DE</CustomerCountry>
   </item>
</Customer>

100000069
前任
试验方法
街头
123
伊斯曼宁
判定元件
XSL:


我的原始输出:

“客户ID”;“客户组”;“客户名称”;“CustomerStreet”;“门牌号”;“顾客城市”;“CustomerZIP”;“客户国家” "100000069";“前”;“测试方法”;“街道”;"123";“Ismaning”;"";“德”

预期产出:

我需要用“null”来更改所有空值。下面是我的预期输出

“客户ID”;“客户组”;“客户名称”;“CustomerStreet”;“门牌号”;“顾客城市”;“CustomerZIP”;“客户国家” "100000069";“前”;“测试方法”;“街道”;"123";“Ismaning”;无效的“德”


请建议对我的代码进行所需的额外更改,以便在出现“”时填充null。

假设您仅限于XSLT 1.0,我建议您这样做:

XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/Customer">
    <!-- header row -->
    <xsl:text>"CustomerID";"CustomerGroup";"CustomerName";"CustomerStreet";"HouseNumber";"CustomerCity";"CustomerZIP";"CustomerCountry"&#10;</xsl:text>
    <!-- data rows -->
    <xsl:for-each select="item">
        <xsl:call-template name="cell">
            <xsl:with-param name="data" select="CustomerID"/>
        </xsl:call-template>
        <xsl:text>;</xsl:text>
        <xsl:call-template name="cell">
            <xsl:with-param name="data" select="CustomerGroup"/>
        </xsl:call-template>
        <xsl:text>;</xsl:text>
        <xsl:call-template name="cell">
            <xsl:with-param name="data" select="CustomerName"/>
        </xsl:call-template>
        <xsl:text>;</xsl:text>
        <xsl:call-template name="cell">
            <xsl:with-param name="data" select="CustomerStreet"/>
        </xsl:call-template>
        <xsl:text>;</xsl:text>
        <xsl:call-template name="cell">
            <xsl:with-param name="data" select="HouseNumber"/>
        </xsl:call-template>
        <xsl:text>;</xsl:text>
        <xsl:call-template name="cell">
            <xsl:with-param name="data" select="CustomerCity"/>
        </xsl:call-template>
        <xsl:text>;</xsl:text>
        <xsl:call-template name="cell">
            <xsl:with-param name="data" select="CustomerZip"/>
        </xsl:call-template>
        <xsl:text>;</xsl:text>
        <xsl:call-template name="cell">
            <xsl:with-param name="data" select="CustomerCountry"/>
        </xsl:call-template>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

<xsl:template name="cell">
    <xsl:param name="data"/>
    <xsl:choose>
        <xsl:when test="string($data)">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="$data"/>
            <xsl:text>"</xsl:text>
        </xsl:when>
        <xsl:otherwise>null</xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

“客户ID”;“客户组”;“客户名称”;“CustomerStreet”;“门牌号”;“顾客城市”;“CustomerZIP”;“客户国家”和#10;
;
;
;
;
;
;
;


"
"
无效的
这有点冗长,但它避免了代码重复

但是,如果您可以确保所有元素始终以相同的顺序存在,即使某些元素可能为空,您可以将其缩短为:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/Customer">
    <!-- header row -->
    <xsl:text>"CustomerID";"CustomerGroup";"CustomerName";"CustomerStreet";"HouseNumber";"CustomerCity";"CustomerZIP";"CustomerCountry"&#10;</xsl:text>
    <!-- data rows -->
    <xsl:for-each select="item">
        <xsl:for-each select="*">
            <xsl:choose>
                <xsl:when test="string(.)">
                    <xsl:text>"</xsl:text>
                    <xsl:value-of select="."/>
                    <xsl:text>"</xsl:text>
                </xsl:when>
                <xsl:otherwise>null</xsl:otherwise>
            </xsl:choose>
            <xsl:if test="position()!=last()">;</xsl:if>
        </xsl:for-each>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

“客户ID”;“客户组”;“客户名称”;“CustomerStreet”;“门牌号”;“顾客城市”;“CustomerZIP”;“客户国家”和#10;
"
"
无效的
;


<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/Customer">
    <!-- header row -->
    <xsl:text>"CustomerID";"CustomerGroup";"CustomerName";"CustomerStreet";"HouseNumber";"CustomerCity";"CustomerZIP";"CustomerCountry"&#10;</xsl:text>
    <!-- data rows -->
    <xsl:for-each select="item">
        <xsl:for-each select="*">
            <xsl:choose>
                <xsl:when test="string(.)">
                    <xsl:text>"</xsl:text>
                    <xsl:value-of select="."/>
                    <xsl:text>"</xsl:text>
                </xsl:when>
                <xsl:otherwise>null</xsl:otherwise>
            </xsl:choose>
            <xsl:if test="position()!=last()">;</xsl:if>
        </xsl:for-each>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>