By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,722 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,722 IT Pros & Developers. It's quick & easy.

Why is only the root-element transformed?

P: n/a
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().to ExternalForm();
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","bottomM argin","float","50.0");
attribImpl.addAttribute("","leftMargin","leftMargi n","float","20.0");
attribImpl.addAttribute("","pageHeight","pageHeigh t","float","650.0");
attribImpl.addAttribute("","pageWidth","pageWidth" ,"float","300.0");
try {
DataWriter w = new DataWriter(caw);
w.startDocument();
w.startElement("","formulier","formulier",attribIm pl);
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:output 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
Oct 5 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.