Loading the stylesheet with Java

M

Michael

Hello

I am trying to write a Java-Program which converts a XML-file in a HTML.
It should take the Transformation-file from the XML-file itself.

Below find a possible XML-file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by Michael
Herren (private) -->
<?xml-stylesheet type="text/xsl" href="ch/qXMLsigner/files/order.xslt"
type="text/xsl?>
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ch/qXMLsigner/files/order.xsd">
<address>
<name>Herren</name>
<firstname>Michael</firstname>
<street>Postgasse 62</street>
<zip>3011</zip>
<place>Bern</place>
</address>
<item>
<quantity>2</quantity>
<productcode>684562</productcode>
<description>DVD "Cruel Intentions"</description>
<unitprice>25</unitprice>
<price>50</price>
</item>
<conditions>
<discount>
<percent>5</percent>
<amount>3.9</amount>
</discount>
<fees>10</fees>
<currency>CHF</currency>
</conditions>
<totalamount>84.1</totalamount>
</order>

When I try to load the stylesheet a Java-Exception is thrown. Below find
the Java code:

XMLReader reader =
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
InputSource inputSource = new InputSource(new
FileInputStream("ch/qXMLsigner/files/order.xml"));
reader.parse(inputSource);
SAXSource xmlDocument = new SAXSource(reader, inputSource);
transformXMLItem.setEnabled(true);
TransformerFactoryImpl transFactory = new TransformerFactoryImpl();
Source xsltSource = transFactory.getAssociatedStylesheet(xmlDocument,
null, null, null);

And the exception looks like:

Failed while looking for xml-stylesheet PI
java.io.IOException: The system cannot find the path specified
java.io.IOException: The system cannot find the path specified
at java.io.FileInputStream.read(Native Method)
at
org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.read(Unknown
Source)
at
org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at
org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:145)
at net.sf.saxon.event.Sender.send(Sender.java:50)
at
net.sf.saxon.TransformerFactoryImpl.getAssociatedStylesheet(TransformerFactoryImpl.java:171)
at ch.qXMLsigner.viewer.Viewer.actionPerformed(Viewer.java:289)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
at javax.swing.AbstractButton.doClick(AbstractButton.java:289)
at
javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1113)
at
javax.swing.plaf.basic.BasicMenuItemUI$MouseInputHandler.mouseReleased(BasicMenuItemUI.java:943)
at java.awt.Component.processMouseEvent(Component.java:5134)
at java.awt.Component.processEvent(Component.java:4931)
at java.awt.Container.processEvent(Container.java:1566)
at java.awt.Component.dispatchEventImpl(Component.java:3639)
at java.awt.Container.dispatchEventImpl(Container.java:1623)
at java.awt.Component.dispatchEvent(Component.java:3480)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
at java.awt.Container.dispatchEventImpl(Container.java:1609)
at java.awt.Window.dispatchEventImpl(Window.java:1590)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)

If anyone could help me I would be very thanksful.

Kind Regards
Michael
 
M

Michael

John said:
^ missing "

has the directory which contains this file a dir ch?

John

John,

Yes. As you can see in the code, the corresponding XML-schema is in the
same directory. And I'm able to validate the XML-file.

I also removed the second type="text/xsl" attribute. But it still does
not work.

Thank you for your help
 
M

Michael

John said:
^ missing "

has the directory which contains this file a dir ch?

John
When I use the following code, everything works fine:

Source xmlSource = new StreamSource("ch/qXMLsigner/files/order.xml");
Source xsltSource = new StreamSource("ch/qXMLsigner/files/order.xslt");
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer(xsltSource);
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
transformer.transform(xmlSource, new StreamResult(oStream));

And the path is the same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top