namespace problem with XSLT

G

Greg

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. 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.)
 

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
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top