How to Convert XML to CSV Using XSL

How to Convert XML to CSV Using XSL thumbnail
XML is a standard recommended by W3C.

The increasing use of XML technologies in various applications has resulted in the need to output XML files in multiple formats so legacy systems can reuse them. To many programmers and IT professionals, this conversion is often time-consuming and requires careful considerations of the data structures embedded in the XML document. EXtensible Stylesheet Language (XSL) assists programmers in transforming XML files into HTML.

Instructions

    • 1

      Displaying XML requires the use of eXtensible Stylesheet Language Transformations (XSLT), which is the recommended style sheet language of XML by World Wide Web Consortium (W3C) and is more sophisticated than Cascading Style Sheet (CSS). XSLT is often used to transform XML into HTML before it is displayed by a browser. An example of an XML document/file (i.e., sample.xml) is shown below:

      <?xml version="1.0" encoding="UTF-8"?>
      <students>
      <student id="10001">
      <name given="Mark" family="Smith"/>
      <gender>Male</gender>
      <address street="1123 Buffalo Street" city="Indianapolis" state="IN"/>
      <emailAddress>marksmith@abc.com</emailAddress>
      <phoneNumber>555-555-5553</phoneNumber>
      </student>
      .....
      .....
      </students>

      In the given sample.xml file, you can transform the XML using the following XSL source code (i.e., sample.xsl):

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns="http://www.w3.org/1999/xhtml">
      <xsl:template match="/">
      <html>
      <table>
      <xsl:for-each select="//student">
      <tr>
      <td>
      <xsl:value-of select="@id"/>
      <xsl:value-of select="','"/>
      <xsl:value-of select="name/@given"/>
      <xsl:value-of select="','"/>
      <xsl:value-of select="name/@family"/>
      <xsl:value-of select="','"/>
      <xsl:value-of select="gender"/>
      <xsl:value-of select="','"/>
      <xsl:value-of select="address/@street"/>
      <xsl:value-of select="','"/>
      <xsl:value-of select="address/@city"/>
      <xsl:value-of select="','"/>
      <xsl:value-of select="address/@state"/>
      <xsl:value-of select="','"/>
      <xsl:value-of select="emailAddress"/>
      <xsl:value-of select="','"/>
      <xsl:value-of select="phoneNumber"/>
      </td>
      </tr>
      </xsl:for-each>
      </table>
      </html>
      </xsl:template>
      </xsl:stylesheet>

    • 2

      To convert the sample.xml file to CSV, you will need to reference the above XSL style sheet in the XML document using the header information: <?xml-stylesheet type="text/xsl" href="sample.xsl"?>. The resulting XML document will look similar to the one shown below.

      <?xml version="1.0" encoding="UTF-8"?>
      <?xml-stylesheet type="text/xsl" href="sample.xsl"?>
      <students>
      <student id="10001">
      <name given="Mark" family="Smith"/>
      <gender>Male</gender>
      <address street="1123 Buffalo Street" city="Indianapolis" state="IN"/>
      <emailAddress>marksmith@abc.com</emailAddress>
      <phoneNumber>555-555-5553</phoneNumber>
      </student>
      <student id="10002">
      <name given="Jane" family="Doe"/>
      <gender>Female</gender>
      <address street="1271 Buffalo Street" city="Indianapolis" state="IN"/>
      <emailAddress>janedoe@abc.com</emailAddress>
      <phoneNumber>555-555-5554</phoneNumber>
      </student>
      <student id="10003">
      <name given="John" family="Smith"/>
      <gender>Male</gender>
      <address street="1281 Buffalo Street" city="Indianapolis" state="IN"/>
      <emailAddress>johnsmith@abc.com</emailAddress>
      <phoneNumber>555-555-5555</phoneNumber>
      </student>
      </students>

    • 3

      When you open the above sample.xml source code in a browser, you will generate a CSV output in the browser, similar to the one shown below:

      10001,Mark,Smith,Male,1123 Buffalo Street,Indianapolis,IN,marksmith@abc.com,555-555-5553
      10002,Jane,Doe,Female,1271 Buffalo Street,Indianapolis,IN,janedoe@abc.com,555-555-5554
      10003,John,Smith,Male,1281 Buffalo Street,Indianapolis,IN,johnsmith@abc.com,555-555-5555

Tips & Warnings

  • There are other approaches for converting XML data to CSV. The choice will depend on the type of project you are working on. For example, if you are working with an XML data that is stored on MySQL database, then you can easily export the data from MySQL to an Excel spreadsheet and then convert the resulting spreadsheet to CSV.

Related Searches:

References

  • Photo Credit Jason Reed/Photodisc/Getty Images

Comments

Related Ads

Featured