namespace problem with XSLT

Bernd Fuhrmann

Usenet Archive
Joined
Sep 30, 2003
Messages
12
Reaction score
0
Hi!

I have some trouble with some simple stupid XSLT-stuff.

My stylesheet:
<?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" <xsl:eek:utput method="xml" cdata-section-elements="script" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" encoding="iso-8859-1"/>

<xsl:template match="html">
<p>Inserted</p>
</xsl:template>

</xsl:stylesheet>
My XML-Document:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Whatever</title></head><body></body></html>

The result of this transformation is:
<?xml version="1.0" encoding="iso-8859-1"?>
or
<?xml version="1.0" encoding="iso-8859-1"?>

Whatever

Obviously the result should be sth. like <?xml ...><p>Inserted</p> and now for the truly strange thing: Changing the stylesheet to:
<?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:xhtml="http://www.w3.org/1999/xhtml" <xsl:eek:utput method="xml" cdata-section-elements="script" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" encoding="iso-8859-1"/>

<xsl:template match="xhtml:html">
<p>Inserted</p>
</xsl:template>

</xsl:stylesheet>
results in:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE p PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <p xmlns="http://www.w3.org/1999/xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml">Inserted</p>

Why? Why does changing a namespace declaration change the behaviour of a XSLT processor (tried Saxon (7.6.5) and Xalan (1.6.0 with Xerxes 2.3.0)) in that way? Shouldn't match="sometag" mean default-xmlns:sometag?

In case the xmlns:xhtml-Namespace is necessary: Is there a way to keep the xslt-processors (saxon or xalan) from inserting unused, thus unnecessary namespace declarations that interfere with the W3C XHTML validator?

Thanks in advance
Bernd Fuhrmann
 

Bernd Fuhrmann

Usenet Archive
Joined
Sep 30, 2003
Messages
12
Reaction score
0
Dimitre said:
Read. However that wasn't obvious from reading the standard. At least I have not found any part in XSLT/XPath/... 1.0 that talks about this problem.
This is a very FAQ.

Possible. I have, so far, only read some examples and done some stuff myself. So I must write <xsl:template match="xhtml:sometag">... . But how can I then avoid to break validation of XHTML by their DTDs when Xalan and Saxon (and most likely some other XSLT processors too) insert their redundant xmlns:xhtml="..." in the result?

Thanks again in advance,
Bernd Fuhrmann
 

Bernd Fuhrmann

Usenet Archive
Joined
Sep 30, 2003
Messages
12
Reaction score
0
Dimitre said:
Can you give an example?
Sure:
Stylesheet:
<?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:xhtml="http://www.w3.org/1999/xhtml" <xsl:eek:utput method="xml" cdata-section-elements="script" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" encoding="iso-8859-1"/>

<xsl:template match="*|@*|text|processing-instruction"> <xsl:copy> <xsl:apply-templates select="*|@*|text|processing-instruction"/> </xsl:copy> </xsl:template>

<xsl:template match="xhtml:html/xhtml:head/xhtml:title"> <title>Some Title</title> </xsl:template>

</xsl:stylesheet>
XML-Document:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Whatever</title></head><body></body></html>
Result:
<?xml version="1.0" encoding="iso-8859-1"?>
<!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"> <head><title xmlns:xhtml="http://www.w3.org/1999/xhtml">Some Title</title></head><body /></html> The result actually changes a bit (but not to any solution) when I use <xhtml:title>Some Title</xhtml:title>. Both, Xalan and Saxon give me a similar result. Of course this will never validate to the XHTML-DTD.

Any idea what I could do to fix this problem?

Thanks in advance,
Bernd Fuhrmann
 

A. Bolmarcich

Usenet Archive
Joined
May 9, 2002
Messages
219
Reaction score
0
Bernd Fuhrmann wrote: said:
Possible. I have, so far, only read some examples and done some stuff myself. So I must write <xsl:template match="xhtml:sometag">... . But how can I then avoid to break validation of XHTML by their DTDs when Xalan and Saxon (and most likely some other XSLT processors too) insert their redundant xmlns:xhtml="..." in the result?

