我在使用 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>
如您所见,我正在几个 xml 上实现此样式表
所以根输出 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
原始造型器:
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
}