3

I'm trying to include a URL to an image in an XML file, and the ampersands in the URL query string are getting stripped out:

bgdoc.xpath('//Master').each do |elem|
  part = elem.xpath('Part').inner_text
  image = imagehash[part]
  image = "" if image.blank?
  elem.xpath('Image').first.content = "<![CDATA[#{image}]]>"
  puts elem.xpath('Image').first.content
end

bgdoc is getting written out with the help of Builder later on. But not the individual elements, it's getting inserted all at once. That makes it a different case than a similar question posted on SO.

2 Answers 2

4

You should be using create_cdata to create a CDATA node and then add_child to add it to the document, just assigning a string to content will leave you with &lt;!CDATA... in your XML and that's not very useful.

A short example should illustrate the process:

xml   = '<Master><Image></Image><Image></Image></Master>'
bgdoc = Nokogiri::XML(xml)
cdata = bgdoc.create_cdata('/where?is=pan&cakes=house')
bgdoc.xpath('//Image').first.add_child(cdata)

Then, if you bgdoc.to_xml you'll get something like this:

<?xml version="1.0"?>
<Master>
    <Image><![CDATA[/where?is=pan&cakes=house]]></Image>
    <Image/>
</Master>

I think that's the sort of result you're looking for. However, if you just assign a string to content:

bgdoc.xpath('//Image').first.content = '<![CDATA[/where?is=pan&cakes=house]]>'

Then you get this XML:

<?xml version="1.0"?>
<Master>
    <Image>&lt;![CDATA[/where?is=pan&amp;cakes=house]]&gt;</Image>
    <Image/>
</Master>

and that doesn't even have a CDATA node.

0

Did you try to replace the ampersand with its xml/html character code ? It should be working.

The code for the ampersand is: &amp;

Click here for more information about character codes.

2
  • The real problem here is that AKWF is not actually adding a CDATA node to their document, they're just adding a string which will be XML-encoded to escape <, >, and &. Using &amp; instead of & will yield &amp;amp; in the XML. Dec 20, 2011 at 21:06
  • That's exactly what happened. Dec 20, 2011 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.