Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tab-Delimited to XML?

Status
Not open for further replies.

SMerrill

Programmer
Jan 19, 2002
145
US
I would like to translate a tab-delimited file into XML using XSL.
I would like to prepend and append an XML wrapper to it, then use string functions to parse it. However, I'm not skilled in this, and have wasted much time. The notation "tab" means that there is a tab character in the line.
By the way, you see five columns, but there could be more.
You see three rows, but there could be more than fifty.

Thanks a lot,
--SMerrill

Here is the source file:
[tt]
<?xml version=&quot;1.0&quot;?>
<root>

RUNORDtabPRESSINtabFLOW_INtabBACK_PRtabMETHANE
5tab300tab20tab137.5tab8.6
20tab300tab20tab137.5tab10.1
28tab800tab500tab137.5tab19.8

</root>
[/tt]
Here is the desired result:
[tt]
<?xml version=&quot;1.0&quot;?>
<root>
<titles>
<title>RUNORD</title>
<title>PRESSIN</title>
<title>FLOW_IN</title>
<title>BACK_PR</title>
<title>METHANE</title>
</titles>
<row>
<RUNORD>5</RUNORD>
<PRESSIN>300</PRESSIN>
<FLOW_IN>20</FLOW_IN>
<BACK_PR>137.5</BACK_PR>
<METHANE>8.6</METHANE>
</row>
<row>
<RUNORD>20</RUNORD>
<PRESSIN>300</PRESSIN>
<FLOW_IN>20</FLOW_IN>
<BACK_PR>137.5</BACK_PR>
<METHANE>10.1</METHANE>
</row>
<row>
<RUNORD>28</RUNORD>
<PRESSIN>800</PRESSIN>
<FLOW_IN>500</FLOW_IN>
<BACK_PR>137.5</BACK_PR>
<METHANE>19.8</METHANE>
</row>
</root>
[/tt]
 
this is pretty simple to do but looks a bit messy.

this code should split the top line into the format you want. the complete answer might just confuse you more.

<!-- Start of split template -->
<xsl:template name=&quot;split&quot;>
<xsl:param name=&quot;list&quot;/>
<xsl:param name=&quot;tag&quot; select=&quot;'title'&quot;/>
<!-- remove '-'s from ascii code -->
<xsl:param name=&quot;sep&quot; select=&quot;'&-#-0-9-;'&quot;/>


<xsl:variable name=&quot;var&quot;
select=&quot;substring-before($list,$sep)&quot;/>
<xsl:choose>
<xsl:when test=&quot;string-length($var) > 0&quot;>
<xsl:element name=&quot;{$tag}&quot;><xsl:value-of select=&quot;$var&quot;/></xsl:element>
<xsl:call-template name=&quot;split&quot;>
<xsl:with-param name=&quot;list&quot;>
<xsl:value-of select=&quot;normalize-space(substring-after($list,$sep))&quot;/>
</xsl:with-param>
<xsl:with-param name=&quot;tag&quot;>
<xsl:value-of select=&quot;$tag&quot;/>
</xsl:with-param>
<xsl:with-param name=&quot;sep&quot;>
<xsl:value-of select=&quot;$sep&quot;/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:element name=&quot;{$tag}&quot;><xsl:value-of select=&quot;$list&quot;/></xsl:element>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
<!-- end of split template -->

you can call this template with

<xsl:call-template name=&quot;split&quot;>
<xsl:with-param name=&quot;list&quot;>
<xsl:value-of select=&quot;$topline&quot;/>
</xsl:with-param>
<xsl:call-template>

where $topline would just be your first line.

the code will split on tabs only, and will strip out any extra spaces before or after words.
 
Mr. Tom:
I have done XML for about 1.5 years so far. When I loaded this into XML Spy, it choked on the part just after the first ampersand in the line below, saying the file was not well formed:

<xsl:param name=&quot;sep&quot; select=&quot;'&-#-0-9-;'&quot;/>

Could you please be so kind as to give me the &quot;complete answer?&quot; Any confusion that may result will be quickly replaced by learning the techniques presented.

Thank you so much,
--Shaun Merrill
 
the comment on the line above says to remove the &quot;-&quot; from the ascii code. i put them in so this site didn't process the code - which is the ascii code for a tab. the code is untested anyway so might contain a couple of errors.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top