[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 656
  • Last Modified:

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="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd" omit-xml-declaration="yes" />

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


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

<?xml version="1.1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml"/>

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


<!-- 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" "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head xmlns="">
</head>
<body>
</body>
</html>

Open in new window

0
EnvisageInformationSystems
Asked:
EnvisageInformationSystems
1 Solution
 
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="http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd" 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="http://www.w3.org/1999/xhtml"
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="http://www.w3.org/1999/xhtml">

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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now