Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Xalan Help

843834Jun 12 2005 — edited Aug 30 2005
Dear All:

Hi, anyone have idea XSLTInputSource in xalan_1_0_0 is replace by what under xalan-j_2_6_0? or anyone can please guide me how to input stylesheet (which is in Document object) . Thanks


:~)

Comments

843834
I don't use Xalan explicitly, but I'd imagine the newer versions support the Source interface <http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/Source.html> so you'd want one of DOMSource, SAXSource, StreamSource. DOMSource if it's in a DOM Document.

Pete
843834
hi all:

i am totally new to xalan. in xalan 1_0_0, XSLTInputsource can take in the input xsl file as a document like this:

XSLTInputSource xmlIn = new XSLTInputSource(xmlDoc);
XSLTInputSource xslIn = new XSLTInputSource(xslDoc);

XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
processor.process(xmlIn, xslIn, xmlOut);
baos.close();
String result = baos.toString();

the first line is taking another xml file and the second line is taking the xsl file.
i ffind the DOMSource, but there seem no method support taking in document, can anyone please help, i am not familiar in java and also xalan

Thanks for helping
843834
The DOMSource constructor takes a Node argument; Document is a subtype of Node:
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    final Source xmlIn = new DOMSource(xmlDoc);
    final Source xslIn = new DOMSource(xslDoc);
    final Result xmlOut = new StreamResult(baos);
    
    final TransformerFactory transformerFactory = TransformerFactory.newInstance();
    final Transformer transformer = transformerFactory.newTransformer(xslIn);
    
    transformer.transform(xmlIn, xmlOut);
    
    baos.close();
    String result = baos.toString("UTF-8");
843834
Hi:

first of all, thanks for your code and help.
but how come i got this error?

javax.xml.transform.TransformerConfigurationException: javax.xml.transform.Trans
formerException: javax.xml.transform.TransformerException: stylesheet requires attribute: version

thanks
843834
The root element in your XSL file needs to contain a verison number:
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- your code goes here -->
</xsl:stylesheet>
Cheers, Neil
843834
Hi:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output version="1.0" indent="yes" encoding="UTF-8" omit-xml-declaration="no" method="xml"/>

this is my xsl heading, but i still got the same error message

Thank You
843834
A quick google might shed some clues?

http://www.google.co.uk/search?q=stylesheet+requires+attribute%3A+version

Could be something to do with the parser / transformer implementation?

Cheers, Neil
843834
hi:

i have try to find in google, but i cant find any solution. :(

public static Document transformXML(Document xmlDoc, Document xslDoc){

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Source xmlIn = new DOMSource(xmlDoc);
Source xslIn = new DOMSource(xslDoc);
Result xmlOut = new StreamResult(baos);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xslIn);
transformer.transform(xmlIn, xmlOut);

baos.close();

String result = baos.toString("UTF-8");
}

this is my java code and my xsl heading is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:output method="xml"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

but still always give me this exception:
****************************************************************************************
javax.xml.transform.TransformerConfigurationException: javax.xml.transform.Trans
formerException: javax.xml.transform.TransformerException: stylesheet requires a
ttribute: version
***************************************************************************************

anyone know how to solve? please help!!.

Thanks
843834
I would suggest printing out the full path of your XSLT file just before the exception is thrown. Perhaps your application is attempting to read an old file without the correct attributes?

Also, perhaps you could post the stacktrace, wrapped in [ code ] blocks, and then show the associated line numbers from your code.

If you are using an IDE, then perhaps you could add a breakpoint just before the exception is thrown, and check all variables contain the correct information?

Cheers, Neil
843834
Hi:

thanks for your reply, did you mean i print the whole string out?

sorry, i don't really understand....i am a newbie to java and XSL also...

can elaborate more?

Thanks
843834
hi:

has anyone really try the transform and can successfully got the result?

i try with another code:


