Solved

XSL adding xmlns="" automatically

Posted on 2007-11-20
9
2,561 Views
Last Modified: 2013-11-18
I am a relative notice to XSL - I am using XSL to produce XHTML pages.

I have a problem where the XSL is adding xmlns="" to HTML nodes in my XSL.

I have two files I will show you the head of each because i think this is effecting whats happening

page.xsl -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xhtml">
  <xsl:output encoding="UTF-8" method="html" indent="yes" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>
<xsl:include href="common.xsl"/>

And page.xsl uses a file called common.xsl -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="no" method="xml" omit-xml-declaration="yes"/>

All the HTML in page.xsl seems fine but items in common.xsl e.g. -
<xsl:template name="toc">
<div id="toc"></div>
</xsl:template>

Are produced in HTML as -
<div id="toc" xmlns=""></div>

I want to stop the XSL adding xmlns="" to these elements, how do i do this?

Im using ASP.NET so its Microsoft XSL processor - not sure if this makes a difference.
0
Comment
Question by:x2cmsac
  • 4
  • 4
9 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 250 total points
ID: 20324448
Your default xmlns should be that of the w3c. Try this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="" >

      <xsl:output
method="html"
version="4.0"
encoding="iso-8859-1"
indent='yes'
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd" />
0
 

Author Comment

by:x2cmsac
ID: 20324553
Thats fixed the xmlns issue but now its stripping all the closing tags, its not proper XHTML ?

e.g.
<link rel="stylesheet" href="/css/style.css" type="text/css"/>
is being transformed to  -
<link rel="stylesheet" href="/css/style.css" type="text/css">

Its removing all the closing elements?

How do i make it conform to proper XHTML?
0
 
LVL 82

Expert Comment

by:hielo
ID: 20324587
I've always used it to create XHTML with strict doctype and have never seen that problem before. My stand-alone tags remamin properly closed. There must be something else going on. Post your XSL.
0
 

Author Comment

by:x2cmsac
ID: 20324637
Thanks for the help.

Im assuming this has todo with the XSLT processor. I changed method="html" to method="xml" and it now closes the stand alone tags properly.

I might need to raise this as a seperate ticket but now i have another problem...

  <xsl:template name="metakeyword">
    <xsl:param name="pagekeywords"/>
<xsl:element name="meta">
  <xsl:attribute name="name">keywords</xsl:attribute>
     <xsl:attribute name="content">
                   <xsl:value-of select="$pagekeywords"/>
              </xsl:attribute>      
    </xsl:element>
  </xsl:template>

is producing as -

<meta name="keywords" content="............."></meta>

Which should just be -

<meta name="keywords" content="............."/>

Is there anyway to change this in XSLT  / workaround ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Expert Comment

by:hielo
ID: 20324668
> Is there anyway to change this in XSLT  / workaround ?
Not that I am aware off. The reason being that your output is XML and that is perfectly valid XML syntax. It is the result of using "XML" vs. "HTML".

This is how I set up my xsl, and am still able to get the <link ... />:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" xmlns:announcements="http://www.adagreatlakes.org" xmlns:dc="http://purl.org/dc/elements/1.1/" exclude-result-prefixes="announcements dc" >

      <xsl:output method="html" version="4.0" encoding="iso-8859-1" indent='yes' doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd" />

      <!-- "main" -->
      <xsl:template match="/" >

<html xml:lang="en-us" lang="en-us">
      <head>
<title>Test></title>
<link href="" type="text/css" />
</head>
<body>
<h1>Hi</h1>
</body>
</html>
</xsl:/template>
0
 

Author Comment

by:x2cmsac
ID: 20324712
That code for me produces -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd">
<html xml:lang="en-us" lang="en-us" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-16">
    <title>Test</title>
    <link href="" type="text/css">
  </head>
  <body>
    <h1>Test</h1>
  </body>
</html>

With the link tag not closed properly, ive no idea whats causing it.
0
 

Author Comment

by:x2cmsac
ID: 20324731
And all i changed was a typo in the title and closing xsl:template element.
0
 
LVL 82

Expert Comment

by:hielo
ID: 20325228
Weird. I have used that setup on MSXML parser and also on whatever it is that comes in LINUX/PHP platforms.
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 250 total points
ID: 20326168
> I've always used it to create XHTML with strict doctype and have never seen that problem before

that is strange, you must not have been paying attention.

If the output mehod is set to html, the xslt processor must serialise as html
and html is NOT wellformed because it doesn't allow (in the strict sense)
end tags for img, meta and link (and likely a couple of others)
since a meta tag is added automatically during serialisation, the one with the encoding,
there is little chance that the resulting HTML will be wellformed XHTML
If you want XHTML, you need the output method to be XML

About the <foo></foo> vs. <foo/>
> <meta name="keywords" content="............."></meta>
> Which should just be -
> <meta name="keywords" content="............."/>
> Is there anyway to change this in XSLT  / workaround ?

In XML (or XHTML) terms they are equal... so it really doesn't matter
the serialiser of the processor picks how he wants to deal with this... and you can't control it
If you really want it differently, you need a Ruby (or Python, ...) postprocess to fix it with a regular expression
I strongly recommend you to leave it as it is, XML parsers don't bother and they should not
Note that in hielo's counter examples there never is an end tag for meta... since he serialises as html

And to answer your original question
the xmlns="" appears because you are constructing the div element in a namespace other than the default namespace of your document
it would help if you added this doument in the correct namespace
<xsl:element name="div" namespace="http://www.w3.org/1999/xhtml">
....
</xsl:element>
I need to see the full XML and XSLT to make sure I got this example right

cheers

Geert

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now