We actually do just this in our program using Xalan. You can't process
the textfile directly. You must XML:ize the text first. We do it simply
by doing the following changes to the file in a separate program.

a.txt:
Dataobj1;instance1;date1;time1;number;type1;val1;val2;val3
Dataobj1;instance1;date2;time2;number;type1;val1;val2;val3

Becomes a.xml
<?xml version="1.0" encoding=" ISO-8859-1"?>
<file>
<line><![CDATA[Dataobj1;instance1;date1;time1;number;type1;val1;val2;val
3]]></line>
<line><![CDATA[Dataobj1;instance1;date2;time2;number;type1;val1;val2;val
3]]></line>
</file>

Here is the whole code for it in C++:

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char** argv)
{
  string line;
  int nLine = 0;

  if (!cin.eof()) {
    getline(cin, line);
    nLine++;

    if (line.substr(0, 5) == "<?xml") {
      // Do plain copy
      cout << line;
      while (!cin.eof()) {    
        getline(cin, line);
        nLine++;
        cout << line << endl;
      }
    }

    else {
      cout << "<?xml version='1.0' encoding='ISO-8859-1'?>" << endl;
      cout << "<file>" << endl;
      cout << "  <line><![CDATA[" << line << "]]></line>" << endl;
      while (!cin.eof()) {
        getline(cin, line);
        nLine++;
        cout << "  <line><![CDATA[" << line << "]]></line>" << endl;
      }
      cout << "</file>" << endl;
    }
  }
}

The transform looks something like this (this is for a fixed length
record file but you'll get the idea):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" version="1.0" encoding="ISO-8859-1"
indent="no" />
  <xsl:template match="/">
    <records>
      <xsl:for-each select="file/line">
        <xsl:choose>
          <xsl:variable name="rectype" select="substring(text(),1,2)"/>
          <xsl:when test="$rectype='00'">
            <rec00 reference="{substring(text(),3,6)}"
name="{substring(text(),9,33)}" date="{substring(text(),50,6)}" />
          </xsl:when>
          <xsl:when test="$rectype ='10'">
            <rec10 reference="{substring(text(),3,6)}"
name="{substring(text(),9,33)}" />
          </xsl:when>
          <xsl:when test="$rectype ='20'">
            <rec20 reference="{substring(text(),3,6)}"
account="{substring(text(),9,10)}" />
          </xsl:when>
          <xsl:when test="$rectype ='30'">
            <rec30 reference="{substring(text(),3,6)}"
date="{substring(text(),19,6)}" account="{substring(text(),9,10)}" />
          </xsl:when>
          <xsl:when test="$rectype ='40'">
            <rec40 amount="{substring(text(),28,13)}"
refno="{substring(text(),3,25)}" />
          </xsl:when>
          <xsl:when test="$rectype ='50'">
            <rec50 reference="{substring(text(),3,6)}"
date="{substring(text(),19,6)}" rowcount="{substring(text(),25,7)}"
sum="{substring(text(),32,15)}" account="{substring(text(),9,10)}" />
          </xsl:when>
          <xsl:when test="$rectype ='90'">
            <rec90 reference="{substring(text(),3,6)}"
date="{substring(text(),19,6)}" rowcount="{substring(text(),25,7)}"
sum="{substring(text(),32,15)}" />
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
    </records>
  </xsl:template>
</xsl:stylesheet>

Enjoy
/ Erik

> -----Original Message-----
> From: Arunkumar Halebid [mailto:[EMAIL PROTECTED]
> Sent: den 17 juni 2004 06:19
> To: xalan-c-users@xml.apache.org
> Subject: transforming text input
> 
> Dear Friends,
> 
> Is there a way to convert/transform a text source to directly to XML
using
> stylesheets and Xalan C++ ?
> * I want to build a cmd line utility that behaves like >transform
a.txt to
> a.xml using a.xsl
> 
> I have input source that looks like:
> -----
> Dataobj1;instance1;date1;time1;number;type1;val1;val2;val3
> Dataobj1;instance1;date2;time2;number;type1;val1;val2;val3
> ...
> 
> -----
> and wish to convert this text stream to something like:
> 
> <file>
> <header>
>       <period Date='date1' Time='time1' />
> </header>
> <mData>
> <mValue dn="Dataobj1=instance1" Type="type1">
>       <mRes>val1 val2 val3</mRes>
> </mVale>
> <mValue dn="Dataobj1=instance2" Type="type1">
>       <mRes>val1 val2 val3</mRes>
> </mData>
> ...
> <footer>
>       <period Date='date2' Time='time2' />
> </footer>
> </file>
> 
> Any leads would be helpful.
> 
> regards,
> Arun

Reply via email to