REXML set default namespace

H

Hans Fugal

str = '<html><body><a href="http://google.com/">Google</a></body></html>'

doc = Document.new(str)

(desired incantation)

doc.to_s desired output:
<html xmlns="http://www.w3.org/1999/xhtml"><body><a
href="http://google.com/">Google</a></body></html>

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

str = '<a href="foo">foo<hr/></a>'
description_el << Document.new(str)

and get something like:
<rss xmlns:xhtml="http://www.w3.org/1999/xhtml">
<item><description>
<xhtml:a href="foo">foo<xhtml:hr/></xhtml:a>
</description></item>
</rss>

or even

<rss>
<item><description>
<a xmlns="http://www.w3.org/1999/xhtml" href="foo">foo<hr/></a>
</description></item>
</rss>

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

Mark Hubbart

str = '<html><body><a
href="http://google.com/">Google</a></body></html>'

doc = Document.new(str)

(desired incantation)

doc.to_s desired output:
<html xmlns="http://www.w3.org/1999/xhtml"><body><a
href="http://google.com/">Google</a></body></html>

Does this do what you want?

doc = Document.new(str)
doc[0].add_attributes("xmlns"=>"http://www.w3.org/1999/xhtml")
doc.to_s
#=> "<html xmlns='http://www.w3.org/1999/xhtml'><body><a
href='http://google.com/'>Google</a></body></html>"

--Mark
 
H

Hans Fugal

Does this do what you want?

doc = Document.new(str)
doc[0].add_attributes("xmlns"=>"http://www.w3.org/1999/xhtml")
doc.to_s
#=> "<html xmlns='http://www.w3.org/1999/xhtml'><body><a
href='http://google.com/'>Google</a></body></html>"

--Mark

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

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,
"<a href='http://google.com/'>Google</a>". 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:
<description>This is a <a href='foo'>link</a> to something</description>

as would this:
<description>Foo</description>

and this:
<description><a href='foo'>foo</a><a href='bar'>bar</a></description>

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/
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top