You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Mikael St�ldal <mi...@home.se> on 2001/04/15 12:25:07 UTC
Problem with SAX2 and namespaces
I reported this problem against Xalan 2.0D05 on 1 january.
The problem still remains with Xalan 2.0.1 and Xerces 1.3.1.
I can't find this bug in the bug database.
I'm using programatically generated SAX input to Xalan. Xalan doesn't
seem to work properly when the qName paramterer to the ContentHandler
methods is the empty string. I think that Xalan should work properly
even when the qName is not given.
Here is a test program and a stylesheet:
// DirTest.java
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;
import javax.xml.transform.*;
import javax.xml.transform.Source;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.StreamSource;
import org.apache.xml.serialize.*;
public class DirTest
{
public static void main(String[] args) throws Exception
{
if (args.length < 3)
{
System.out.println("Syntax: DirTest <input_dir> <stylesheet> <output>");
return;
}
String inDir = args[0];
String xslFile = args[1];
String outFile = args[2];
TransformerFactory tf = TransformerFactory.newInstance();
if (!(tf.getFeature(SAXTransformerFactory.FEATURE)
&& tf.getFeature(SAXResult.FEATURE)
&& tf.getFeature(StreamSource.FEATURE)))
{
System.out.println("The transformer factory "
+ tf.getClass().getName() + " doesn't support SAX");
return;
}
SAXTransformerFactory tfactory = (SAXTransformerFactory)tf;
System.out.println("Read stylesheet " + xslFile);
Templates stylesheet = tfactory.newTemplates(new StreamSource(xslFile));
System.out.println("Transforming " + inDir + " to " + outFile);
XMLSerializer ser = new XMLSerializer(
new FileOutputStream(outFile), new OutputFormat());
TransformerHandler th = tfactory.newTransformerHandler(stylesheet);
th.setResult(new SAXResult(ser.asContentHandler()));
generateDirListing(new File(inDir), th);
System.out.println("Finished");
}
static void generateDirListing(File dir, ContentHandler sax)
throws SAXException
{
String[] files = dir.list();
sax.startDocument();
sax.startElement("", "dirlist", "", new AttributesImpl());
// sax.startElement("", "dirlist", "dirlist", new AttributesImpl());
for (int i = 0; i < files.length; i++)
{
File file = new File(dir, files[i]);
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "filename", "", "CDATA", files[i]);
// atts.addAttribute("", "filename", "filename", "CDATA", files[i]);
if (file.isFile())
{
sax.startElement("", "file", "", atts);
// sax.startElement("", "file", "file", atts);
sax.endElement("", "file", "");
// sax.endElement("", "file", "file");
}
else if (file.isDirectory())
{
sax.startElement("", "directory", "", atts);
// sax.startElement("", "directory", "directory", atts);
sax.endElement("", "directory", "");
// sax.endElement("", "directory", "directory");
}
else
;
}
sax.endElement("", "dirlist", "");
// sax.endElement("", "dirlist", "dirlist");
sax.endDocument();
}
}
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<head>
<title>Directory listing</title>
</head>
<body>
<h1>Directory listing</h1>
<ul>
<xsl:apply-templates select="dirlist"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="file">
<li><xsl:value-of select="@filename"/></li>
</xsl:template>
</xsl:stylesheet>
--
/****************************************************************\
* You have just read a message from Mikael Ståldal. *
* *
* Remove "-ingen-reklam" from the address before mail replying. *
\****************************************************************/