Add

exclude-result-prefixes="xhtml"

as an attribute of xsl:stylesheet.
 

Dimitre Novatchev

Usenet Archive
Joined
Nov 7, 2000
Messages
737
Reaction score
0
Using
exclude-result-prefixes="xhtml"

on the xsl:stylesheet element

I get a good result from XalanJ 2.1.4

but with Saxon 6.5.2 still the prefix is on the title element.

File this as a Saxon bug (the latest version is 6.5.3 -- probably this is fixed there?)

or
use Saxon 7, which also produces the correct result.

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 

Bernd Fuhrmann

Usenet Archive
Joined
Sep 30, 2003
Messages
12
Reaction score
0
Dimitre said:
Using
exclude-result-prefixes="xhtml"

on the xsl:stylesheet element

I get a good result from XalanJ 2.1.4

but with Saxon 6.5.2 still the prefix is on the title element.

File this as a Saxon bug (the latest version is 6.5.3 -- probably this is fixed there?)

or
use Saxon 7, which also produces the correct result.
I'm using Saxon 7 and/or Xalan. It works, thanks a lot. Thanks aswell to A. Bolmarcich!
Bernd Fuhrmann
 

Greg (21)

Usenet Archive
Joined
Dec 12, 2004
Messages
9
Reaction score
0
Sorry if I'm not posting to the right place.

I have an unwanted "xmlns" attribute showing up in my otherwise-valid XHTML result document from an XSLT transformation.

The source XML document uses XHTML elements. To avoid prefixing them, I have made "http://www.w3.org/1999/xhtml" the default namespace.

<?xml version="1.0" encoding="UTF-8"?>
<myml:thought
xmlns:myml="http://www.mywords.com"
xmlns="http://www.w3.org/1999/xhtml">

<myml:translation xml:lang="fr">
<p>
Tu es <em>belle</em>.
</p>
</myml:translation>

</myml:thought>

In XSLT style sheet, I again make "http://www.w3.org/1999/xhtml" the default namespace. Notice that I have using the XLST "copy-of" to get the XHTML elements from the XML source document into the result document.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myml="http://www.mywords.com" xmlns="http://www.w3.org/1999/xhtml" <xsl:eek:utput method="xml" version="1.0" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"

doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" indent="yes" media-type="application/xhtml+xml" />

<xsl:template match="myml:thought">
<html>
<head>
<title></title>
</head>
<body>
<xsl:apply-templates select="myml:translation"/>
</body>
</html>
</xsl:template>

<xsl:template match="myml:translation">
<div>
<xsl:attribute name="xml:lang">
<xsl:value-of select="@xml:lang"/>
</xsl:attribute>
<xsl:copy-of select="./*"/>
</div>
</xsl:template>

</xsl:stylesheet>

The result I would hope for is this, a valid XHTML 1.0 Strict web page:

<?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"> <head> <title /> </head> <body> <div xml:lang="fr"> <p> Tu es <em>belle</em>. </p> </div> </body> </html>

But the result I get is an invalid XHTML 1.0 Strict web page:

<?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:myml="http://www.mywords.com"> <head> <title /> </head> <body> <div xml:lang="fr"> <p> Tu es <em>belle</em>. </p> </div> </body> </html>

Note that the opening html tag of this result includes a "xmlns:myml" which I don't think I need. Is there any way to make sure that it doesn't get into the result document?

I'm using Ant 1.6.2 to make the transformation (so Xalan 2.6.0?).
Thanks,

Greg.
 

Joris Gillis

Usenet Archive
Joined
Sep 28, 2004
Messages
355
Reaction score
0
I have an unwanted "xmlns" attribute showing up in my otherwise-valid
XHTML result document from an XSLT transformation.
Hi,

it's actually quite simple:
just add the 'exclude-result-prefixes' attribute to the 'xsl:stylesheet' element:

