I am new to XSLT and XML. I have been going through tutorials and stackoverflow examples and learning a lot of new things.
Currently I am trying to apply XSLT on a XML file to transform it into CSV. What I am trying to achieve is that, I have some information in the XML file, which I would like to extract. The CSV file will have some information by default, which I am passing in the XSLT.
Also I have used basic XSLT syntax that I learnt quickly and I am sure this is the worst way to do it. I would appreciate all the help, if anyone can guide me how to make this solution better.
The issues I am having are as follows:
The information is not getting formatted properly, its getting printed on a new line, which I don't want unless I specify for a new line. Also if I don't indent the XSLT file then the information can be kept on single like but the code looks a mess, I am sure there is some easy way to do this.
Every person will have some licenses, which I want to read inside the for-each loop, but I am confused how to do that. As if the person doesn't have some licenses then by default I want it to be blank. Currently in this example I am using 5 states.
This is the xml file I am using to test.
<?xml version="1.0" encoding="UTF-8"?>
<People>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<first-name>Mike</first-name>
<last-name>Hewitt</last-name>
<licenses>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">IL</state>
</license>
</licenses>
</Person>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<first-name>John</first-name>
<last-name>Jhonny</last-name>
<licenses>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">NY</state>
</license>
</licenses>
</Person>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<first-name>Xmen</first-name>
<last-name>Bat</last-name>
<licenses>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">AK</state>
</license>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">CA</state>
</license>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">IL</state>
</license>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">NY</state>
</license>
<license>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">TX</state>
</license>
</licenses>
</Person>
</People>
The XSLT that I have written is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:csv="csv:csv" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="text"/>
<xsl:template match="/People">First Name,Last Name,AK,CA,IL,NY,TX,default value1,required-tag1,required-tag2,default value2,default value3
<xsl:for-each select="Person">
<xsl:value-of select="first-name"/>, <xsl:value-of select="last-name"/>,<xsl:choose>
<xsl:when test="licenses/license/state='AK'">
<xsl:value-of select="licenses/license/state"/>,
</xsl:when>
<xsl:otherwise>NA,
</xsl:otherwise></xsl:choose><xsl:choose>
<xsl:when test="licenses/license/state='CA'">
<xsl:value-of select="licenses/license/state"/>,
</xsl:when>
<xsl:otherwise>NA,
</xsl:otherwise></xsl:choose><xsl:choose>
<xsl:when test="licenses/license/state='IL'">
<xsl:value-of select="licenses/license/state"/>,
</xsl:when>
<xsl:otherwise>NA,
</xsl:otherwise></xsl:choose><xsl:choose>
<xsl:when test="licenses/license/state='NY'">
<xsl:value-of select="licenses/license/state"/>,
</xsl:when>
<xsl:otherwise>NA,
</xsl:otherwise></xsl:choose><xsl:choose>
<xsl:when test="licenses/license/state='TX'">
<xsl:value-of select="licenses/license/state"/>,
</xsl:when>
<xsl:otherwise>NA,
</xsl:otherwise></xsl:choose>DEFAULT VALUE1,<xsl:value-of select="required-tag1"/>,<xsl:value-of select="required-tag2"/>,DEFAULT VALUE2,DEFAULT VALUE3<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I tried using a for-each loop within the for-each loop for licenses but that executed 5 times for all licenses, how do I execute the condition to test for license-state 5 time and keep the format proper as per default.
This is how I want to output to be:
First Name,Last Name,AK,CA,IL,NY,TX,default value1,required-tag1,required-tag2,default value2,default value3
Mike, Hewitt,NA,NA,IL,NA,NA,DEFAULT VALUE1,some-information,some-information,DEFAULT VALUE2,DEFAULT VALUE3
John, Jhonny,NA,NA,NA,NY,NA,DEFAULT VALUE1,some-information,some-information,DEFAULT VALUE2,DEFAULT VALUE3
Xmen, Bat,AK,CA,IL,NY,TX,DEFAULT VALUE1,some-information,some-information,DEFAULT VALUE2,DEFAULT VALUE3
I am using XSLT version 1.0.
Thank you, for helping.