[jdom-interest] builders not preserving namespace attributes in xsl files
Ed Morehouse
EMorehouse at fool.com
Wed Sep 6 12:09:50 PDT 2000
I came accross this problem when i tried to build an org.jdom.Document out of an
xsl file. Both DOMBuilder and SAXBuilder seem to be ignoring all namespace
attributes except for the one for xsl itself. Here is an example:
[ed at evilgenius xml]$ java -classpath
.:/usr/local/java/jdom/build/jdom.jar:/usr/local/java/xerces/xerces.jar BugDemo
foo-html.xsl
----------------------------------------
here's an XML file:
----------------------------------------
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:JavaXML="http://www.oreilly.com/catalog/javaxml/"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="http://www.foo.com/bar"
version="1.0">
<xsl:template match="JavaXML:Book">
<html>
<head>
<title><xsl:value-of select="JavaXML:Title" /></title>
</head>
<body>
<xsl:apply-templates select="*[not(self::JavaXML:Title)]" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
----------------------------------------
here's the same file after the SAXBuilder has its way with it:
----------------------------------------
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="JavaXML:Book">
<html>
<head>
<title><xsl:value-of select="JavaXML:Title" /></title>
</head>
<body>
<xsl:apply-templates select="*[not(self::JavaXML:Title)]" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
----------------------------------------
here's the same file after the DOMBuilder has its way with it:
----------------------------------------
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE xsl:stylesheet><xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="JavaXML:Book">
<html>
<head>
<title><xsl:value-of select="JavaXML:Title" /></title>
</head>
<body>
<xsl:apply-templates select="*[not(self::JavaXML:Title)]" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
----------------------------------------
The point to note is that there are three namespace declarations in the
xsl:stylesheet element in the input file but only one after the same file has
been run through either SAXBuilder or DOMBuilder. This is clearly bad. I've
been looking through the jdom source tree and suspect that the problem may be
with Xerces (by the way, in case you weren't reading carefully, i am using the
Apache Xerces parser in this example).
Could someone who understands this more than i please comment on this issue. If
this is a bug wit Xerces i'll take it up with them, but some informed discussion
would be nice at this point.
The input file looks exactly like the first output segment. Here is the source
of the program that generated the above output:
import org.jdom.input.SAXBuilder;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
public class BugDemo
extends Object
{
public static void main(String[] args)
throws Exception
{
if (args.length == 1)
{
File file = new File(args[0]);
new BugDemo().demoBug(file);
}
else
{
System.out.println("useage: java BugDemo <XMLFileName>");
}
}
public void demoBug(File file)
throws Exception
{
System.out.println("\n----------------------------------------\n");
System.out.println("here's an XML file:");
System.out.println("\n----------------------------------------\n");
copyStream(new FileInputStream(file), System.out);
System.out.println("\n----------------------------------------\n");
System.out.println("here's the same file after the SAXBuilder has its way with
it:");
System.out.println("\n----------------------------------------\n");
new XMLOutputter().output(new SAXBuilder().build(file), System.out);
System.out.println("\n----------------------------------------\n");
System.out.println("here's the same file after the DOMBuilder has its way with
it:");
System.out.println("\n----------------------------------------\n");
new XMLOutputter().output(new DOMBuilder().build(file), System.out);
System.out.println("\n----------------------------------------\n");
}
private void copyStream(InputStream in, OutputStream out)
throws Exception
{
byte[] buffer = new byte[256];
while (true)
{
int bytes = in.read(buffer);
if (bytes == -1) break;
out.write(buffer, 0, bytes);
}
}
}
------------
- The happiest of people aren't the ones
who always have the best of everything;
they are the ones who always make the best
of everything they have.
Ed Morehouse
Software Engineer/Evil Genius
The Motley Fool
More information about the jdom-interest
mailing list