By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,375 Members | 1,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,375 IT Pros & Developers. It's quick & easy.

namespace problem with XSLT

P: n/a
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:output 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:output 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

Jul 20 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
This is a very FAQ.

See:

http://www.topxml.com/people/bosley/defaultns.asp
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"Bernd Fuhrmann" <si**********@gmx.de> wrote in message
news:bl*************@news.t-online.com...
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:output 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:output 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

Jul 20 '05 #2

P: n/a
Dimitre Novatchev wrote:
See:
http://www.topxml.com/people/bosley/defaultns.asp 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

Jul 20 '05 #3

P: n/a
> 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?

Can you give an example?
=====
Cheers,

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

Jul 20 '05 #4

P: n/a
Dimitre Novatchev wrote:
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:output 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

Jul 20 '05 #5

P: n/a
In article <bl*************@news.t-online.com>, Bernd Fuhrmann wrote:
[snip]
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.
Jul 20 '05 #6

P: n/a
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" <si**********@gmx.de> wrote in message
news:bl*************@news.t-online.com...
Dimitre Novatchev wrote:
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:output 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

Jul 20 '05 #7

P: n/a
Dimitre Novatchev wrote:
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
Jul 20 '05 #8

P: n/a
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:output
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.

Jul 20 '05 #9

P: n/a
> 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,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Spread the wiki (http://en.wikipedia.org)
Jul 20 '05 #10

P: n/a

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
Jul 20 '05 #11

P: n/a

me: <xsl:template mode="h">

should be

<xsl:template mode="h" match="*">
^^^^^^^^
of course, sorry.
Jul 20 '05 #12

P: n/a
"Joris Gillis" <ro**@pandora.be> writes:
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,

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

David
Jul 20 '05 #13

P: n/a
>> 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,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Spread the wiki (http://en.wikipedia.org)
Jul 20 '05 #14

P: n/a
"Joris Gillis" <ro**@pandora.be> writes:
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>


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

David
Jul 20 '05 #15

P: n/a
>
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.

Jul 20 '05 #16

P: n/a
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
Jul 20 '05 #17

P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>,
Greg <sa**********@hotmail.com> wrote:
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
Jul 20 '05 #18

P: n/a
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:output
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.

Jul 20 '05 #19

P: n/a


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

oops sorry: do what I mean not what I write,
@* or as you say, attribute::* not just @

David

PS the xsl-list faq lists more or less the same thing here:

http://www.dpawson.co.uk/xsl/sect2/N...tml#d6408e1750
Jul 20 '05 #20

P: n/a
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:output
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. Clever
trick.

(P.S. I would have done it with XHTML 1.1 instead of XHTML 1.0, but I
wrote the definition for my own XML document with XML Schema and,
afaik, there is no XML Schema document defining XHTML 1.1--just a
DTD--and I don't yet know how to validate an XML document against an
XML Schema for some (my) parts and also against a DTD for other (XHTML
1.1) parts. For XHTML 1.0 Strict, though, I have found an XML Schema
document (at http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd) so my
XML document can be validated against the two definitions (mine and
XHTML 1.0's) because both are XML Schema documents. But that is another
story, perhaps for another thread.)

Jul 20 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.