|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Processing XML with XSL
Hi Srivalli,
Thank you for your reply. However that seems not to be the root of my problems. The XML file I am using is pretty simple. <?xml version="1.0"?> <ARTICLE> <TITLE>A Sample Article</TITLE> <SECT>The First Major Section <PARA>This section will introduce a subsection.</PARA> <SECT>The Subsection Heading <PARA>This is the text of the subsection. </PARA> </SECT> </SECT> </ARTICLE> and the XSL file I am using to transform this into simple HTML is <?xml version="1.0" encoding="ISO-8859-1" ?> - <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl utput method="html" /> - <xsl:template match="/"> - <html> - <body> <xsl:apply-templates /> </body> </html> </xsl:template> - <xsl:template match="/ARTICLE/TITLE"> + <h1 align="center"> <xsl:apply-templates /> </h1> </xsl:template> - <!-- Top Level Heading --> - <xsl:template match="/ARTICLE/SECT"> - <h2> <xsl:apply-templates select="text()|B|I|U|DEF|LINK" /> </h2> <xsl:apply-templates select="SECT|PARA|LIST|NOTE" /> </xsl:template> - <!-- Second-Level Heading --> - <xsl:template match="/ARTICLE/SECT/SECT"> - <h3> <xsl:apply-templates select="text()|B|I|U|DEF|LINK" /> </h3> <xsl:apply-templates select="SECT|PARA|LIST|NOTE" /> </xsl:template> - <!-- Third-Level Heading --> - <xsl:template match="/ARTICLE/SECT/SECT/SECT"> <xsl:message terminate="yes">Error: Sections can only be nested 2 deep.</xsl:message> </xsl:template> - <!-- Paragraph --> - <xsl:template match="PARA"> - <p> <xsl:apply-templates /> </p> </xsl:template> </xsl:stylesheet> There is a java class which takes the XML and XSL stylesheet and outputs the HTML ( In theory - in practice I get the error). The Java code for completeness is 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 } To be honest I am following a tutorial from the Java Webservices Development Pack by SUN - even then its not working. Any ideas? I cnt seem to find any leads as to what the problem may actually be. Kind Regards. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Processing XML with XSL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|