我知道我可以将参数从 Java 传递到 xsl 转换,但是在转换完成后如何读取变量/参数?
例如,我有一个这样的 xml 文档:
<author>
<name>Barry Bonds</name>
<books>
<book>Hitting Home Runs</book>
<book>Cheating at Baseball</book>
<book>Nobody Likes Cooperstown Anyway</book>
<books>
<author>
在单个转换中,我希望能够转换 name 元素并将其存储为变量,还可以转换 book 元素并将它们存储为第二个变量。然后我想在转换后从 Java 代码中访问这两个变量。
这是可能的,如果是这样,它是如何完成的?
感谢迈克尔凯的回答,我现在有了一个解决方案。对于遇到此问题的其他人,这里有一个单元测试显示它是如何工作的:
import net.sf.saxon.*;
import org.junit.Test;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class SimpleTransform {
@Test
public void splitOutput() throws TransformerException, UnsupportedEncodingException {
final Map<String, StreamResult> results = new HashMap<String, StreamResult>();
results.put("test1", new StreamResult(new ByteArrayOutputStream()));
results.put("test2", new StreamResult(new ByteArrayOutputStream()));
results.put("test3", new StreamResult(new ByteArrayOutputStream()));
TransformerFactoryImpl factory = new TransformerFactoryImpl();
Source source = new StandardURIResolver(new Configuration()).resolve("transform.xslt", null);
Controller xsl = (Controller) factory.newTransformer(source);
xsl.setOutputURIResolver(new OutputURIResolver() {
@Override
public Result resolve(String s, String s1) throws TransformerException {
return results.get(s);
}
@Override
public void close(Result result) throws TransformerException {
}
});
Source xsd = (Source) Transform.loadDocuments("input.xml", false, null, false);
StreamResult standardResult = new StreamResult(new ByteArrayOutputStream());
xsl.transform(xsd, standardResult);
for (Map.Entry<String, StreamResult> stringStreamResultEntry : results.entrySet()) {
System.out.println(stringStreamResultEntry.getKey() + ": " + ((ByteArrayOutputStream)stringStreamResultEntry.getValue().getOutputStream()).toString("UTF-8"));
}
assertEquals("<html>\n <body>test1</body>\n</html>", ((ByteArrayOutputStream) results.get("test1").getOutputStream()).toString("UTF-8"));
assertEquals("<html>\n <body>test2</body>\n</html>", ((ByteArrayOutputStream) results.get("test2").getOutputStream()).toString("UTF-8"));
assertEquals("<html>\n <body>test3</body>\n</html>", ((ByteArrayOutputStream) results.get("test3").getOutputStream()).toString("UTF-8"));
}
}