With a browser, I want to transform XML which may contain some HTML, with an XSL stylesheet.
In this article, user Mads Hansen wrote:
If your HTML is well-formed, then just embed the HTML tags without escaping or wrapping
in CDTATA. If at all possible, it helps to keep your content in XML. It gives you more
flexibility for transforming and manipulating the document.
You could set a namespace for the HTML, so that you could disambiguate your
HTML tags from the other XML wrapping it.
I like the proposed solution, but can’t make it work. I used h as the namespace for html:
temp.xml
<?xml version='1.0' encoding='UTF-8' ?>
<?xml-stylesheet type='text/xsl' href='temp.xsl'?>
<root xmlns:h="http://www.w3.org/1999/xhtml">
<MYTAG title="thisnthat">
text before ol
<h:ol>
<h:li>item</h:li>
<h:li>item</h:li>
</h:ol>
text after ol
</MYTAG>
</root>
temp.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:h="http://www.w3.org/1999/xhtml">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="MYTAG">
<h3>
<xsl:value-of select="@title" />
</h3>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
The output (from Firefox 18) is:
thisnthat
text before ol item item text after ol
Since you are generating the final HTML and are in control, I’m not sure why you want to use namespaces here. You’d need to do that only if there was a conflict between your custom tags and standard HTML — i.e. if you had a custom
<a...>tag that had different semantics from HTML. I got your transform to work bya) Removing all HTML namespacing
b) Adding an identity transform
test.xml
test.xsl