Nokogiri: SVG Parsing Error

Created on 1 Nov 2016  路  3Comments  路  Source: sparklemotion/nokogiri

We have an svg with a large base64 encoded image (attached) that is not parsing correctly.

file = Nokogiri::XML(File.read('DM_V3-front.svg'))
file.remove_namespaces!

file.xpath('//image/@href').count             # => 0 (should be 1)
file.xpath('//image').count                   # => 1 (it is finding the image like this)

The problem is that when the svg is parsed it turns the href attribute into text outside of the image tag for some reason. This only happens when the base64 data is quite large, smaller base64 encoded images work fine as far as we can tell.

The sample file below illustrates the issue and has only one image to facilitate reproducing this. I would be happy to look into this issue further if someone could point me in the right direction.

DM_V3-front.svg.zip

Most helpful comment

libxml and therefore nokogiri have some hardcoded limits about the maximum sizes of elements. You can disable them by:

file = Nokogiri::XML(File.read('DM_V3-front.svg'), nil, nil, Nokogiri::XML::ParseOptions::HUGE)

This should solve your issue.

All 3 comments

libxml and therefore nokogiri have some hardcoded limits about the maximum sizes of elements. You can disable them by:

file = Nokogiri::XML(File.read('DM_V3-front.svg'), nil, nil, Nokogiri::XML::ParseOptions::HUGE)

This should solve your issue.

Out of curiosity is this documented somewhere that I missed. I don't recall seeing anything about that option but I may have just missed it in the docs. In all honestly I probably should have dug into the source a bit.

It's an implementation detail of libxml2, so although the parse options are documented, this particular option is not mentioned in the tutorials.

Was this page helpful?
0 / 5 - 0 ratings