Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow attributes without namespace on jRuby #2677

Open
flavorjones opened this issue Oct 20, 2022 Discussed in #2675 · 3 comments
Open

Allow attributes without namespace on jRuby #2677

flavorjones opened this issue Oct 20, 2022 Discussed in #2675 · 3 comments

Comments

@flavorjones
Copy link
Member

Discussed in #2675

Originally posted by ShockwaveNN October 20, 2022
Hi, trying to port one of my library for parsing ooxml to jruby and I got a problem

I got this XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
          xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  <bookViews>
    <workbookView r:xWindow="360" yWindow="15" windowWidth="20955"
                  windowHeight="9720" activeTab="0"/>
  </bookViews>
</workbook>

And simplified ruby code to parse it:

require 'nokogiri'
xml = Nokogiri::XML(File.open('example.xml'), &:strict)
xml.xpath('xmlns:workbook/xmlns:bookViews/xmlns:workbookView').each do |sheet|
  puts sheet.attribute('xWindow').value
end

On mruby this output 360

But on jruby it just crash with:

NoMethodError: undefined method `value' for nil:NilClass

If I change puts to

  puts sheet.attribute('r:xWindow').value

It works correctly on both rubies

I understand that mruby and jruby uses different XML parser

Is there any option to jruby to make parsing of such attributes same as on jruby or proper way will be to fix my codebase by adding r: namespace?

@flavorjones
Copy link
Member Author

Hi! I've reproduced what you're seeing, and just to share my analysis:

#! /usr/bin/env ruby

require "nokogiri"

xml = <<~XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
          xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  <bookViews>
    <workbookView r:xWindow="360" yWindow="15" windowWidth="20955"
                  windowHeight="9720" activeTab="0"/>
  </bookViews>
</workbook>
XML

doc = Nokogiri::XML(xml, &:strict)
sheet = doc.at_xpath('xmlns:workbook/xmlns:bookViews/xmlns:workbookView')
puts sheet.attribute('xWindow').inspect

On CRuby, this outputs something like:

#<Nokogiri::XML::Attr:0x50 name="xWindow" namespace=#<Nokogiri::XML::Namespace:0x3c prefix="r" href="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> value="360">

but on JRuby, the attribute lookup fails and so the script prints nil.

This appears to be a bug, and I'm honestly surprised that we don't have tests for the desired behavior. I'll dig in!

@flavorjones
Copy link
Member Author

OK, this isn't a simple fix, unfortunately, because the JRuby implementation is handling attributes inconsistently, sometimes treating local names as prefixed names, and vice versa, in a few different places.

I'll work on it, but it may not be fixed immediately.

@flavorjones
Copy link
Member Author

See #2679 for the work-in-progress. Maybe this branch is good enough for you? If so, please let me know and I may be able to get that partial fix into the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant