I'm trying to convert the xml data in following format to csv format using xslt.I am right now struck trying to output multivalues for particular custom field. This is my xml data.
<input-xml>
<add add-value="First Name">
<value type="string">Newbee</value>
</add>
<add add-value="Surname">
<value type="string">user1</value>
</add>
<add add-value="Title">
<value type="string">Software,engineer</value>
</add>
<add add-value="Title">
<value type="string">Associate level</value>
</add>
<add add-value="Description">
<value type="string">This user is new to xslt.</value>
</add>
</input-xml>
Below is the snippet of the xslt code I am using to transform the data.
<xsl:template match="input-xml">
<!-- output the fields in order -->
<xsl:call-template name="output-field">
<xsl:with-param name="field-value" select="*[(@attr-name = 'First')]/value"/>
</xsl:call-template>
.............
.............
<xsl:call-template name="output-field">
<xsl:with-param name="field-value" select="*[(@attr-name = 'Title')]/value"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="output-field">
<xsl:param name="field-value"/>
<xsl:choose>
<xsl:when test="contains($field-value,$delimiter)">
<!-- if the field value contains a delimiter, then enclose in quotes -->
<!--delimiter here is comma (,)-->
<xsl:text>"</xsl:text>
<xsl:value-of select="$field-value"/>
<xsl:text>"</xsl:text>
</xsl:when>
<xsl:otherwise>
<!-- otherwise output it raw -->
<xsl:value-of select="$field-value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template
With the above code I'm able to output data as:
Newbee,User1,Software engineer,This user is new to xslt.
This is absolutely fine as long as there are no multiple values. The problem now is there are two value for field 'Title' which will have to enclosed within quotes ("") as below. Expected output csv data:
Newbee,User1,"|Software engineer|,|Associate level|",This user is new to xslt.
I'm not able to figure out what changes i'll have to make above code and how to proceed further.. Can anyone help me out..??
@LarsH your previous reply to question i posted is working fine..I found that for the below xml format,the xslt doesn't quite work the way it is desired.
<input-xml>
<add add-value="First Name">
<value type="string">Newbee</value>
</add>
<add add-value="Surname">
<value type="string">user1</value>
<add add-value="Title">
<value type="string">Associate level</value>
<value type="string">Software,engineer</value>
</add>
<add add-value="Description">
<value type="string">This user is new to xslt.</value>
</add>
</input-xml>
The output i'm getting is
Newbee,User1,Software engineerAssociate level,This user is new to xslt.
Do i need to write another xslt to be able to handle this xml..? can i modify the existing xslt itself to take care of both xml variants..?