2014-07-15 102 views
0

我使用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

回答

0

第一步是解決SAXParseException。這是因爲您的rules.xsl文件不是命名空間格式良好的。除非在樣式表中聲明,否則不能在樣式表中使用名稱空間前綴dt。因此,將xmlns:dt =「http://www.intershop.com/xml/ns/enfinity/6.1/core/impex-dt」添加到樣式表中。

如果您顯示爲rules.xsl的文件是整個文件,那麼它將失敗,因爲此文件不是XSLT樣式表。

我不能真正幫你解決第一個問題,關於對模式的引用。這將有助於顯示你想要產生的輸出。但是,你似乎首先要解決一些更基本的問題。