我在使用xslt样式表解析xml文件时遇到问题 http://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html 我在样式代码中稍微改了一下,输入了多个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>
</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>
正如您所看到的,我正在几个xmls上实现这个样式表
所以root被输出x次(x = xml输入文件)
这是输出的样子:
<request campus="UQU" year="2013" term="second">
<student key="42701646">
<name first="فؤاد" last="خوج"/><MAX>0</MAX>
<acadArea abbv="CSandISG"><major code="143100"/></acadArea>
<updateCourseRequests commit="true">
<courseOffering1 subjectArea="RELg" courseNumber="16312012" priority="19981.921" credit="2"/>
</updateCourseRequests></student>
.
.
.
.
</request>
<request campus="UQU" year="2013" term="second"/>
<request campus="UQU" year="2013" term="second"/>
<request campus="UQU" year="2013" term="second"/>
根“请求”再打印三次!:( 因为我输入了四个xml文件 这是我在命令提示符中写的命令:
java Stylizer data/file1.xsl data/file1.xml data/file2.xml data/file3.xml data/file4.xml
原始的STYLIZER:
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
}
答案 0 :(得分:0)
您可以通过将样式表文件的名称更改为outputname
结尾来创建新文件.xml
。
您可以将此文件用于名为FileOutputStream
的新outputStream
。
然后,您将此输出流用于名为StreamResult
的新result
。
最后,使用相同的结果流转换每个源XML文件。
变换器按照你的说法进行操作:它正在独立地转换每个DOM源并将每个源的结果放在同一个地方。
目前尚不清楚您的期望。您是否希望以某种方式将输出合并为单个XML文档?如果是这样,那么你必须说 你想要它们的组合。
您是否正在使用四个命令行XML文件以及作为XSLT样式表中document
调用中的另外两个XML文件?要合并为一个的六个XML文件非常多!
如果你想要四个独立的变换,那么你将不得不创建四个不同的输出文件,并将每个变换的输出发送到不同的地方。