Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的xsl样式表生成具有多个根的xml文件_Java_Xslt - Fatal编程技术网

Java 我的xsl样式表生成具有多个根的xml文件

Java 我的xsl样式表生成具有多个根的xml文件,java,xslt,Java,Xslt,我在用xslt样式表解析xml文件时遇到问题 我在Stylezer代码中做了一些修改,以输入多个xml文件 ((只是给你一个好的背景) 一切都很好,但根被切断了 这是我的xsl代码: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" exten

我在用xslt样式表解析xml文件时遇到问题 我在Stylezer代码中做了一些修改,以输入多个xml文件 ((只是给你一个好的背景)

一切都很好,但根被切断了

这是我的xsl代码:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:exsl="http://exslt.org/common"
 extension-element-prefixes="exsl" version="1.0">

 <xsl:output method="xml" omit-xml-declaration="yes"/>

 <xsl:template match="/">
 <xsl:text>&#xa;</xsl:text>
 <request campus="UQU" year="2013" term="second">
 <xsl:for-each select="/SIS_REP070/LIST_G_STUDENT_ID/G_STUDENT_ID">
 .
 .
 .
 <xsl:for-each select="document('sis_rep413b_anon.xml')/MODULE1/LIST_G_STUDENT_ID/G_STUDENT_ID[STUDENT_ID=$varID]">
 .
 .
 .
 <xsl:for-each select="document('sis_rep814.xml')/SIS_REP814/LIST_DEGREE_PLANS/DEGREE_PLANS[EDITION1=57][TOTAL_HRS=160]/LIST_G_COURSE_LEVEL/G_COURSE_LEVEL/LIST_G_COURSE_CODE/G_COURSE_CODE">
 .
 .
 .
 </xsl:for-each>
 </request>
 </xsl:template>
</xsl:stylesheet>
原始样式器:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;

// For write operation
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;


public class Stylizer {
// Global value so it can be ref'd by the tree-adapter
static Document document;

public static void main(String[] argv) {
    if (argv.length != 2) {
        System.err.println("Usage: java Stylizer stylesheet xmlfile");
        System.exit(1);
    }

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    //factory.setNamespaceAware(true);
    //factory.setValidating(true);
    try {
        File stylesheet = new File(argv[0]);
        File datafile = new File(argv[1]);

        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(datafile);

        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = tFactory.newTransformer(stylesource);

        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
    } catch (TransformerConfigurationException tce) {
        // Error generated by the parser
        System.out.println("\n** Transformer Factory error");
        System.out.println("   " + tce.getMessage());

        // Use the contained exception, if any
        Throwable x = tce;

        if (tce.getException() != null) {
            x = tce.getException();
        }

        x.printStackTrace();
    } catch (TransformerException te) {
        // Error generated by the parser
        System.out.println("\n** Transformation error");
        System.out.println("   " + te.getMessage());

        // Use the contained exception, if any
        Throwable x = te;

        if (te.getException() != null) {
            x = te.getException();
        }

        x.printStackTrace();
    } catch (SAXException sxe) {
        // Error generated by this application
        // (or a parser-initialization error)
        Exception x = sxe;

        if (sxe.getException() != null) {
            x = sxe.getException();
        }

        x.printStackTrace();
    } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();
    } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
    }
} // main
}
修改后:

 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.FactoryConfigurationError;
 import javax.xml.parsers.ParserConfigurationException;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 import org.w3c.dom.Document;
 import org.w3c.dom.DOMException;

 // For write operation
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.transform.stream.StreamResult;
 import java.io.*;