File xmlFile = new File("C:/a.xml");
File xsltFile = new File("C:/b.xsl");

Source xmlSource = new StreamSource(xmlFile);
Source xsltSource = new StreamSource(xsltFile);

TransformerFactory transFact =
TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);

trans.transform(xmlSource, new StreamResult(System.out));

for this case, it does not give me that exception, (I also no idea why)
but it only print out this:

<?xml version="1.0" encoding="UTF-8"?>
then it stop..

anyone have other way to do transformation using xsl...please help...:'(

Thank You
843834
Try the following.
TraxTester class:
import java.io.InputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class TraxTester {

	public static void main(String[] args) throws Exception {
		// for this example, files are in the same directory as this class
		InputStream ixml = TraxTester.class.getResourceAsStream("test.xml");
		InputStream ixsl = TraxTester.class.getResourceAsStream("test.xsl");
		
		StreamSource xml = new StreamSource(ixml);
		StreamSource xsl = new StreamSource(ixsl);
		StreamResult out = new StreamResult(System.out);
		
		TransformerFactory factory = TransformerFactory.newInstance();
		Transformer transformer = factory.newTransformer(xsl);
		
		// print out implementing classes for reference
		System.out.println("TransformerFactory: "+factory.getClass().getName());
		System.out.println("Transformer: "+transformer.getClass().getName());
				
		transformer.transform(xml, out);
	}

}
test.xml:
<root>
	<line>Hello</line>
	<line>World</line>
</root>
test.xsl:
<xsl:stylesheet 
	version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	
	<xsl:output method="html"/>

	<xsl:template match="root">
		<html>
			<head></head>
			<body>
				<xsl:apply-templates/>				
			</body>
		</html>

	</xsl:template>
	
	<xsl:template match="line">
		<xsl:value-of select="."/><br/>
	</xsl:template>
	
</xsl:stylesheet>
What classes are reported for TransformerFactory and Transformer?
Cheers, Neil
843834
hi neil:

I am transforming xhtml to xml, not xml to html, so the xsl heading, do i still need to change the html?

and i try the code and i got these error:

Exception in thread "main" javax.xml.transform.TransformerConfigurationException
: javax.xml.transform.TransformerConfigurationException: javax.xml.transform.Tra
nsformerException: java.lang.NullPointerException
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(Tran
sformerFactoryImpl.java:805)
at TraxTester.main(TraxTester.java:20)
Caused by: javax.xml.transform.TransformerConfigurationException: javax.xml.tran
sform.TransformerException: java.lang.NullPointerException
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(Transf
ormerFactoryImpl.java:984)
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(Tran
sformerFactoryImpl.java:788)
... 1 more
Caused by: javax.xml.transform.TransformerException: java.lang.NullPointerExcept
ion
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(Transf
ormerFactoryImpl.java:980)
... 2 more
Caused by: java.lang.NullPointerException
at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:691)
at org.apache.crimson.parser.Parser2.parse(Parser2.java:337)
at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:448)

at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(Transf
ormerFactoryImpl.java:972)......
......

is it that transformation is only use to convert xml to html?

my xsl file is like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/html/body/table[5]/tr/td/table/tr[3]/td/table/tr/td[2]/table">
<Username>
<xsl:value-of select="tr[2]/td[2]/b"/>
</Username>
<Address>
<xsl:value-of select="tr[3]/td[2]/b"/>
</Address>
</xsl:template>


Thanks for your help!!

:~ )
843834
I would suggest creating a small test program, with a simple XML and XSLT file, then slowly add more steps until you uncover your error.

From your stacktrace, it appears that you may be attempting to create a transformer where the StreamSource is null.

Cheers, Neil
843834
Check my blog entry:
http://photeus.com:8080/roller/page/gary_kephart/20050829#transformerexception_stylesheet_requires_attribute_version
1 - 15
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 27 2005
Added on Jun 12 2005
15 comments
252 views