Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML to Tab delimited Text

Tags:

xml

xslt

need the help from the XML, XSLT experts.

I have 2 sample records exported from a system in XML format which is shown below. I like to use XSLT to convert the data to tab delimited text as shown here https://docs.google.com/spreadsheet/ccc?key=0AgOV9Or8-zdzdG8yWTlRb1hWeUhzZG02MHA5TGxqR2c

The transformation is a bit complex for a XML novice. I would appreciate if someone could help me by creating a XSL file.

Thanks.

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<Results productTitle="XYZ DB/TextWorks" productVersion="11.00" xmlns:inm="http://www.xyz.com/webpublisher/query">
<Recordset setCount="2">
<Record setEntry="0">
<ID>282</ID>
<TIL>TIL value</TIL>
<SET>SET value</SET>
<NOA>NOA value</NOA>
<NOQ>NOQ value 1, NOQ value 2, NOQ value 3, NOQ value 4</NOQ>
<LAN>LAN value</LAN>
<CAL>CAL value</CAL>
<INP>INP value</INP>
<SUT>SUT value</SUT>
<SUG>SUG value</SUG>
<EDI>EDI value</EDI>
<MEP />
<NOG>NOG value</NOG>
<IDX>IDX value</IDX>
<SAU />
<Database>Database value</Database>
<Format>Format value 1</Format>
<Format>Format value 2</Format>
<Format>Format value 3</Format>
<Format>Format value 4</Format>
<DES />
<TYP />
<UTI />
<SUR />
<DATE />
<SystemID>530</SystemID>
</Record>
<Record setEntry="1">
<ID>373</ID>
<TIL>TIL value</TIL>
<SET>SET value</SET>
<NOA>NOA value</NOA>
<NOQ>NOQ value 1, NOQ value 2, NOQ value 3, NOQ value 4</NOQ>
<LAN>LAN value 1</LAN>
<LAN>LAN value 2</LAN>
<LAN>LAN value 3</LAN>
<CAL>CAL value</CAL>
<INP>INP value</INP>
<SUT>SUT value</SUT>
<SUG />
<EDI>EDI value</EDI>
<MEP />
<NOG>NOG value</NOG>
<IDX>IDX value</IDX>
<SAU />
<Database>Database value</Database>
<Format>Format value</Format>
<DES />
<TYP />
<UTI />
<SUR />
<DATE />
<SystemID>611</SystemID>
</Record>
</Recordset>
</Results>
like image 907
user605179 Avatar asked Nov 08 '12 15:11

user605179


People also ask

Can you convert XML to CSV?

First, you can copy and enter data of the XML file and save the data as a CSV file; secondly, you can upload an XML file to the converter and convert it to CSV without opening; Finally, it allows you to enter the URL of your XML file. You can choose the method that fits you best.


1 Answers

Something like that should work as a good starting point

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="//Recordset">
<xsl:for-each select="Record">
        <xsl:value-of select="ID"/>
        <!-- tab char -->
        <xsl:text>&#x9;</xsl:text>
        <xsl:value-of select="TIL"/>
        <xsl:text>&#x9;</xsl:text>
        <xsl:value-of select="SET"/>
        <xsl:text>&#x9;</xsl:text>
        <!-- all other fields... -->
        <xsl:value-of select="SystemID"/>
        <!-- line feed char -->
        <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
like image 96
Draykos Avatar answered Nov 16 '22 01:11

Draykos