473,287 Members | 3,181 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Help! 'dummy.xsl' ?

As various people will have noticed, I've been having a lot of trouble with
XSL lately. Brief history: I wrote myself an XML toolkit back in 2000, and
it worked well enough for me, so it's been little changed since. However,
it works only with an obsolete version of Saxon (6.2.2 I think), and it
has a number of small bugs; and I've at last got to the point where I need
to fix it. And I'm finding it, frankly, absurdly frustrating and
difficult.

But what's frustrating me at this moment is something absurd and weird.
Some weeks ago I did a test with a dummy XSL file called 'dummy.xsl'. Now,
whenever I test with modern Xalan2, I'm persistently getting an error
message:

/home/simon/tmp/test/dummy.xsl; Line #0; Column #0; stylesheet requires
attribute: version

I am really, really not referring to a file called 'dummy.xsl'. I have
grepped through every single file in my working set, and nothing - really
nothing at all - contains the text 'dummy.xsl'. Also, the stylesheet I am
using, really does have a version attribute - see below.

I do not get this message when I'm using my obsolete version of Saxon (and,
indeed, all my transforms work with my obsolete Saxon). So I guessed that
Xalan might use a cache somewhere on my hard disk that I don't know about.
I copied my working files - all three of them, stripping my test down as
far as I could - to my laptop, which should be virgin.

When I run the test, on the laptop, using Xalan2, using GCJ as the Java
implementation I get the 'stylesheet requires attribute: version' error,
but no reference to dummy.xsl. But if I use IBM's Java, I get exactly the
same error message as I get on my development machine - *INCLUDING* the
reference to dummy.xsl!

So I now completely fail to understand where my problem is coming from. I
include my three files below, so that anyone who has the time and patience
can verify my problem and suggest solutions (hopefully not including
either pistols at dawn or a vial of cyanide)

,----[ /home/simon/tmp/xsltest/testharness.sh ]
#!/bin/bash

# XSLPROC=/usr/share/java/xalan2.jar
XSLPROC=/usr/local/lib/java/saxon.jar

export CLASSPATH=".:$XSLPROC"

javac TransformerTestHarness.java
java TransformerTestHarness \
-d org.apache.xerces.dom.DOMImplementationImpl -s test.xsl
`----

,----[ /home/simon/tmp/xsltest/test.xsl ]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<test>
<xsl:apply-templates/>
</test>
</xsl:template>

<xsl:template match="@* node()">
<xsl:copy>
<xsl:apply-templates select="@* node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
`----

,----[ /home/simon/tmp/xsltest/TransformerTestHarness.java ]
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.xml.sax.InputSource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringReader;

import java.text.DateFormat;

