我有一个使用 Saxon HE 9.7 进行 XML 转换的程序。
public String transform() throws TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
TransformerFactoryImpl tFactoryImpl = (TransformerFactoryImpl) factory;
Configuration saxonConfig = tFactoryImpl.getConfiguration();
Processor processor = (Processor) saxonConfig.getProcessor();
processor.registerExtensionFunction(new Employee());
Source xslt = new StreamSource(new File("mappings.xslt"));
Transformer transformer = factory.newTransformer(xslt);
Source text = new StreamSource(new File("payload.xml"));
transformer.transform(text, new StreamResult(sw));
return sw.toString();
}
扩展功能类:
public class Employee implements ExtensionFunction {
private List<HashMap<String, String>> employee = new ArrayList<HashMap<String, String>>();
private String employeeName = "John";
public List<HashMap<String, String>> getEmployee() {
HashMap<String, String> map1 = new HashMap<>();
map1.put("name", "john");
HashMap<String, String> map2 = new HashMap<>();
map2.put("age", "30");
employee.add(map1);
employee.add(map2);
return employee;
}
public String getEmployeeName(){
return employeeName;
}
@Override
public XdmValue call(XdmValue[] arg0) throws SaxonApiException {
return new XdmAtomicValue(getEmployeeName());
}
@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[] {};
}
@Override
public QName getName() {
return new QName("test.extension.Employee", "getEmployeeName");
}
@Override
public SequenceType getResultType() {
return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
}
目前,我能够调用从 xslt 返回字符串的 getEmployeeName 方法。但是我如何修改 Employee 类以也支持返回 HasMap 的 ArrayList 即方法 getEmployee() 因为新的 XdmAtomicValue() 方法不将 ArrayList 作为构造函数参数。
回首忆惘然
相关分类