A place for my resume to sit in an anal retentive tank of bliss. And maybe some hackery.
I haven't yet had time to make any real entries for this page.
The Problem:
When processing the xslt, the resulting xml document contains what may seem to be random xmlns="" attributes to some elements. The reason this happens is because child-nodes that are visited wipe out the namespace thus forcing them to redeclar xmlns="". We'll look at two ways to create the problem.
An Example:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/body">
<xsl:document href="ex1_output.html" method="xml" indent="yes">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Page</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:document>
</xsl:template>
<xsl:template match="para">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="section">
<div>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="img">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Example 1 XSLT
<?xml version="1.0"?>
<body>
<section>
<para>Random para one</para>
<para>Random para two</para>
</section>
<img src="foo.gif"/>
</body>
Example 1 XML
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Page</title>
</head>
<body>
<div xmlns="">
<p>Random para one</p>
<p>Random para two</p>
</div>
<img xmlns="" src="foo.gif"/>
</body>
</html>
Example 1 Output
In both cases the child-node changes the current namespace to the empty string. The <div> tag creates the problem because the template doesn't contain the xmlns="http://www.w3.org/1999/xhtml", so the xslt engine gives it an empty namespace, thus pushing out the problem. The solution to this problem is to include the namespace declaration somewhere in the parent XML tree, probably in the <xsl:stylesheet> tag. The <img> tag creates a problem because it uses the <xsl:copy-of> tag, which forces the element's namespace attribute to be coppied. The best solution I've found for this is not to use the xsl copy-of and instead use <xsl:element> rebuild the elements desired. Corrected examples follow.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/body">
<xsl:document href="ex2_output.html" method="xml" indent="yes">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Page</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:document>
</xsl:template>
<xsl:template match="para">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="section">
<div>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="img">
<xsl:element name="{local-name(.)}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Example 2 XSLT
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Page</title>
</head>
<body>
<div>
<p>Random para one</p>
<p>Random para two</p>
</div>
<img src="foo.gif"/>
</body>
</html>
Example 2 Output
So as can be seen, these two changes correct this common problem for xslt.
Comming Soon(tm) - math: and func: of exslt problems.
Comming Soon(tm) - A lesson in the insane.
coffee | nose > keyboard or C|N>K for short.
-- Unknown