public class MultiXslt
{
public static void main(String[] args) throws TransformerException,ParserConfigurationException, SAXException, IOException
{
//source xslt
StreamSource stylesource = new StreamSource("C:/Users/santhanamk/Desktop/newxslt/Xslt inputs/Idml0.xsl");
DocumentBuilderFactory docbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = docbFactory.newDocumentBuilder();
//source XML
Document sourceDoc = dBuilder.parse("C:/Users/santhanamk/Desktop/newxslt/input.xml");
DOMSource source = new DOMSource(sourceDoc);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory
.newTransformer(stylesource);
Document document = dBuilder.newDocument();
DOMResult result = new DOMResult(document);
transformer.transform(source, result);
Node resultDoc = ((Document) result.getNode()).getDocumentElement();
System.out.println(resultDoc.getChildNodes().getLength());
// print the result
StringWriter writer = new StringWriter();
Transformer transformer2 = transformerFactory.newTransformer();
transformer2.transform(new DOMSource(resultDoc), new StreamResult(writer));
String s = writer.toString();
}
}
实际上我有一个xml文件和多个xsl文件(C:/ Users / santhanamk / Desktop / newxslt / Xslt输入/ xsl列表)。当我将xml和xsl0作为输入时,我需要将输出作为字符串。因此,在得到输出后,我需要为xsl1提供与输入字符串相同的输出以获得另一个输出(字符串)。然后我需要将输出作为xsl2的输入字符串来获取另一个输出。当给定的源目录(C:/ Users / santhanamk / Desktop / newxslt / Xslt输入/ xsl列表)doest有任何新的xsl文件来加载输出字符串时,它应该给出最终输出为xml!
答案 0 :(得分:2)
我认为使用JAXP转换API,如果你想链变换,那么http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT8.html有一个例子,使用XMLFilter
创建的SAXTransformerFactory.newXMLFilter
。
以下是一个示例Java代码,它显示了如何使用样式表文件名数组并设置过滤器链:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
public class JAXPTransChain1 {
public static void main(String[] args) throws FileNotFoundException, SAXException, ParserConfigurationException, TransformerException {
String[] stylesheets = new String[] {"sheet1.xsl", "sheet2.xsl", "sheet3.xsl"};
String inputDoc = "input1.xml";
chainSheets(stylesheets, inputDoc, new StreamResult(System.out));
}
private static void chainSheets(String[] stylesheets, String inputDoc, Result result) throws FileNotFoundException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException {
InputSource input = new InputSource(new FileInputStream(inputDoc));
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader reader = spf.newSAXParser().getXMLReader();
SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance();
XMLReader parent = reader;
for (int i = 0; i < stylesheets.length; i++)
{
String sheetUri = stylesheets[i];
XMLFilter sheetFilter = stf.newXMLFilter(new StreamSource(new FileInputStream(sheetUri)));
sheetFilter.setParent(parent);
parent = sheetFilter;
}
Transformer proc = stf.newTransformer();
SAXSource transSource = new SAXSource(parent, input);
proc.transform(transSource, result);
}
}
如果输入是
之类的样本<root>
<foo>bar</foo>
</root>
和样式表就是这样。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/node()[last()]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:comment>sheet 1</xsl:comment>
</xsl:template>
</xsl:stylesheet>
和
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/node()[last()]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:comment>sheet 2</xsl:comment>
</xsl:template>
</xsl:stylesheet>
和
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/node()[last()]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:comment>sheet 3</xsl:comment>
</xsl:template>
</xsl:stylesheet>
然后最终输出是
<?xml version="1.0" encoding="UTF-8"?><root>
<foo>bar</foo>
</root><!--sheet 1--><!--sheet 2--><!--sheet 3-->
所以样式表都按输入数组的顺序应用。一旦知道了你想要的订单,就可以很容易地从目录中的文件列表中设置这样的数组。