I want to create xml file using xsl file that will use in memory data only (no use of a source file). I have the code below but I'm not sure what to use instand of the source file
part of the xsl for example :
<ExternalReference>
<xsl:value-of select="erstefnImport:retriveXslExternalReference()" />
</ExternalReference>
the code part :
if (xsltFile != null) {
Transformer transformer = getTransformer(xsltFile);
Source source = null;
try {
source = new StreamSource(new FileInputStream(null));// ??
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
StreamResult outputTarget = new StreamResult(byteOutputStream);
if (transformer != null) {
try {
transformer.transform(source, outputTarget);
rst = new String(byteOutputStream.toByteArray());
} catch (Exception e) {
Log.error(this, "Error while transforming xml.", e);
}
}
}
I tried to use Document as a source but the output is only :
<?xml version="1.0" encoding="iso-8859-1"?>
the code :
if (xsltFile != null) {
Transformer transformer = getTransformer(xsltFile);
Source source = null;
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
Document docResult = null;
try {
docResult = docFactory.newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e2) {
e2.printStackTrace();
}
DOMSource domSource = new DOMSource(docResult);
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
StreamResult outputTarget = new StreamResult(byteOutputStream);
if (transformer != null) {
try {
transformer.transform(domSource, outputTarget);
rst = new String(byteOutputStream.toByteArray());
} catch (Exception e) {
Log.error(this, "Error while transforming xml.", e);
}
}
}
CodePudding user response:
XSLT 1.0 and that way the APIs of XSLT 1.0 processors are pretty much built around the idea of processing a source tree as the input to an XSLT stylesheet.
This is different in XSLT 2 and 3, in XSLT 2 you can start processing with a named template, in XSLT 3 as well, there you can also start by calling a public function.
As you are using Java, you could easily consider switching to Saxon 10, available in the open-source HE edition from Sourceforge and Maven, to use XSLT 3 and then make sure you use its own API https://www.saxonica.com/html/documentation10/using-xsl/embedding/s9api-transformation.html which has methods like callTemplate
(https://www.saxonica.com/html/documentation10/using-xsl/embedding/s9api-transformation.html) or callFunction
(https://www.saxonica.com/html/documentation10/javadoc/net/sf/saxon/s9api/Xslt30Transformer.html#callFunction-net.sf.saxon.s9api.QName-net.sf.saxon.s9api.XdmValue:A-).
In XSLT 3 there is a predefined name for the initial template that would be
<xsl:template name="xsl:initial-template">
<root>
<foo>bar</foo>
</root>
</xsl:template>
in XSLT there is no predefined named so you can chose one like main
e.g.
<xsl:template name="main">
<root>
<foo>bar</foo>
</root>
</xsl:template>
and then use the referenced callTemplate
method of the API to use the main
name (new QName("", "main")
, I think).
CodePudding user response:
I fount that using (almost)empty xml source file will work . Just need to use a root tag (map in this example) in that source xml , and use this tag name in the xsl tempele :
<xsl:template match="map">