I would like to transform an XML file into a CSV file. The fields I want to export are the <issues>. Each issue has a set of standard fields and some custom fields. The possible custom fields are defined in <issue-custom-fields>. Not every <issue> has all custom fields set. For the CSV export I have to add an empty field (",,") for each missing entry. How would I do this?
The following xslt file loops through all fields of issue and through all custom fields. Instead of "field value?" I would like to have look up the respective field value in the current item (if it exists, a blank if not).
XSLT version should be 1.0.
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:apply-templates select="xml_data/issues"/>
</xsl:template>
<xsl:template match="issue">
<xsl:for-each select="*">
<xsl:choose>
<xsl:when test="name(.) = 'custom-values'">
<xsl:for-each select="/xml_data/issue-custom-fields/issue-custom-field">
<xsl:variable name="f" select="id" />
<xsl:text>field value?</xsl:text>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:transform>
acting on the following data:
<?xml version="1.0" encoding="UTF-8"?>
<xml_data>
<projects type="array">
<project>
<id type="integer">10</id>
<name>Helpdesk</name>
<type>open</type>
</project>
<project>
<id type="integer">20</id>
<name>Development</name>
<type>closed</type>
</project>
</projects>
<issue-custom-fields>
<issue-custom-field>
<id>1000</id>
<name>Delay</name>
</issue-custom-field>
<issue-custom-field>
<id>1001</id>
<name>Means of Delivery</name>
</issue-custom-field>
<issue-custom-field>
<id>1002</id>
<name>Shipping Date</name>
</issue-custom-field>
</issue-custom-fields>
<issues type="array">
<issue>
<id type="integer">100</id>
<project-id type="integer">10</project-id>
<subject>first helpdesk issue</subject>
<description>a small problem</description>
<custom-values>
<custom-value>
<custom-field-id>1000</custom-field-id>
<value>15</value>
</custom-value>
<custom-value>
<custom-field-id>1002</custom-field-id>
<value>2016-08-01</value>
</custom-value>
</custom-values>
</issue>
<issue>
<id type="integer">101</id>
<project-id type="integer">10</project-id>
<subject>second helpdesk issue</subject>
<description>a medium problem</description>
<custom-values>
<custom-value>
<custom-field-id>1000</custom-field-id>
<value>10</value>
</custom-value>
<custom-value>
<custom-field-id>1001</custom-field-id>
<value>FTP</value>
</custom-value>
</custom-values>
</issue>
<issue>
<id type="integer">102</id>
<project-id type="integer">10</project-id>
<subject>third helpdesk issue</subject>
<description>a huge security problem</description>
<custom-values>
<custom-value>
<custom-field-id>1001</custom-field-id>
<value>SSH</value>
</custom-value>
</custom-values>
</issue>
<issue>
<id type="integer">103</id>
<project-id type="integer">20</project-id>
<subject>first "development" issue</subject>
<description>just some "strange" software</description>
<custom-values>
</custom-values>
</issue>
</issues>
</xml_data>
Thank you very much for your help.