使用XSLT进行XML到CSV的转换

问题描述 投票:2回答:1

我正在尝试使用xslt将XML转换为csv,我不熟悉xslt所以只是阅读一些教程和其他在线资源。这是我的XML

<?xml version="1.0" encoding="UTF-8"?>
<impex>
    <test>
        <Employee />
        <UID>auma</UID>
        <Name>HR Manager</Name>
        <Groups />
        <Password>228781</Password>
    </test>
    <test>
        <Employee />
        <UID>auma1</UID>
        <Name>HR Manager</Name>
        <Groups />
        <Password>2287811</Password>
    </test>
</impex>

我正在使用以下XSL将xml转换为csv

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:f="Functions"
  version="2.0">

<xsl:output method="text" />
 <xsl:param name="headerVal" select="'INSERT_UPDATE Employee;UID[unique=true];name;groups(uid);password'"/>


<xsl:template match="/impex">
     <xsl:apply-templates select="test[1]/*" mode="header"/>
    <xsl:apply-templates select="test" />
</xsl:template>


<xsl:template match="*" mode="header" >

    <xsl:value-of select="$headerVal" />
</xsl:template>

<xsl:template match="test">
    <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="*" >
    <xsl:value-of select="."/>
    <xsl:choose>
        <xsl:when test="position()=last()">
            <xsl:text>&#10;</xsl:text>
        </xsl:when>
        <xsl:otherwise>;</xsl:otherwise>
    </xsl:choose>
</xsl:template>


</xsl:stylesheet>

我试图定义我的csv标头使用,但这对我不起作用

csv输出

INSERT_UPDATEEmployeeUID[unique=true]namegroups(uid)password
INSERT_UPDATEEmployeeUID[unique=true]namegroups(uid)password
INSERT_UPDATEEmployeeUID[unique=true]namegroups(uid)password
INSERT_UPDATEEmployeeUID[unique=true]namegroups(uid)password
INSERT_UPDATEEmployeeUID[unique=true]namegroups(uid)password
;auma;HR Manager;;228781
;auma1;HR Manager;;2287811

如下线

<xsl:param name="headerVal" select="'INSERT_UPDATE Employee;UID[unique=true];name;groups(uid);password'"/>

我假设这是由于;select,但我甚至删除它,甚至删除了空间,但没有任何帮助我谷歌搜索但无法找到一种方法,如何在我的xsl文件中定义此标头,以便我可以使用此标头为我的csv

提前致谢

xml xslt xslt-2.0
1个回答
1
投票

问题解决了。

<xsl:apply-templates select="test[1]/*" mode="header"/>

在这里我选择了test[1]中的所有元素,在我的XML中,我在test元素中有五个元素,所以我只是将这一行改为:

<xsl:apply-templates select="test[1]" mode="header"/>

它工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.