2

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you SOOO much for posting this. It was the answer to my problem--doing something very similar. Essentially, I also need to convert XML to CSV. My output is more traditionally formatted than yours (no INSERT statements at the top). Regardless, the mode="header" trick was what I was missing. Again, thank you /very/ much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.