REXML set default namespace

str = ‘Google

doc = Document.new(str)

(desired incantation)

doc.to_s desired output:

Google

More specifically in my situation I have a document (RSS) in one namespace
and I want to do something like:

str = 'foo



description_el << Document.new(str)

and get something like:


<xhtml:a href=“foo”>fooxhtml:hr/</xhtml:a>

or even

foo

str is user-entered and for the application asking them to write their own
xhtml is fine, but asking them to prepend the xhtml prefix to every
element is silly.

doc.namespace= doesn’t work, which is the only thing I can divine as maybe
being a sensible thing to do.

Does this do what you want?

doc = Document.new(str)
doc[0].add_attributes(“xmlns”=>“http://www.w3.org/1999/xhtml”)
doc.to_s
#=> “Google

–Mark

···

On Mar 2, 2004, at 1:04 PM, Hans Fugal wrote:

str = ‘Google

doc = Document.new(str)

(desired incantation)

doc.to_s desired output:

Google

Does this do what you want?

doc = Document.new(str)
doc[0].add_attributes(“xmlns”=>“http://www.w3.org/1999/xhtml”)
doc.to_s
#=> “Google

–Mark

Thank you, that is a solution to my question. Now I’ve found I asked the
wrong question :frowning:

Let me give more detail. I’m generating RSS with an xslt instruction like
this:

<?xml-stylesheet type='application/xml' href='default.xsl'?>

Now, given the following segment from the RSS:

<item>
  <description>
    <a href='http://google.com/'>Google</a>
  </description>
</item>

and the following segment from default.xsl:
<xsl:value-of select=“description” disable-output-escaping=“yes”/>

The output of xsltproc (which I use for testing) is what I would expect,
Google”. But Mozilla’s internal XSLT
engine does not honor disable-output-escaping, resulting in the browser
displaying the markup. The mozilla developers claim this is expected
behavoir which they don’t intend to ‘fix’.

The supposed solution[1] is to replace the xsl above with
<xsl:copy-of select=“description/*”/>

This doesn’t work with the above example because the a tag gets a default
namespace of “”, hence my question. In tests if I put the namespace in the
input data this solution works fine. In the case that I have a well-formed
xml document as the sole content of the description tag (e.g. not mixed or
only text) the solution you presented works, otherwise it doesn’t.

e.g. this would fail:
This is a link to something

as would this:
Foo

and this:
foobar

So as I see it there are two solutions: make the content of description
well-formed xml, e.g. by wrapping the whole thing in a div, or adding a
namespace or prefix to every element that is a child of description that
does not already have a namespace/prefix. The latter is what I’d like to
do, the former feels klugey. I can’t figure out how to do the latter in
REXML though. Any ideas?

  1. http://home.clear.net.nz/pages/c.evans/proto/MoveableTypeXSLT/

Hans,

I’m having a hard time understanding your problem, so I have some
questions.

Hans Fugal fugalh@byu.edu wrote in message news:pan.2004.03.02.22.54.51.544450@byu.edu

Let me give more detail. I’m generating RSS with an xslt instruction like
this:

Now, given the following segment from the RSS:

<item>
  <description>
    <a href='http://google.com/'>Google</a>
  </description>
</item>

and the following segment from default.xsl:
<xsl:value-of select=“description” disable-output-escaping=“yes”/>

What are you trying to do in the large picture? You say you’re
generating RSS, and it looks like you’re transforming an RSS document;
so:

RSS --- XSL ---> RSS

and you want to copy the contents of the description from one RSS to
another. Is that correct?

The supposed solution[1] is to replace the xsl above with
<xsl:copy-of select=“description/*”/>

Yes, that is what I’d recommend. copy-of should preserve the
namespace, by the way, so if description is in the HTML namespace on
input, it should also be in that namespace on output.

This doesn’t work with the above example because the a tag gets a default
namespace of “”, hence my question. In tests if I put the namespace in the

Why is this a problem? It didn’t have the HTML namespace in the
original RSS, so why should it have the HTML namespace in the new RSS?

And what does this have to do with REXML? So far, it looks like
you’re discussion XSL issues.

So as I see it there are two solutions: make the content of description
well-formed xml, e.g. by wrapping the whole thing in a div, or adding a
namespace or prefix to every element that is a child of description that
does not already have a namespace/prefix. The latter is what I’d like to
do, the former feels klugey. I can’t figure out how to do the latter in
REXML though. Any ideas?

At this point, I’m going to ignore the entire XSLT thread, because I
don’t see how it is relevant to using REXML to generate XML. Unless
you’re generating RSS that is the /input/ to the XSLT, in which case,
we can still ignore the XSL, because all you care about is generating
valid RSS, and the transformation questions are a separate issue.

If the RSS spec requires you to place HTML content in the HTML
namespace, then you have to do that when generating the RSS.

Section 5.2 of the namespace specification says that all elements have
a namespace that is the default namespace of the parent; EG:

<div xmlns='html'>
	<a/>
</div>

the namespace of “a” is “html”. This means that “adding a
namespace or prefix to every element that is a child of description
that does not already have a namespace/prefix” isn’t practical,
because elements inherit their namespace from their parent. Either
there is a default namespace declared, or there isn’t, and neither
case will help you filter elements the way you want to.

I doubt that I’ve answered any questions, because I don’t think I
understood the question in the first place, but perhaps we can clarify
what exactly you’re having trouble with.

— SER