1

I want to reproduce this example in Java sample app:

http://www.w3schools.com/Xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

This is my sample app:

  • input.xml - contains the content that was shown in w3schools site
  • transform.xslt- contains the content that was shown in w3schools site
  • output.xml - just empty file. will be filled by content as xslt-transformation result

All files are locateed in my /myproject/...resource/myclasspath directory. [I use getResourceAsStream])

Here it is, the sample app:

public class TestMain {

    public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();

        InputStream xstlStream = TestMain.class.getResourceAsStream("transform.xslt");

        Source xslt = new StreamSource(xstlStream);
        Transformer transformer = factory.newTransformer(xslt);

        InputStream inputStream = TestMain.class.getResourceAsStream("input.xml");

        Source inputSource = new StreamSource(inputStream);

        URL url = TestMain.class.getResource("output.xml");

        transformer.transform(inputSource, new StreamResult(new File(url.toURI())));
    }

}

When I start this app, It still does not have any content in output.xml. I was exception the same result that was shown on w3schools site.

Basically, for may particular case I just want to add attribute to <data></data> tag, to have output like this <data attr=""></data>. But first start with this sample app.

Q: Why does it not work?

4

1 に答える 1

0

解決策:ソースフォルダではなく、コンパイルされたフォルダを見てください。期待されるコンテンツを含む出力ファイルがあります。

于 2013-05-13T12:52:49.880 に答える