Reputation: 3046
I am trying to transform xml to xhtml using xslt 2.0.
I am creating separate documents for some elements, like this:
<xsl:result-document method="xhtml" href="output/{generate-id()}.html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" encoding="utf-8" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
<xsl:value-of select="$title"/>
</title>
<link rel="stylesheet" type="text/css" href="styles.css"/>
</head>
<body>
<div id="{generate-id()}">
<xsl:apply-templates/>
</div>
</body>
</html>
</xsl:result-document>
I am having trouble with the namespaces, though. Some of the created documents look like this:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title/>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div id="d1e1203">
<img xmlns="" src="file:/P:/199/199201.tif" alt=""/>
<h1 xmlns="">gfdg</h1>
<h1 xmlns="">gfdg</h1>
<img xmlns="" src="file:/P:/124/124566.tif" alt="gdf"/>
<!-- and so on -->
Why is the xmlns=""
added to some descendants of html?
And if I put it like this:
<xsl:result-document method="xhtml" href="output/{generate-id()}.html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" encoding="utf-8" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
then no empty namespace attributes are added. However, when I transform one of those documents, I get the following error:
Warning: SXXP0005: The source document is in namespace http://www.w3.org/1999/xhtml, but all the
template rules match elements in no namespace (Use --suppressXsltNamespaceCheck:on to
avoid this warning)
How do I handle the namespaces correctly?
Upvotes: 0
Views: 868
Reputation: 163458
If Saxon outputs <img xmlns=""/>
, that's because your result tree contains an img
element in no namespace, and some ancestor element is in a namespace such as the XHTML namespace. To avoid the unwanted namespace undeclaration, you need to ensure that the img
element in the result tree is created in the correct namespace. You haven't shown the code that generated the img
element, but the most common mistake is to use a literal result element in the stylesheet in the form <img/>
with no in-scope declaration of a default namespace. The best way of fixing this is often to declare the XHTML namespace on the xsl:stylesheet element, rather than on the instruction that outputs the html
element.
Upvotes: 1
Reputation: 167696
If you use different templates that are supposed to create XHTML result elements then you need to move xmlns="http://www.w3.org/1999/xhtml"
up to the xsl:stylesheet
(respectively xsl:transform
) root element of your stylesheet e.g. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0">
.
As for writing stylesheets that process (read in) XHTML documents with elements in the XHTML namespace, with XSLT 2.0 the easiest is using xpath-default-namespace="http://www.w3.org/1999/xhtml"
on the xsl:stylesheet
element, as that way you can write match patterns like <xsl:template match="p">...</xsl:template>
and XPath expressions like //table
matching respectively selecting elements in the XHTML namespace, without the XPath/XSLT 1.0 need to bind a prefix to the namespace and use the prefix in all patterns and expressions.
Upvotes: 3