xmlcsvvariablesxsltxmltocsv

XSLT (XML to CSV): How to create dynamic variable and reference it later in concatination?


I need to turn XML file into CSV via XSLT.

Here's my XML

<pi:Payroll_Extract_Employees xmlns:pi="urn:com.workday/picof">
<pi:Employee>
    <pi:Summary>
        <pi:Employee_ID>12345</pi:Employee_ID>
        <pi:Name>John Smith</pi:Name>
    </pi:Summary>
</pi:Employee>
<pi:Employee>
    <pi:Summary>
        <pi:Employee_ID>09876</pi:Employee_ID>
        <pi:Name>Jane Rance</pi:Name>
    </pi:Summary>
</pi:Employee>
</pi:Payroll_Extract_Employees>

Here's desired output:

Employee ID,Employee Name
12345,
09876,

Here's my XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pi="urn:com.workday/picof" exclude-result-prefixes="xsl">

<xsl:output method="text" encoding="x-UTF-16LE-BOM"/>

<xsl:variable name="delimiter" select="','"/>

 <xsl:variable name="EmployeeID">
    <xsl:for-each select="pi:Payroll_Extract_Employees/pi:Employee">
        <xsl:value-of select="pi:Summary/pi:Employee_ID"/>
    </xsl:for-each>
</xsl:variable>

<xsl:variable name="ColumnHeadings">
    <field>Employee ID</field>
    <field>Employee Name</field>
</xsl:variable>

<xsl:param name="headings" select="document('')/*/xsl:variable[@name='ColumnHeadings']/*" />

<xsl:template match="/">

    <xsl:for-each select="$headings">
        <xsl:if test="position() != 1">
            <xsl:value-of select="$delimiter"/>
        </xsl:if>
            <xsl:value-of select="." />
    </xsl:for-each>

    <xsl:text>&#xa;</xsl:text>

    <xsl:for-each select="pi:Payroll_Extract_Employees/pi:Employee">
        <xsl:value-of
            select="concat($EmployeeID,$delimiter)"/>
        <xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
    
</xsl:template>

</xsl:stylesheet>

Unfortunately, it returns it like this:

Employee ID,Employee Name
1234509876,
1234509876,

QUESTION: : do you know how I can adjust my variable so I can refer to it in a concatination and it will simply go through each employee and return thir values? This is the variable from that code I'm having troubles with (everything else works perfectly):

<xsl:variable name="EmployeeID">
   <xsl:for-each select="pi:Payroll_Extract_Employees/pi:Employee">
       <xsl:value-of select="pi:Summary/pi:Employee_ID"/>
   </xsl:for-each>
</xsl:variable>

Any help will be greatly appreciated!


Solution

  • You are doing it the hard way. You have created a variable which is node of the document. It is easier to create templates that transform each node of the document directly.

    Try like this:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:pi="urn:com.workday/picof" exclude-result-prefixes="xsl">
    
    <xsl:output method="text" encoding="x-UTF-16LE-BOM"/>
    
    <xsl:param name="headings" select="document('')/*/xsl:variable[@name='ColumnHeadings']/*" />
    
    <xsl:template match="/">
    
    
    <!-- Output the headings -->
        <xsl:text>Employee ID,Employee Name</xsl:text>
        <xsl:text>&#xa;</xsl:text>
        
    <!-- For each employee -->
        <xsl:for-each select="pi:Payroll_Extract_Employees/pi:Employee">
    <!-- Output the value of each element that you need -->
            <xsl:value-of select="pi:Summary/pi:Employee_ID"/>,<xsl:value-of select="pi:Summary/pi:Name"/>
            <xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
        
    </xsl:template>
    
    </xsl:stylesheet>