nori
nori copied to clipboard
XML Parsing fails with unescaped ampersand in content (not tag)
If I have XML like this:
<?xml version="1.0" encoding="UTF-8" ?>
<outer>
<inner>
<before>data before</before>
<data>Some & More</data>
<after>here is after</after>
</inner>
</outer>
and try to parse it like this:
xml = File.read("bad.xml")
result = Nori.new.parse(xml)
I get this:
{
"data" => "Some More\n here is after\n \n"
}
Which is clearly wrong. If I change the & into & it parses just fine:
<?xml version="1.0" encoding="UTF-8" ?>
<outer>
<inner>
<before>data before</before>
<data>Some & More</data>
<after>here is after</after>
</inner>
</outer>
{
"outer" => {
"inner" => {
"before" => "data before",
"data" => "Some & More",
"after" => "here is after"
}
}
}
Why can't I use a raw & in the content? That seems to be a bug, right?
My bad... I didn't know my XML validation well enough. Apparently a "naked ampersand" is invalid. It either needs to be part of an HTML encoding (like < for example) or it needs to be encoded itself (as in: & ). So this isn't a Nori issue at all.
However, I wonder if Nori should do something other than just try and parse, because the result was really not what it should be.