使用 XSLT 将 XML 转换为 CSV [英] XML to CSV conversion using XSLT

查看:29
本文介绍了使用 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>&#10;</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"/>

它工作正常.

这篇关于使用 XSLT 将 XML 转换为 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