You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by SP/Nowotny Florian <fn...@vfst.de> on 2004/01/16 16:00:19 UTC
transforming with istream
Hi everybody.
I'm using C++ with Xalan 1.4 (and Xerces 2.1).
Did anybody take a std::istream as XSLTInputSource for the XalanTransformer.transform(.,.,.) method?
Like the sample code below my program takes two files, converts the xml file into an std::ifstream and should transform it into a third file.
Why do I get "Fatal Error at (file <unknown>, line 1, column 1): Invalid document structure"?
2nd attempt in code: It's the same problem when I use the buffer of the ifstream as the buffer of an std::istream.
3rd attempt: using a stringstream allows it to fill the buffer character by character.
Here I get the problem that Xalan seems to stop reading before the stream is ending. Why?
3rd extended: when I copy the str() into itself the transformation works. Is this how to do it?
By the way: It doesn't matter wether I take the xml oder the xsl file as istream (or both of them).
//
// testing C++ xalan/xerces with istream
//
#include <Include/PlatformDefinitions.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <XalanTransformer/XalanTransformer.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main( int argc, const char* argv[] )
{
try
{
if ( argc < 4 )
{
cerr << "------------------------------------------------------------" << endl
<< "Usage: " << endl
<< "tstxx xml xsl out" << endl
<< endl
<< " xml filename " << endl
<< " xsl filename " << endl
<< " out filename " << endl
<< "------------------------------------------------------------" << endl;
return 1;
}
else
{
// create Xalan instance
XMLPlatformUtils::Initialize();
XalanTransformer::initialize();
XalanTransformer* pXalan = new XalanTransformer();
if( pXalan == NULL )
{
cerr << "Error: no XalanTransformer ";
return -1;
}
cout << "Xalan is ready." << endl;
// 1st attempt
// open XML file
ifstream xml;
xml.open( argv[1], ios::in );
if( ! xml.is_open() )
{
cerr << argv[1] << " not opened!" << endl;
return -1;
}
cout << "xml file open" << endl;
// 2nd attempt
/*
// xml data into istream
istream is( xml.rdbuf() );
// show buffer content
cout << is.rdbuf() << endl;
// reset get pointer
is.seekg( 0, ios::beg );
cout << "istream get position is " << is.tellg() << endl;
cout << "istream good? " << is.good() << endl;
*/
// 3rd attempt
/*
stringstream ss;
char c;
while ( xml.get(c) )
ss.put(c);
cout << "stringstream get position is " << ss.tellg() << endl;
cout << "stringstream good? " << ss.good() << endl;
// uncomment this and no.3 works, why?
//ss.str( ss.str() );
*/
// Choose the right one
XSLTInputSource in_xml( &xml );
//XSLTInputSource in_xml( &is );
//XSLTInputSource in_xml( &ss );
// xsl input source, should be istream later
XSLTInputSource in_xsl( argv[2] );
// xml result, should be ostream later
XSLTResultTarget out( argv[3] );
int result = pXalan->transform( in_xml, in_xsl, out );
if(result != 0)
{
cerr << "Xalan Error: \n" << pXalan->getLastError() << endl;
return -1;
}
}
}
catch( exception& e )
{
cerr << "exception: " << e.what() << endl;
}
catch( ... )
{
cerr << "unknown exception!" << endl;
}
return 0;
}
This is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<befehl>
something
</befehl>
</doc>
This is my XSL file:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="befehl">
<tag>
<xsl:value-of select="text()"/>
</tag>
</xsl:template>
</xsl:stylesheet>