Why is only the root-element transformed?

B

Ben Engbers

Hi,

This is the first time I use SAX in order to translate my XML and I
encountered the following problem.

I use the following Java-code to produce xml that should be transformed
(Based on the xml that is created by the 'createXml()' several
underlying xml-files are merged together. The resulting xml seems to be
well-formed):
When I use the xsl-file, only the root element is transformed, resulting
in the output at the end of this message (blank lines are deleted).

Why are all the other elements ignored (If I write the xml and xsl to my
file-system and use Internet Exlorer to view the xml, the xml -s
transtlated as intended)?

Ben


public class XMLtoPDFtrans {
private Reader inputXml = null;
private String inputXsl;
private String blockDefLocation;

private SAXTransformerFactory saxTransFact;
private TransformerHandler transHand;

static Document document;
private TransformerFactory tf;

public XMLtoPDFtrans(String formName,
String[] formBlocks,
String formBlockDefLocation) {
if (formName == null || formBlocks ==null) {
System.out.println("Onvoldoende input");
System.exit(1);
}
blockDefLocation = formBlockDefLocation;
inputXml = createXml(formName, formBlocks);

tf = TransformerFactory.newInstance();
try {
// inputXsl = new
File("WEB-INF\\src\\minlnv\\pdf\\XML_to_PDF.xsl").toURL().toExternalForm();
if (tf.getFeature(SAXTransformerFactory.FEATURE)) {
saxTransFact = (SAXTransformerFactory) tf;
transHand = null;
if (inputXsl == null) {
transHand = saxTransFact.newTransformerHandler();
} else {
transHand = saxTransFact.newTransformerHandler(
new StreamSource(inputXsl));
}
}
transHand.setResult( new StreamResult(System.out));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
// } catch (MalformedURLException e) {
e.printStackTrace();
}
}

public StreamResult getStream() {
StreamResult result = null;
try {
InputSource xmlInputFile = new InputSource( inputXml);
SAXFormLezer saxFormLezer = new SAXFormLezer( blockDefLocation);

saxFormLezer.setContentHandler(transHand);
saxFormLezer.parse(xmlInputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return result;
}

private Reader createXml(String form, String[] blocks) {
CharArrayWriter caw = new CharArrayWriter();
String opbouw = form;

AttributesImpl attribImpl = new AttributesImpl();
attribImpl.addAttribute("","bottomMargin","bottomMargin","float","50.0");
attribImpl.addAttribute("","leftMargin","leftMargin","float","20.0");
attribImpl.addAttribute("","pageHeight","pageHeight","float","650.0");
attribImpl.addAttribute("","pageWidth","pageWidth","float","300.0");
try {
DataWriter w = new DataWriter(caw);
w.startDocument();
w.startElement("","formulier","formulier",attribImpl);
for (int tel = 0; tel <blocks.length; tel++) {
attribImpl = new AttributesImpl();
attribImpl.addAttribute("","naam","naam","String",blocks[tel]);
opbouw += blocks[ tel];
w.dataElement("","formBlock","formBlock", attribImpl,"");
}
w.dataElement("formClassName", opbouw);
w.endElement("","formulier","formulier");
w.endDocument();
} catch (SAXException e) {
e.printStackTrace();
}
return new CharArrayReader(caw.toCharArray());
}

Produced xml:
_____________

<?xml version="1.0" encoding="UTF-8"?><interim>
<formBlock blockHeight="650.0" blockName="A01">
<pageSeparator>true</pageSeparator>
<dataItemString posY="640.0" posX="202.0" substringBegin="2"
substringEnd="4">
<dataItem>
<dataId>1</dataId>
</dataItem>
</dataItemString>
<dataItemString posY="640.0" posX="257.0" substringBegin="2"
substringEnd="4">
<dataItem>
<dataId>71</dataId>
</dataItem>
</dataItemString>
<dataItemString posY="640.0" posX="312.0">
<dataItem>
<dataId>81</dataId>
</dataItem>
</dataItemString>

</formBlock>
<formClassName>eGDIA01</formClassName>
</interim>

xsl
____

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="text" />
<xsl:template match="formClassName" >
formClassName
<xsl:apply-templates />
</xsl:template>
<xsl:template match="/">
DataItem
<xsl:apply-templates />
</xsl:template>
<xsl:template match="//pageSeparator" >
PageSep
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>

Output:
_________

DataItem
true
1
71
81
eGDIA01
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top