有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java xslt解析目标xml中的模式

我使用xslt将一种xml转换为另一种。 关键是,在目标xml中,我需要解析shema。 指向此架构的链接应位于何处?请为我提供正确的方法。 使用的API是否足够:

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;
     }

进来。xml文件:

<?xml version="1.0" encoding="UTF-8"?>
     <enfinity
 xmlns:dt="http://www.intershop.com/xml/ns/enfinity/6.1/core/impex-dt">..

规则。xsl:

 <custom-attribute name="isPerformance" dt:dt="int">1</custom-attribute>

我得到: 组织。xml。萨克斯。SAXParseException:与元素类型“custom attribute”关联的属性“dt:dt”的前缀“dt”未绑定。 线程“main”java中出现异常。lang.NullPointerException


共 (1) 个答案

  1. # 1 楼答案

    第一步是解决SAXParseException。这是因为你的规则。xsl文件的名称空间格式不正确。除非在样式表中声明,否则不能在样式表中使用名称空间前缀dt。所以加上xmlns:dt=”http://www.intershop.com/xml/ns/enfinity/6.1/core/impex-dt“到样式表

    如果文件已按规则显示。xsl是整个文件,然后它会失败,因为这个文件不是XSLT样式表

    我真的不能帮你回答第一个问题,关于对模式的引用。这将有助于展示你想要产生什么样的产出。但你似乎有一些更基本的问题要先解决