0

Xalan-J 允许我们使用它的扩展机制创建和操作 java 对象。我想要做的是从Java(从调用Xalan 的JVM)传递一个已经创建的对象,然后从xslt 操作这个对象。

例如,我想做这样的事情:

Transformer transformer = tFactory.newTransformer(new StreamSource(xsl));
HashMap mymap = ...
transformer.setParameter("MapToBeManipulatedByXsl", mymap);
transformer.transform(...)
String fromXSL = mymap.get("some-key-added-by-xsl");

这可能吗?

4

1 回答 1

2

This isn't quite the same level of integration that you get with Xalan-J, but by far the simplest way of manipulating Java objects with XSL that I've dealt with was to use JAXB to turn the objects to/from XML.

JAXB lets you annotate your Java objects with tags specifying how you want them to appear in their equivalent XML document, then does all the work of translating Java-to-XML and XML-to-Java. Take your Java object, run it through JAXB, run your XSL over it, then run it back through JAXB to get the Java object back.

The benefits of this technique include:

  • You are not tied to a particular XSL environment or interpreter. Your XSL only operates on XML.
  • The translation from Java to XML and back again is a core part of the JRE, and does not require any third party libraries
  • You can unit test your XSLs independently of the Java application environment, by passing in text files and verifying the output
于 2011-08-19T04:45:22.100 回答