import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
* Test XSL transformations
*/
public class TransformerTestHarness
{
//~ Static fields/initializers ------------------------------------------

/** A string with parsable content */
public static final String PARSETHIS =
"<parsable><p>Parse me</p></parsable>";

//~ Instance fields -----------------------------------------------------

/** the DOM Implementation I will use */
public DOMImplementation rebus = null;

/** the XSL stylesheet (transform) I will use */
public File stylesheet;

/** DOCUMENT ME! */
public String label = "Transformer test harness";

/**
* A directory into which to write output files - if null, use standard
* out
*/
protected File baseDir = null;

/** A factory class for XSL transformers; */
protected TransformerFactory transformerFactory = null;

//~ Constructors --------------------------------------------------------

/**
* Construct a new test harness
*/
public TransformerTestHarness( )
{
super( );

try
{
transformerFactory = TransformerFactory.newInstance( );
}
catch ( Exception d )
{
System.err.println( "Could not instantiate transformer: " +
d.getMessage( ) );
System.exit( 4 );
}
}

//~ Methods -------------------------------------------------------------

/**
* Generate a document
*
* @return the document
*/
public Document generate( )
{
Document doc = rebus.createDocument( "", "root", null );
Element root = doc.getDocumentElement( );
Element generated = doc.createElement( "generated" );
root.appendChild( generated );

DateFormat df = DateFormat.getDateTimeInstance( );

generated.appendChild( doc.createTextNode( df.format( new
Date( ) ) ) );

try
{
root.appendChild( maybeParse( doc, PARSETHIS ) );
}
catch ( Exception any )
{
System.err.println( "Failed to parse: " + any.getMessage( ) );
}

return doc;
}

/**
* @param args
*/
public static void main( String[] args ) throws Exception
{
TransformerTestHarness tth = new TransformerTestHarness( );

for ( int i = 0; i < args.length; i++ )
{
if ( args[i].startsWith( "-" ) && ( args[i].length( ) 1 ) )
{
switch ( args[i].charAt( 1 ) )
{
case 'b':
tth.baseDir = new File( args[++i] );

if ( tth.baseDir.exists( ) &&
tth.baseDir.canWrite( ) &&
tth.baseDir.isDirectory( ) )
{
System.err.println( "Using base directory " +
tth.baseDir.getCanonicalPath( ) );
}
else
{
System.err.println( "Bad base directory: " +
args[i] );
}

break;

case 'd':

try
{
tth.rebus =
(DOMImplementation) Class.forName( args[++i] )
.newInstance( );
System.err.println(
"Using DOM implementation of class " +
tth.rebus.getClass( ).getName( ) );
}
catch ( Exception any )
{
System.err.println( "Could not find DOM " +
"implementation " + args[i] );
System.err.println( any.getMessage( ) );
System.exit( 2 );
}

break;

case 's':
tth.stylesheet = new File( args[++i] );

if ( tth.stylesheet.exists( ) &&
tth.stylesheet.canRead( ) )
{
System.err.println( "Using XSL stylesheet " +
tth.stylesheet.getCanonicalPath( ) );
}
else
{
System.err.println( "Can't read stylesheet " +
args[i] );
System.exit( 1 );
}

break;

default:
System.err.println( "Unrecognised arg " + args[i] );
System.exit( 3 );

break;
}
}
else
{
System.err.println( "Unrecognised arg " + args[i] );
System.exit( 3 );

break;
}
}

tth.test( );
}

/**
* Get a suitable output stream - if I have a base directory, a file in
* that directory with this name; else the standard out
*
* @param name a name ofr my file if any
*
* @return an output stream
*
* @throws Exception unlikely
*/
public OutputStream getOutputStream( String name )
throws Exception
{
OutputStream out = System.out;

if ( ( baseDir != null ) && baseDir.exists( ) &&
baseDir.canWrite( ) && baseDir.isDirectory( ) )
{
out = new FileOutputStream( new File( baseDir, name ) );
}

return out;
}
/**
* Run the tests
*/
public void test( )
{
Document generated = generate( );
System.out.println( label );

try
{
System.out.println( "Printer test -justprint" );
justPrint( generated, "justprint" );

if ( stylesheet != null )
{
DocumentBuilder p =
DocumentBuilderFactory.newInstance( ).newDocumentBuilder( );
Document xslDocument =
p.parse( new InputSource( new FileInputStream( stylesheet ) ) );

System.out.println( "Verify stylesheet -transform.xsl");
justPrint( xslDocument, "transform.xsl");

System.out.println( "DOM to DOM test -dom2dom" );
domToDom( generated, xslDocument, "dom2dom" );

System.out.println( "DOM to Stream test -dom2stream" );
domToDom( generated, xslDocument, "dom2stream" );
}
else
{
System.err.println( "No stylesheet?");
}
}
catch ( Exception e )
{
System.err.println( "Whilst running print test:" );
e.printStackTrace( );
}
}

/**
* Transform from DOM object to DOM object; seralize after
*
* @param doc the document transformed
* @param name the name for printing
*/
protected void domToDom( Document doc, Document xslDocument, String
name )
{
try
{
if ( stylesheet != null )
{
DOMSource domSource = new DOMSource( doc );
DOMResult domResult = new DOMResult( );

transformerFactory.newTransformer( new DOMSource( xslDocument ) )
.transform( domSource, domResult );

Node root = domResult.getNode( );

if ( root instanceof Document )
{
justPrint( (Document) root, name );
}
else
{
System.err.println( "DOM to DOM transform returned " +
root );
}
}
}
catch ( Exception e )
{
System.err.println( "DOM to DOM failed: " + e.getMessage( ) );
e.printStackTrace( );
}
}

/**
* Transform from DOM object to output stream directly
*
* @param doc the document transformed
* @param transform the xsl transform to apply
* @param name the name for printing
*/
protected void domToStream( Document doc, Document transform, String
name )
{
try
{
if ( stylesheet != null )
{
DOMSource domSource = new DOMSource( doc );
StreamResult result =
new StreamResult( getOutputStream( name ) );

transformerFactory.newTransformer( new DOMSource( transform ) )
.transform( domSource, result );
}
}
catch ( Exception e )
{
System.err.println( "DOM to DOM failed: " + e.getMessage( ) );
e.printStackTrace( );
}
}

/**
* Just print the document
*
* @param doc the document to print
* @param name the name of the file to print to
*
* @throws Exception DOCUMENT ME!
*/
protected void justPrint( Document doc, String name )
throws Exception
{
XMLSerializer dickens =
new XMLSerializer( getOutputStream( name ),
new OutputFormat( doc, null, true ) );
dickens.serialize( doc );
}

/**
* Construct a node representing this value. It's perfectly possible (and
* possibly legitimate) that the value of a child should contain embedded
* markup. If so, try to parse a node out of it.
*
* @param doc the document in which the node is to be created
* @param unparsed the string, possibly with embedded markup, to parse
*
* @exception GenerationException if parsing fails
*/
protected Node maybeParse( Document doc, String unparsed )
throws Exception
{
Node val = doc.createTextNode( unparsed ); // safe default

if ( unparsed != null ) // defensive
{
if ( unparsed.indexOf( "<" ) -1 ) // it looks like markup
{
if ( !unparsed.trim( ).startsWith( "<" ) )
{
// nasty: if it contains markup, but
// isn't contained in markup, the
// parser will barf.
unparsed = "<parsed>" + unparsed + "</parsed>";
}

try
{
DocumentBuilder parser =
DocumentBuilderFactory.newInstance( )
.newDocumentBuilder( );

if ( parser == null )
{
System.err.println( "Could not initialise XML parser" );
}

InputSource i =
new InputSource( new StringReader( unparsed ) );

Document parsed = parser.parse( i );

if ( parsed != null )
{
Node root = parsed.getDocumentElement( );

val = doc.importNode( root, true );
}
}
catch ( Exception e )
{
System.err.println( "Could not parse '" + unparsed +
"'as XML" );
e.printStackTrace( System.err );
}
}
}

return val;
}
}
`----

,----[ Sample output with Saxon (i.e. what I think you should get) ]
Using DOM implementation of class
org.apache.xerces.dom.DOMImplementationImpl
Using XSL stylesheet /home/simon/tmp/xsltest/test.xsl
Transformer test harness
Printer test -justprint
<?xml version="1.0"?>
<root>
<generated>18-Mar-2007 14:33:26</generated>
<parsable>
<p>Parse me</p>
</parsable>
</root>
Verify stylesheet -transform.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<test>
<xsl:apply-templates/>
</test>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
DOM to DOM test -dom2dom
<?xml version="1.0"?>
<test>
<root>
<generated>18-Mar-2007 14:33:26</generated>
<parsable>
<p>Parse me</p>
</parsable>
</root>
</test>
DOM to Stream test -dom2stream
<?xml version="1.0"?>
<test>
<root>
<generated>18-Mar-2007 14:33:26</generated>
<parsable>
<p>Parse me</p>
</parsable>
</root>
</test>

,----[ Sample output with Xalan2 ]
Using DOM implementation of class
org.apache.xerces.dom.DOMImplementationImpl
Using XSL stylesheet /home/simon/tmp/xsltest/test.xsl
Transformer test harness
Printer test -justprint
<?xml version="1.0"?>
<root>
<generated>18-Mar-2007 14:34:50</generated>
<parsable>
<p>Parse me</p>
</parsable>
</root>
Verify stylesheet -transform.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<test>
<xsl:apply-templates/>
</test>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
DOM to DOM test -dom2dom
/home/simon/tmp/xsltest/dummy.xsl; Line #0; Column #0; stylesheet requires
attribute: version
file:///home/simon/tmp/xsltest/dummy.xsl; Line #0; Column #0;
java.util.EmptyStackException
DOM to Stream test -dom2stream
/home/simon/tmp/xsltest/dummy.xsl; Line #0; Column #0; stylesheet requires
attribute: version
file:///home/simon/tmp/xsltest/dummy.xsl; Line #0; Column #0;
java.util.EmptyStackException
--
si***@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

Do not sail on uphill water.
- Bill Lee

Mar 18 '07 #1
3 4207
Simon Brooke wrote:
DocumentBuilder p =
DocumentBuilderFactory.newInstance( ).newDocumentBuilder( );
I don't know whether that causes your problem but for namespace aware
processing
<http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setNamespaceAware(bool ean)>
you should do e.g.
DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder p = docBuilderFactory.newDocumentBuilder();


--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 18 '07 #2
in message <45***********************@newsspool3.arcor-online.net>, Martin
Honnen ('m********@yahoo.de') wrote:
Simon Brooke wrote:
> DocumentBuilder p =
DocumentBuilderFactory.newInstance( ).newDocumentBuilder( );

I don't know whether that causes your problem but for namespace aware
processing
<http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setNamespaceAware(bool ean)>
you should do e.g.
DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder p = docBuilderFactory.newDocumentBuilder();
Thank you very much indeed!

You may not know what my problem was, but you certainly managed to solve
it! What a /bizarre/ error message for that problem!

--
si***@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

Due to financial constraints, the light at the end of the tunnel
has been switched off.

Mar 18 '07 #3
Simon Brooke wrote:
it! What a /bizarre/ error message for that problem!
not entirely bizarre, the top level element of an xsl stylesheet can be
any element in any namespace (because of the so called literal result
element as stylesheet syntax) however in that case there has to be an
xsl:version attribute on the LRE (basically so that a processor can trap
the case of a file which isn't an xsl file at all being passed in as xsl
code) so if there's an error in specifying the namespace, the
xsl:stylesheet is just some element in an unknown mamepsace, so the
system looks for an xsl:version attribute....
David
Mar 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.