我使用xslt將一個xml轉換爲其他xml。 重點是,在目標xml我需要shema解決。 這個模式的鏈接應該駐留在哪裏?請爲我提供正確的方法。 是所用的API不夠:xslt - 解析目標xml中的模式
import javax.xml.transform.*;
public class Main implements URIResolver{
private File root;
public static void main(String[] args) throws TransformerException, TransformerConfigurationException,
FileNotFoundException, IOException {
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setURIResolver(new URIResolver() {
public Source resolve(String href, String base) {
if (href.endsWith("in.xml")) {
return new StreamSource(this.getClass().getResourceAsStream("in.xml"));
} else {
return null;
}
}
});
Transformer transformer = tFactory.newTransformer(new StreamSource("rules.xsl"));
transformer.transform(new StreamSource("in.xml"), new StreamResult(new FileOutputStream("out.xml")));
System.out.println("************* The result is in out.xml *************");
@Override
public Source resolve(String href, String base) throws TransformerException {
StreamSource source = new StreamSource(getInputStream(href));
// this works with saxon7/saxon6.5.2/xalan
source.setSystemId(href);
return source;
}
protected InputStream getInputStream(String path)
{
InputStream file = null;
try
{
// load from a dir
file = new FileInputStream(new File(this.root, path));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
System.out.println("File not found");
}
return file;
}
in.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<enfinity
xmlns:dt="http://www.intershop.com/xml/ns/enfinity/6.1/core/impex-dt">..
rules.xsl:
<custom-attribute name="isPerformance" dt:dt="int">1</custom-attribute>
我: org.xml.sax.SAXParseException:本與元素類型「定製屬性」相關聯的屬性「dt:dt」的前綴「dt」未被綁定。 線程「main」中的異常java.lang.NullPointerException