开发者

XML to CSV conversion using XSLT

开发者 https://www.devze.com 2023-03-28 21:15 出处:网络
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.

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>

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>

i am trying to define my csv header using but this is not working for me

csv output

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

as below line

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

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

Thanks in advance


Problem resolved.

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

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"/>

and it working fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消