public class Stylizer {


public static void main(String[] argv) {
    if (argv.length < 2) {
        System.err.println("Usage: java Stylizer stylesheet xmlfile");
        System.exit(1);
    }


    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    //factory.setNamespaceAware(true);
    //factory.setValidating(true);
    try {
        File stylesheet = new File(argv[0]);
        File [] fileList = new File[argv.length];

        for(int i=1 ; i<argv.length ; i++)
            fileList[i] = new File(argv[i]);

        String targetExtension = ".xml";  
        int extIndex = argv[0].lastIndexOf(".");  
        String ext = argv[0].substring(extIndex);  
        argv[0] = argv[0].substring(0, extIndex) + targetExtension;  
        File outputname = new File(argv[0]);

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document [] document = new Document[argv.length];

        for(int i=1 ; i<argv.length ; i++)
            document[i] = builder.parse(fileList[i]);

        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = tFactory.newTransformer(stylesource);

        DOMSource [] source = new DOMSource [argv.length];
        for(int i=1 ; i<argv.length ; i++)
            source[i] = new DOMSource(document[i]);

        FileOutputStream outputStream = new FileOutputStream((File)outputname);

        StreamResult result = new StreamResult(outputStream);
        for(int i=1 ; i<argv.length ; i++)
            transformer.transform(source[i], result);

    } catch (TransformerConfigurationException tce) {
        // Error generated by the parser
        System.out.println("\n** Transformer Factory error");
        System.out.println("   " + tce.getMessage());

        // Use the contained exception, if any
        Throwable x = tce;

        if (tce.getException() != null) {
            x = tce.getException();
        }

        x.printStackTrace();
    } catch (TransformerException te) {
        // Error generated by the parser
        System.out.println("\n** Transformation error");
        System.out.println("   " + te.getMessage());

        // Use the contained exception, if any
        Throwable x = te;

        if (te.getException() != null) {
            x = te.getException();
        }

        x.printStackTrace();
    } catch (SAXException sxe) {
        // Error generated by this application
        // (or a parser-initialization error)
        Exception x = sxe;

        if (sxe.getException() != null) {
            x = sxe.getException();
        }

        x.printStackTrace();
    } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();
    } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
    }
} // main
}
import javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.FactoryConfigurationError;
导入javax.xml.parsers.parserConfiguration异常;
导入org.xml.sax.SAXException;
导入org.xml.sax.SAXParseException;
导入org.w3c.dom.Document;
导入org.w3c.dom.domeException;
//用于写操作
导入javax.xml.transform.Transformer;
导入javax.xml.transform.TransformerException;
导入javax.xml.transform.TransformerFactory;
导入javax.xml.transform.TransformerConfiguration异常;
导入javax.xml.transform.dom.DOMSource;
导入javax.xml.transform.stream.StreamSource;
导入javax.xml.transform.stream.StreamResult;
导入java.io.*;
公共类样式器{
公共静态void main(字符串[]argv){
如果(argv.length<2){
System.err.println(“用法:JavaStylezer样式表xmlfile”);
系统出口(1);
}
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
//factory.setNamespaceAware(true);
//工厂设置验证(true);
试一试{
文件样式表=新文件(argv[0]);
File[]fileList=新文件[argv.length];

对于(int i=1;i您可以通过将样式表文件的名称更改为以
.xml
结尾来创建一个新文件
outputname

将此文件用于名为
outputStream
的新
FileOutputStream

然后将此输出流用于名为
result
的新
StreamResult

最后,使用相同的结果流转换每个源XML文件

转换器正在按照您所说的那样进行:它独立地转换每个DOM源,并将每个DOM源的结果放在同一个位置

不清楚您期望的是什么。您是否希望以某种方式将输出合并到单个XML文档中?如果是,则必须说明您希望如何将它们合并

您是否使用了四个命令行XML文件以及XSLT样式表中
document
调用中的另外两个XML文件?将六个XML文件合并成一个文件是非常多的


如果您需要四个独立的转换,那么您必须创建四个不同的输出文件,并将每个转换的输出发送到不同的位置。

我怀疑在Stylezer类中可能会出现任何解决方法,因此请向我们具体显示您在那里修改的代码。好的,非常感谢。我现在添加源代码AL Stylezer和修改后的一个,因为我的修改分散在这里和那里。好的,谢谢你添加它。但是,你预期的结果是什么?XML输出有多个根“请求”重复四次。正如您在页面中看到的那样,第一个请求中填写了我所需的输出。我不想要第二个、第三个和第四个“请求”。请停止键入所有大写字母。如果您不想要其他三个
请求
s,则只需指定一个输入文件,而不是四个。:)我假设这不是你真正的意思,所以请更清楚地说明你想要什么。你想要一个包含所有四个转换结果的
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;

// For write operation
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;


public class Stylizer {
// Global value so it can be ref'd by the tree-adapter
static Document document;

public static void main(String[] argv) {
    if (argv.length != 2) {
        System.err.println("Usage: java Stylizer stylesheet xmlfile");
        System.exit(1);
    }

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    //factory.setNamespaceAware(true);
    //factory.setValidating(true);
    try {
        File stylesheet = new File(argv[0]);
        File datafile = new File(argv[1]);

        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(datafile);

        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = tFactory.newTransformer(stylesource);

        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
    } catch (TransformerConfigurationException tce) {
        // Error generated by the parser
        System.out.println("\n** Transformer Factory error");
        System.out.println("   " + tce.getMessage());

        // Use the contained exception, if any
        Throwable x = tce;

        if (tce.getException() != null) {
            x = tce.getException();
        }

        x.printStackTrace();
    } catch (TransformerException te) {
        // Error generated by the parser
        System.out.println("\n** Transformation error");
        System.out.println("   " + te.getMessage());

        // Use the contained exception, if any
        Throwable x = te;

        if (te.getException() != null) {
            x = te.getException();
        }

        x.printStackTrace();
    } catch (SAXException sxe) {
        // Error generated by this application
        // (or a parser-initialization error)
        Exception x = sxe;

        if (sxe.getException() != null) {
            x = sxe.getException();
        }

        x.printStackTrace();
    } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();
    } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
    }
} // main
}
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.FactoryConfigurationError;
 import javax.xml.parsers.ParserConfigurationException;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 import org.w3c.dom.Document;
 import org.w3c.dom.DOMException;

 // For write operation
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.transform.stream.StreamResult;
 import java.io.*;


public class Stylizer {


public static void main(String[] argv) {
    if (argv.length < 2) {
        System.err.println("Usage: java Stylizer stylesheet xmlfile");
        System.exit(1);
    }


    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    //factory.setNamespaceAware(true);
    //factory.setValidating(true);
    try {
        File stylesheet = new File(argv[0]);
        File [] fileList = new File[argv.length];

        for(int i=1 ; i<argv.length ; i++)
            fileList[i] = new File(argv[i]);

        String targetExtension = ".xml";  
        int extIndex = argv[0].lastIndexOf(".");  
        String ext = argv[0].substring(extIndex);  
        argv[0] = argv[0].substring(0, extIndex) + targetExtension;  
        File outputname = new File(argv[0]);

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document [] document = new Document[argv.length];

        for(int i=1 ; i<argv.length ; i++)
            document[i] = builder.parse(fileList[i]);

        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = tFactory.newTransformer(stylesource);

        DOMSource [] source = new DOMSource [argv.length];
        for(int i=1 ; i<argv.length ; i++)
            source[i] = new DOMSource(document[i]);

        FileOutputStream outputStream = new FileOutputStream((File)outputname);

        StreamResult result = new StreamResult(outputStream);
        for(int i=1 ; i<argv.length ; i++)
            transformer.transform(source[i], result);

    } catch (TransformerConfigurationException tce) {
        // Error generated by the parser
        System.out.println("\n** Transformer Factory error");
        System.out.println("   " + tce.getMessage());

        // Use the contained exception, if any
        Throwable x = tce;

        if (tce.getException() != null) {
            x = tce.getException();
        }

        x.printStackTrace();
    } catch (TransformerException te) {
        // Error generated by the parser
        System.out.println("\n** Transformation error");
        System.out.println("   " + te.getMessage());

        // Use the contained exception, if any
        Throwable x = te;

        if (te.getException() != null) {
            x = te.getException();
        }

        x.printStackTrace();
    } catch (SAXException sxe) {
        // Error generated by this application
        // (or a parser-initialization error)
        Exception x = sxe;

        if (sxe.getException() != null) {
            x = sxe.getException();
        }

        x.printStackTrace();
    } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();
    } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
    }
} // main
}