使用 XSLT 将 XML 转换为 CSV
我正在尝试使用 xslt 将 XML 转换为 csv,我对 xslt 不熟悉,所以只是阅读一些教程和其他在线资源.这是我的 XML
i am trying to convert XML to csv using xslt, i am not familiar with xslt so just reading some tutorial and other online resources. Here is my 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
i am using the following XSL for the conversion of xml to 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> </xsl:text>
</xsl:when>
<xsl:otherwise>;</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我正在尝试使用定义我的 csv 标头,但这对我不起作用
i am trying to define my csv header using but this is not working for me
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
I was assuming that it was due to ;
in the select
value but i even removed it and even removed the space but nothing helped
i have googled about this but unable to find a way as how to define this header inside my xsl file so that i can use this header for my csv
提前致谢
问题已解决.
<xsl:apply-templates select="test[1]/*" mode="header"/>
这里我选择了 test[1]
中的所有元素,在我的 XML 中,test
元素中有五个元素,所以我只是将这一行更改为:
Here I was selecting all elements inside test[1]
and in my XML I have five elements inside the test
element, so I just changed this line to:
<xsl:apply-templates select="test[1]" mode="header"/>
它工作正常.