Copying without namespace?

A

Andre-John Mas

Hi,

I have some code that looks as follows:

<xsl:for-each select="$pageMetaData/*">
<xsl:choose>
<xsl:when test="local-name() = 'meta'">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:eek:therwise>
<meta content="{.}" name="{local-name()}"/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:for-each>

The problem is that it copies the namespace when it does so (it
appears to be implied, rather than declared). So this:

<meta content="abc" name="dc.creator"/>

becomes this in the final output:

<meta xmlns="http://www.myco.com/datalist" content="yuri"
name="dc.creator"/>

How do I copy the element and not have the namespace in the result?
Note that I only want to apply this here, and not globaly to all
cases.

Andre
 
P

Pavel Lepin

Andre-John Mas said:
<xsl:for-each select="$pageMetaData/*">
<xsl:choose>
<xsl:when test="local-name() = 'meta'">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:eek:therwise>
<meta content="{.}" name="{local-name()}"/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:for-each>
Awful.

The problem is that it copies the namespace when it does
so (it appears to be implied, rather than declared).

"Implied rather than declared"? What do you mean?
So this:

<meta content="abc" name="dc.creator"/>

becomes this in the final output:

<meta xmlns="http://www.myco.com/datalist" content="yuri"
name="dc.creator"/>

And what is the problem - the fact that your resulting meta
element is in http://www.myco.com/datalist namespace, or
the fact that this namespace is explicitly declared as the
default namespace on this element?
How do I copy the element and not have the namespace in
the result?

You can't. You can only create an element with the same
name, but not in a namespace.

I would recommend posting a minimal complete example of your
problem, since it's not really clear to me what your
problem is. Reading some introductory materials on XML
Namespaces might be advisable as well.
 
J

Johannes Koch

Pavel said:
"Implied rather than declared"? What do you mean?


And what is the problem - the fact that your resulting meta
element is in http://www.myco.com/datalist namespace, or
the fact that this namespace is explicitly declared as the
default namespace on this element?


You can't. You can only create an element with the same
name, but not in a namespace.

I would recommend posting a minimal complete example of your
problem, since it's not really clear to me what your
problem is.

I guess, the problem is that DTDs are not namespace-aware and so the
result of the transformation is not DTD-valid.
 
P

Pavel Lepin

Johannes Koch said:
I guess, the problem is that DTDs are not namespace-aware
and so the result of the transformation is not DTD-valid.

I think someone should start a "Let the DTDs go!" movement,
would be about time. Hmm, maybe I could, but... hold on for
a second... Yep. That's my natural laziness kicking in.
Never mind.
 
J

Joseph Kesselman

Johannes said:
I guess, the problem is that DTDs are not namespace-aware and so the
result of the transformation is not DTD-valid.

DTDs and namespaced XML are incompatable at some pretty basic levels.
Namespace-aware processing assumes that prefixes are purely syntactic
sugar and can be changed at will. DTDs will insist they be nailed down.

The best suggestion I can give you for intermixing the two is to have
your DTD hard-code a default namespace assignment and not use prefixes,
and to have all the elements in the DTD likewise set up that default but
permit it to be explicitly specified in case someone re-asserts it.

The best suggestion I can give you generally is to stop trying to
intermix these, and move to XML Schema.
 
A

Andre-John Mas


I am not going argue this point. I am trying to convert code that
claims it was creating xhtml pages (it was creating html), to actually
create xhtml compliant output. There have been plenty of developers
dipping in this code before me.
"Implied rather than declared"? What do you mean?





And what is the problem - the fact that your resulting meta
element is inhttp://www.myco.com/datalistnamespace, or
the fact that this namespace is explicitly declared as the
default namespace on this element?

This name space is something used in the XSL, to refer to a
configuration file specifying localised texts, it is not something
that is meant to be in the output document.

I'll see if I can cobble together an example, but basically what is
being done is copying a block like this, declared in an XML file:

<meta-tags>
<meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57
GMT" />
<meta name="authour" content="the author" />
</meta-tags>

to the output which will be used for rendering the final xhtml page.
For example:

<head>
<meta name="authour" content="the author" />
</head>

The issue is that I end up with an xhtml document that includes
namespaces that were only meant to be used in the xsl and not the
final document. Having the "http://www.myco.com/datalist" specified
as part of the meta tag just breaks the page.

Andre
 
J

Joseph Kesselman

Andre-John Mas said:
The issue is that I end up with an xhtml document that includes
namespaces that were only meant to be used in the xsl and not the
final document.

Sounds like you're looking for xsl:stylesheet's exclude-result-prefixes
attribute.
 
P

Pavel Lepin

Joseph Kesselman said:
Sounds like you're looking for xsl:stylesheet's
exclude-result-prefixes attribute.

Actually, to me it looks more like the OP wants to copy
{meta-data-namespace}meta element as an
{xhtml-namespace}meta element. Namespace prefix declaration
alone shouldn't break XHTML validation I believe. meta
element being in the wrong namespace would, on the other
hand.
 
J

Johannes Koch

Pavel said:
Namespace prefix declaration
alone shouldn't break XHTML validation I believe.

It breaks XHTML DTD validation, because the XHTML DTDs allow the xmlns
"attribute" on the html element only.
 
P

Pavel Lepin

Johannes Koch said:
It breaks XHTML DTD validation, because the XHTML DTDs
allow the xmlns "attribute" on the html element only.

Oops, my fault.

Just reinforces my anti-DTD sentiment, though. I mean,
seriously, in case of XHTML using DTD for validation is
about as bad as using Appendix C for 'compatibility'. Both
mean we cannot freely use XML tools for processing without
a lot of kludgery to make sure nothing 'breaks' in the
pipeline. Kinda defeats the whole point of XML.
 
A

Andre-John Mas

Sounds like you're looking for xsl:stylesheet's exclude-result-prefixes
attribute.

That what I tried doing, currently I have:

exclude-result-prefixes="i18n ota dataList iItemList math"

which excludes non-anonymous namespaces, such as:

xmlns:i18n=""
xmlns:math=""

but how do you exclude an anonymous namespace:

xmlns=""

Reading elsewhere I was given the suggestion of using:

<!-- copies without the template -->
<xsl:template match="*">
<xsl:element name="{name()}" namespace="">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

and changed the original block to:

<xsl:for-each select="$pageMetaData/*">
<xsl:choose>
<xsl:when test="local-name() = 'meta'">
<xsl:apply-templates select="."/>
</xsl:when>
<xsl:eek:therwise>
<meta content="{.}" name="{local-name()}"/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:for-each>

but I am not sure this is the best approach.

Andre
 
J

Joseph Kesselman

Andre-John Mas said:
but how do you exclude an anonymous namespace:
xmlns=""

The default namespace declaration is generally not generated into the
output unless it is required, meaning that the parent element is in a
different default namespace. I'd have to take another look at your
specific example to see whether that's what's going on, but if it is
then the proper answer is "if you don't want to change default
namespaces, don't output an element that's in a different default
namespace than its parent."
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top