I am getting an XML document output from Cognos which I want to use as Input for Crystal reports.
However, the XML format needed by Crystal Report is different from the XML format of Cognos output.
I am trying to transform the Input XML document (Cognos) using XSLT to get desired XML for Crystal.
Having set the context, below is the Input XML coming from Cognos:
<?xml version="1.0"?>
<dataset>
<metadata>
<item Name="EmpId" />
<item Name="EmpName" />
<item Name="DeptName" />
</metadata>
<data>
<rows>
<row>
<value>1</value>
<value>John</value>
<value>Finance</value>
</row>
<row>
<value>2</value>
<value>Peter</value>
<value>Admin</value>
</row>
</rows>
</data>
Desired XML format required by Crystal Report:
<?xml version="1.0"?>
<dataset>
<row>
<EmpId>1</EmpId>
<EmpName>John</EmpName>
<DeptName>Finance</DeptName>
</row>
<row>
<EmpId>2</EmpId>
<EmpName>Peter</EmpName>
<DeptName>Admin</DeptName>
</row>
</dataset>
I have written below XSLT for the desired transformation:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<dataset>
<xsl:for-each select="./dataset/data/rows/row">
<row>
<xsl:for-each select="/dataset/metadata/item">
<xsl:element name="{@Name}">
<xsl:for-each select="/dataset/data/rows/row/value">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</row>
</xsl:for-each>
</dataset>
</xsl:template>
</xsl:stylesheet>
I am getting below Output:
<?xml version="1.0" encoding="UTF-16"?>
<dataset>
<row>
<EmpId>1JohnFinance2PeterAdmin</EmpId>
<EmpName>1JohnFinance2PeterAdmin</EmpName>
<DeptName>1JohnFinance2PeterAdmin</DeptName>
</row>
<row>
<EmpId>1JohnFinance2PeterAdmin</EmpId>
<EmpName>1JohnFinance2PeterAdmin</EmpName>
<DeptName>1JohnFinance2PeterAdmin</DeptName>
</row>
Where am I going wrong?
After customizing the solution provided by @DimitreNovatchev I was able to get the desired transformation of XML.
Input XML
XSLT Transformation
</xsl:stylesheet>
Output XML