Loading the stylesheet with Java

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    Loading the stylesheet with Java

    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:noNamespace SchemaLocation= "ch/qXMLsigner/files/order.xsd">
    <address>
    <name>Herren</name>
    <firstname>Mich ael</firstname>
    <street>Postgas se 62</street>
    <zip>3011</zip>
    <place>Bern</place>
    </address>
    <item>
    <quantity>2</quantity>
    <productcode>68 4562</productcode>
    <description>DV D "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 =
    XMLReaderFactor y.createXMLRead er("org.apache. xerces.parsers. SAXParser");
    InputSource inputSource = new InputSource(new
    FileInputStream ("ch/qXMLsigner/files/order.xml"));
    reader.parse(in putSource);
    SAXSource xmlDocument = new SAXSource(reade r, inputSource);
    transformXMLIte m.setEnabled(tr ue);
    TransformerFact oryImpl transFactory = new TransformerFact oryImpl();
    Source xsltSource = transFactory.ge tAssociatedStyl esheet(xmlDocum ent,
    null, null, null);

    And the exception looks like:

    Failed while looking for xml-stylesheet PI
    java.io.IOExcep tion: The system cannot find the path specified
    java.io.IOExcep tion: The system cannot find the path specified
    at java.io.FileInp utStream.read(N ative Method)
    at
    org.apache.xerc es.impl.XMLEnti tyManager$Rewin dableInputStrea m.read(Unknown
    Source)
    at
    org.apache.xerc es.impl.XMLEnti tyManager.setup CurrentEntity(U nknown Source)
    at
    org.apache.xerc es.impl.XMLVers ionDetector.det ermineDocVersio n(Unknown
    Source)
    at org.apache.xerc es.parsers.XML1 1Configuration. parse(Unknown Source)
    at org.apache.xerc es.parsers.DTDC onfiguration.pa rse(Unknown Source)
    at org.apache.xerc es.parsers.XMLP arser.parse(Unk nown Source)
    at org.apache.xerc es.parsers.Abst ractSAXParser.p arse(Unknown Source)
    at net.sf.saxon.ev ent.Sender.send SAXSource(Sende r.java:145)
    at net.sf.saxon.ev ent.Sender.send (Sender.java:50 )
    at
    net.sf.saxon.Tr ansformerFactor yImpl.getAssoci atedStylesheet( TransformerFact oryImpl.java:17 1)
    at ch.qXMLsigner.v iewer.Viewer.ac tionPerformed(V iewer.java:289)
    at
    javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:1764)
    at
    javax.swing.Abs tractButton$For wardActionEvent s.actionPerform ed(AbstractButt on.java:1817)
    at
    javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel.java: 419)
    at
    javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:257)
    at javax.swing.Abs tractButton.doC lick(AbstractBu tton.java:289)
    at
    javax.swing.pla f.basic.BasicMe nuItemUI.doClic k(BasicMenuItem UI.java:1113)
    at
    javax.swing.pla f.basic.BasicMe nuItemUI$MouseI nputHandler.mou seReleased(Basi cMenuItemUI.jav a:943)
    at java.awt.Compon ent.processMous eEvent(Componen t.java:5134)
    at java.awt.Compon ent.processEven t(Component.jav a:4931)
    at java.awt.Contai ner.processEven t(Container.jav a:1566)
    at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:3639)
    at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1623)
    at java.awt.Compon ent.dispatchEve nt(Component.ja va:3480)
    at
    java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:3450)
    at
    java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3165)
    at java.awt.Lightw eightDispatcher .dispatchEvent( Container.java: 3095)
    at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1609)
    at java.awt.Window .dispatchEventI mpl(Window.java :1590)
    at java.awt.Compon ent.dispatchEve nt(Component.ja va:3480)
    at java.awt.EventQ ueue.dispatchEv ent(EventQueue. java:450)
    at
    java.awt.EventD ispatchThread.p umpOneEventForH ierarchy(EventD ispatchThread.j ava:197)
    at
    java.awt.EventD ispatchThread.p umpEventsForHie rarchy(EventDis patchThread.jav a:150)
    at
    java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:144)
    at
    java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:136)
    at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:99 )

    If anyone could help me I would be very thanksful.

    Kind Regards
    Michael

  • Michael

    #2
    Re: Loading the stylesheet with Java

    John Bokma wrote:[color=blue]
    > Michael wrote:
    >[color=green]
    >> 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?>[/color]
    >
    >
    > ^ missing "
    >
    > has the directory which contains this file a dir ch?
    >
    > John
    >[/color]

    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

    Comment

    • Michael

      #3
      Re: Loading the stylesheet with Java

      John Bokma wrote:[color=blue]
      > Michael wrote:
      >[color=green]
      >> 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?>[/color]
      >
      >
      > ^ missing "
      >
      > has the directory which contains this file a dir ch?
      >
      > John
      >[/color]
      When I use the following code, everything works fine:

      Source xmlSource = new StreamSource("c h/qXMLsigner/files/order.xml");
      Source xsltSource = new StreamSource("c h/qXMLsigner/files/order.xslt");
      TransformerFact ory transFactory = TransformerFact ory.newInstance ();
      Transformer transformer = transFactory.ne wTransformer(xs ltSource);
      ByteArrayOutput Stream oStream = new ByteArrayOutput Stream();
      transformer.tra nsform(xmlSourc e, new StreamResult(oS tream));

      And the path is the same.

      Comment

      Working...