<xsl:stylesheet exclude-result-prefixes="myml"/>

regards,
 

David Carlisle

Usenet Archive
Joined
Jan 5, 2000
Messages
609
Reaction score
0
copy-of copies the node with all its in scope namespaces, which you don't want here, so instead do something like

<xsl:template match="myml:translation">
<div>
<xsl:copy-of select="@xml:lang"/>
<xsl:apply-templates mode="h"/>
</div>
</xsl:template>

<xsl:template mode="h">
<xsl:element name="{name}">
<xsl:copy-of select="@"/>
<xsl:apply-templates mode="h"/>
</xsl:element>
</xsl:template>

that stops the namespace nodes being copied from the source and add

exclude-result-prefixes = "myml"

to your xsl:stylesheet to stop the myxml namespace nodes being copied from the stylesheet.

David
 

David Carlisle

Usenet Archive
Joined
Jan 5, 2000
Messages
609
Reaction score
0
Joris Gillis said:
Hi,

it's actually quite simple:
just add the 'exclude-result-prefixes' attribute to the 'xsl:stylesheet' element:

<xsl:stylesheet exclude-result-prefixes="myml"/>

regards,

that stops namespaces escaping from the stylesheet, but not from being copied from the source.

David
 

Joris Gillis

Usenet Archive
Joined
Sep 28, 2004
Messages
355
Reaction score
0
just add the 'exclude-result-prefixes' attribute to the 'xsl:stylesheet' element
that stops namespaces escaping from the stylesheet, but not from being copied from the source.
Hi,

I'm not sure I understand that; it gives the desired output with me, but I'm probably not reading very well.. I have the nasty tendancy to skip just the essential parts of one's posting:)

btw,
<xsl:element name="{name}">
<xsl:copy-of select="@"/>
<xsl:apply-templates mode="h"/>
</xsl:element>

couldn't that be written as this way?
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="h"/>
</xsl:copy>

regards,
 

David Carlisle

Usenet Archive
Joined
Jan 5, 2000
Messages
609
Reaction score
0
Joris Gillis said:
Hi,

I'm not sure I understand that; it gives the desired output with me, but I'm probably not reading very well.. I have the nasty tendancy to skip just the essential parts of one's posting:)

btw,
<xsl:element name="{name}">
<xsl:copy-of select="@"/>
<xsl:apply-templates mode="h"/>
</xsl:element>

couldn't that be written as this way?
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="h"/>
</xsl:copy>

no,xsl:copy, like xsl:copy-of copies namespace nodes as well as the element.

David
 

Greg (21)

Usenet Archive
Joined
Dec 12, 2004
Messages
9
Reaction score
0
Add

exclude-result-prefixes="xhtml"

as an attribute of xsl:stylesheet.

Right. Given that it's the "myml" prefix I want to exclude from the result document, I've added . . .

exclude-result-prefixes="myml"

