Go Premium for a chance to win a PS4. Enter to Win

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

XSL adding xmlns="" automatically

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
x2cmsac
Asked:
x2cmsac
  • 4
  • 4
2 Solutions
 
hieloCommented:
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
 
x2cmsacAuthor Commented:
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
 
hieloCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
x2cmsacAuthor Commented:
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
 
hieloCommented:
> 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
 
x2cmsacAuthor Commented:
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
 
x2cmsacAuthor Commented:
And all i changed was a typo in the title and closing xsl:template element.
0
 
hieloCommented:
Weird. I have used that setup on MSXML parser and also on whatever it is that comes in LINUX/PHP platforms.
0
 
Geert BormansCommented:
> 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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