XSLT exclude-result-prefixes not preventing namespace declaration in output

  • Thread starter Cameron McCormack
  • Start date
C

Cameron McCormack

Hi everyone.

I'm having trouble working out why exclude-result-prefixes isn't
preventing a namespace declaration in my output document.

This is my input document:

<html xmlns='http://www.w3.org/1999/xhtml' xmlns:x='http://mcc.id.au/
ns/local'>
<head>
<title>blah</title>
</head>
<body>
<x:something/>
</body>
</html>

And this is the XSLT I'm using:

<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:h='http://www.w3.org/1999/xhtml'
xmlns:x='http://mcc.id.au/ns/local'
xmlns='http://www.w3.org/1999/xhtml'
exclude-result-prefixes='h x'
version='1.0'>

<xsl:eek:utput method='xml' encoding='UTF-8'
doctype-public='-//W3C//DTD XHTML 1.0 Strict//EN'
doctype-system='http://www.w3.org/TR/xhtml1/DTD/xhtml1-
strict.dtd'
media-type='application/xhtml+xml; charset=UTF-8'/>

<xsl:template match='/'>
<xsl:apply-templates select='/*'/>
</xsl:template>

<xsl:template match='h:*'>
<xsl:copy>
<xsl:copy-of select='@*'/>
<xsl:apply-templates select='node()'/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

When I run xsltproc to transform the input document, I get this
output:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:x="http://mcc.id.au/
ns/local">
<head><meta http-equiv="Content-Type" content="text/html;
charset=UTF-8" />
<title>blah</title>
</head>
<body>

</body>
</html>

Could someone explain why the xmlns:x attribute is still in there, and
how I can prevent it?

Thanks,

Cameron
 
C

Cameron McCormack

Cameron McCormack:
I'm having trouble working out why exclude-result-prefixes isn't
preventing a namespace declaration in my output document.

And please excuse Google Groups sticking in unwanted line breaks.
 
M

Martin Honnen

Cameron said:
I'm having trouble working out why exclude-result-prefixes isn't
preventing a namespace declaration in my output document.

This is my input document:

<html xmlns='http://www.w3.org/1999/xhtml' xmlns:x='http://mcc.id.au/
ns/local'>

<xsl:template match='h:*'>
<xsl:copy>

xsl:copy copies the element including any namespace nodes that are in
scope. And for your elements the default namespace is in scope but the
other namespace is in scope too. exclude-result-prefixes only helps to
prevent namespace declarations used in the stylesheet, not those copied
from the input XML.
So to get rid of the xmlns:x use
<xsl:template match="h:*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
that way the element is copied but not any additional namespace nodes
that are in scope.
 
C

Cameron McCormack

Martin Honnen:
xsl:copy copies the element including any namespace nodes that are in
scope. And for your elements the default namespace is in scope but the
other namespace is in scope too. exclude-result-prefixes only helps to
prevent namespace declarations used in the stylesheet, not those copied
from the input XML.
So to get rid of the xmlns:x use
<xsl:template match="h:*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
that way the element is copied but not any additional namespace nodes
that are in scope.

Ah, great, that did the trick. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top