.. . . to the opening tag of my style sheet. See below:

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myml="http://www.mywords.com" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="myml" The effect seems to be that the "myml" prefix does get excluded from the result document, as a prefix. (I.e. it doesn't prefix anything in the result document.) But the "http://www.mywords.com" namespace gets added to the result document nonetheless (exactly as it is declared in the source document, tied to the "myml" prefix) in any content that is copied from the source document with the XSLT "copy-of".

The result now looks like this:

<?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"> <head> <title /> </head> <body> <div xml:lang="fr"> <p xmlns:myml="http://www.mywords.com"> Tu es <em>belle</em>. </p> </div> </body> </html>

Which, with a "p" element that has a "xmlns:myml" attribute, is still not valid XHTML 1.0 Strict.
Hmmmmmm.

Any further ideas on how I can make this valid?

Thanks.
 

David Carlisle

Usenet Archive
Joined
Jan 5, 2000
Messages
609
Reaction score
0
The effect seems to be that the "myml" prefix does get excluded from the result document, as a prefix. (I.e. it doesn't prefix anything in the result document.)

No, exclude-result-prefixes has no effect on wheneter any elements are prefixed or not it stops any namespace node with namespace uRI http://www.mywords.com being copied from the stylesheet uless it is needed to generate an element or attribute name in that namespaace.

But the "http://www.mywords.com" namespace gets added to the result document nonetheless

In that case namespace nodes from that namespace must have been copied from the source tree (exclude-result-prefixes has _no_ effect on nodes copied from the source) see earlier recursove template posted which can be used to avoid copying namespace nodes.

David
 

Richard Tobin

Usenet Archive
Joined
Jan 6, 2000
Messages
4,251
Reaction score
0
But the "http://www.mywords.com" namespace gets added to the result document nonetheless (exactly as it is declared in the source document, tied to the "myml" prefix) in any content that is copied from the source document with the XSLT "copy-of".

exclude-result-prefixes just stops namespace nodes being copied from literal result elements in the stylesheet. The rationale is that you may need to bind prefixes in the stylesheet just to address the source document, when they have no relevance to the output.

But if you copy nodes from the source document that have namespace bindings in scope, it's up to you to remove any that you don't want.
Since xsl:copy copies an element's namespace nodes, to get rid of them you will have to *not* use xsl:copy for elements. You can instead use a template that matches elements, and which creates an element with the same namespace-uri and local-name. You may also want to copy the namespace nodes except the myml: one, but you can probably get away without doing this (because if they are required for element or attribute names they will get inserted anyway).

-- Richard
 

Greg (21)

Usenet Archive
Joined
Dec 12, 2004
Messages
9
Reaction score
0
I didn't see David's earlier post with the recursive template.

In my style sheet, I tried . . .

<xsl:template match="myml:translation">
<div>
<xsl:copy-of select="@xml:lang"/>
<xsl:apply-templates mode="h"/>
</div>
</xsl:template>

<xsl:template match="*" mode="h">
<xsl:element name="{name}">
<xsl:copy-of select="@"/>
<xsl:apply-templates mode="h"/>
</xsl:element>
</xsl:template>

.. . . but Ant complained about the line:

<xsl:copy-of select="@"/>

Saying:

[xslt] Unknown file:39:38: Fatal Error! A node test that matches either
NCNam:* or QName was expected.

So I replaced the "@" with "attribute::*", and no complaints - just an XHTML 1.0 Strict web page that is . . . valid! I have found peace and oneness with the world again. Thankyou.

For the benefit of others find this thread and want to include XHTML in their own namespace-using XML document and then transform that XML document into valid XHTML . . .

The source XML document:

<?xml version="1.0" encoding="UTF-8"?>
<myml:thought
xmlns:myml="http://www.mywords.com"
xmlns="http://www.w3.org/1999/xhtml"
<myml:translation xml:lang="fr">
<p>
Tu es <em title="Hot!">belle</em>.
</p>
</myml:translation>

</myml:thought>

The XSLT style sheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" xmlns:myml="http://www.mywords.com" exclude-result-prefixes="myml" <xsl:eek:utput method="xml" version="1.0" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"

doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" indent="yes" media-type="application/xhtml+xml" />

<xsl:template match="myml:thought">
<html>
<head>
<title>Thought</title>
</head>
<body>
<xsl:apply-templates select="myml:translation"/>
</body>
</html>
</xsl:template>

<xsl:template match="myml:translation">
<div>
<xsl:copy-of select="@xml:lang"/>
<xsl:apply-templates mode="h"/>
</div>
</xsl:template>

<xsl:template match="*" mode="h">
<xsl:element name="{name}">
<xsl:copy-of select="attribute::*"/>
<xsl:apply-templates mode="h"/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

And the resulting document, transformed with Ant 1.6.2's "xslt" task:

<?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"> <head> <title>Thought</title> </head> <body> <div xml:lang="fr"> <p> Tu es <em title="Hot!">belle</em>. </p> </div> </body> </html>

Valid according to http://validator.w3.org/
Thank you all. And thank you David for the recursive template.
 

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

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top