Transforming XML into XHTML in Java

Hi all,

I'm working with a 10-12 year old framework for web applications.  Our framework uses a Java Servlet to produce XML which the servlet then transforms to HTML via XSLT.  So each screen of the website has its own XSL file and uses some common XSL files for things like the header and footer.  The system works well.  The problem is that our newest client requires the output to be XHTML not HTML.  So I have to convert our XSL files over to generate valid XHTML.  This is not going smoothly.  

I believe the issue is due to the XSL files calling templates that are located in a separate file.  It seems when this happens, I get xmlns="" in the output.  Please see the attached code for examples.  Ideally, i would like to move the <html> tag from the individual page's XSL file into the Layout XSL file but when I do that, the page usually doesn't show in the browser at all.  To make matters worse our XSL files often call a template from another XSL file which calls a template from the original XSL file.  In all this, I can't seem to keep the namespaces from getting screwed up in the output.  

Any ideas?  Thanks for the help in advance!!!

Scott Keller

<!-- This is example code from one of our XSL files -->

<?xml version="1.1"?>

<xsl:stylesheet xmlns:xsl="" version="1.0">
    <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="" omit-xml-declaration="yes" />

    <xsl:template match="/PAGE">
        <html xmlns="" xml:lang="en">
			<xsl:call-template name="PageLayout" />
	<xsl:template name="PageBody">
		<!-- page body HTML goes here -->
	<xsl:include href="/usr/local/commonremittance/ParticipantLayout.xsl" />

<!-- This is example code from the ParticipantLayout.xsl -->

<?xml version="1.1"?>
<xsl:stylesheet xmlns:xsl="" version="1.0">
    <xsl:output method="xml"/>

    <xsl:template name="PageLayout">
			<!-- head code is here -->
			<form action="" method="past" name="myform">
				<!-- basic header HTML is here -->
				<xsl:call-template name="PageBody" />
				<!-- basic footer HTML is here -->

<!-- Output seems to always have xmlns="" in it, right now it only shows in the <head> tag. -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" xml:lang="en">
<head xmlns="">

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
Generating xhtml there some issues.
First there is the serialisation:
for various reasons this is the best you can do
    <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="" omit-xml-declaration="yes" />
so, you have that one right allready (though I usually tend to go for strict instead of transitional)

I would not set xml version to 1.1 but to 1.0 for the stylesheet, 1.1 is likely ot needed for you and it is opening a can of worms (not all parsers support 1.1)

OK, the real problem now.
you have this
<html xmlns=""
that way you are setting the html tag explicitely in the xhtml namespace (and that is good)
but as a consequence, you will need to set all the other elements, explicitely to this namespace too.
If not, they will end up in the null namespace, and for switching back to the null namespace, you will create a xmlns="" (or the serialiser will do that for you)
That is correct behaviour

In order to get rid of that, you will need to explicitely state the namespace for every element in the stylesheets

Everywhere you put a literal element to the output tree
like this <body>
you will have to use <xsl:element, with a namespace added
like this
<xsl:element name="body" namespace="">

And you need to make that change on every element, in every stylesheet
It is a bit tedious, but basically, the stylesheets create elements in the null namespace,
and now you want them in the xhtml namespace...
that means changing all of it

If for one reason or another, you don't want to do that, you can have a two step.
- First step leaves the stylesheets exactly as they were
- second step creates xhtml from the html. Then you only need one template for setting all the namespaces right

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.