1
votes

I would try to convert an XML file to CSV and I am taking help from the following link but struck in a place where I have to retrieve the value from a two different tag that exists at the same level. From the first tag, I have to select a value and from the next tag its child and sub-child tags values. To select values from the second tag I am using an iterator and finally, I have to publish output in the same row but I don't know how to select values from the first tag as well as from the second tag and concatenate the values of the first tag with the values coming from the second tag. For more clarification I have attached the following sample output:
My input XML sample as follows:

<PayLocation>
    <LocationCode>VNS</LocationCode>
    <LocationDescription>VARANASI</LocationDescription>
    <PayrunDetails>
        <PayrunNumber>000428</PayrunNumber>
    </PayrunDetails>
    <CompanyDetails>
        <CompanyCode>Stack99</CompanyCode>
        <CompanyName>StackOverFlow</CompanyName>

        <Payslip>
            <StaffNumber>123456</StaffNumber>
            <StaffName>ALIBABA</StaffName>      
            <PayDetails>
                <AmountNet>2100</AmountNet> 
                <ComponentDetails>
                    <ComponentType>SALARY</ComponentType>
                    <Code>SAL</Code>
                </ComponentDetails>                          
            </PayDetails>
            <LeaveTaken>
                <Description>LEAVE</Description>
                <StartDate/>
                <EndDate/>
            </LeaveTaken>
        </Payslip>

        <Payslip>
            <StaffNumber>456789</StaffNumber>
            <StaffName>AMAZON</StaffName>      
            <PayDetails>
                <AmountNet>2200</AmountNet> 
                <ComponentDetails>
                    <ComponentType>SALARY</ComponentType>
                    <Code>SAL</Code>
                </ComponentDetails>                         
            </PayDetails>
            <LeaveTaken>
                <Description>LEAVE</Description>
                <StartDate>2015-05-28</StartDate>
                <EndDate>2015-05-31</EndDate>
            </LeaveTaken>
        </Payslip>
    </CompanyDetails>
</PayLocation>

My expected output sample is as follows:

output sample

Above Output contains first column values I got from the first tag and rest of the values I got from the second tag and its child tags.

I am also sharing my XSL file:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" >
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
StaffNumber,Payslip
<xsl:for-each select="//PayLocation/CompanyDetails/Payslip">
<xsl:value-of select="concat(StaffNumber,',',PayDetails/AmountNet,'&#xA;')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Please note that I have not mention PayNumber, CompanyCode, CompanyName in the above XSL file because I don't know how to select values of the first tag along with the second tag. I'm fairly new to XSLT so please excuse the potential novice question. Any guidance would be appreciated here. Thanks in advance.

1
Is that output sample the result you want to create? Or how does it relate to the XSLT code you have shown which outputs the static text line StaffNumber,StaffName although the StaffName is not even listed in the output?Martin Honnen
I can see CompanyCode and CompanyName in the XML, but where does PayNumber come from? Thanks!Tim C
I apologize @ Martin Honnen,@Tim C I have done a correction. I need an XSLT file for the above XML file. I need almost similar kind of output. The value of the first tag will be the same for the all the employees so at the time of final output I will print the same value for each employee details. Eg: CompanyName will be the same for all the employees and inside the Payslip tag it contains different employee values.venus

1 Answers

1
votes

To get the CompanyCode (and CompanyName) you can just do this

parent::*/CompanyCode

i.e, get the child CompanyCode under the current parent

This can be simplified to this

../CompanyCode

And similarly to get the pay number, which is a child of the grand-parent, do this...

../../PayrunDetails/PayrunNumber

Try this XSLT

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

<xsl:template match="/">
    <xsl:text>PayNumber,CompanyCode,CompanyNameStaffNumber,Payslip&#10;</xsl:text>
    <xsl:for-each select="//PayLocation/CompanyDetails/Payslip">
        <xsl:value-of select="concat(../../PayrunDetails/PayrunNumber,',',../CompanyCode,',',../CompanyName,',',StaffNumber,',',PayDetails/AmountNet,'&#xA;')"/>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>