0

i am new to XSLT and have gone through some of the earlier thread on SO Thread i Followed

my requirements are similar and i need to convert XML to CSV, for e.g i have following XML

<?xml version="1.0" encoding="UTF-8"?>
<impex>
<record>
<Employee/>
<UID>aa</UID>
<Name>HR Manager</Name>
<Groups/>
<Password>123456</Password>
</record>
<record>
<Employee/>
<UID>bb</UID>
<Name>HR Executive</Name>
<Groups/>
<Password>123456</Password>
</record>
</impex>

and i need to convert this XML to following csv output

INSERT_UPDATE Employee;UID[unique=true];name;groups(uid);password
;"aa";"HR Manager";;"123456"
 ;"bb";"HR Executive";;"123456"

where i have to manage csv headers dynamically (based on xml elements)

additonaly i have also to take care if some of the values are missing i can provide them e.g is missing or its empty in such case i need to provide some default value to the generated csv as this csv will be the final output which will be imported to the system

any starting help by which i can move ahead will be much appreciated

7
  • i am not familiar about xslt so i am struck how to create header as what i have saw so far is no header only the values Commented Aug 16, 2011 at 12:11
  • 1
    I would recommend that you start reading the W3CSchools XSLT tutorials. They are very good, and you will probably learn anough to solve your problem within an hour w3.org/TR/xslt Commented Aug 16, 2011 at 12:14
  • sure will do that..but any help will also be appreciated, i know i am asking much but this is something blocking my work :) Commented Aug 16, 2011 at 12:17
  • Why are you confusing your readers? I don't see anything in common between the provided XML document and the wanted result! Commented Aug 16, 2011 at 12:43
  • That XML structure looks like it can only have one record; is that true? If not, how would it contain more than one record? Commented Aug 16, 2011 at 12:57

1 Answer 1

2

Not the most beautiful, but it works

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" />

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


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

<xsl:template match="record">
    <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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Per for this..though i also have to learn xslt myself and i am doing that but it can help me to get started a bit

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.