Nokogiri can't parse XSLT which imports elements

548 views
Skip to first unread message

Xin Zheng

unread,
Oct 4, 2010, 4:17:33 AM10/4/10
to nokogiri-talk
I am trying to use Nokogiri to transform an XML using XSLT.

Using the following instructions from the tutorials:

doc = Nokogiri::XML(File.read('some_file.xml'))
xslt = Nokogir::XSLT(File.read('some_transformer.xslt'))

puts xslt.transform(doc)

Replacing some_file.xml and some_transformer.xslt with my own files, I
get this error:

I/O warning : failed to load external entity "cals2html.xsl"
RuntimeError: compilation error: element import

from /opt/local/lib/ruby/gems/1.8/gems/nokogiri-1.4.3.1/lib/nokogiri/
xslt.rb:25:in `parse_stylesheet_doc'
from /opt/local/lib/ruby/gems/1.8/gems/nokogiri-1.4.3.1/lib/nokogiri/
xslt.rb:25:in `parse'
from /opt/local/lib/ruby/gems/1.8/gems/nokogiri-1.4.3.1/lib/nokogiri/
xslt.rb:13:in `XSLT'
from (irb):19
from :0

'cals2html.xsl' is imported like this:
<xsl:import href="cals2html.xsl"/>

The file exists in the same folder as the parent XSLT file.

<xsl:import href="/Users/myusername/dir1/cals2html.xsl"/>
If I use absolute path in import like above, it transforms fine!

I'm new to XSLT but I understand relative filenames in import is
suppose to work?

I tried ruby-xslt and it transformed without any problems.

Can anyone shed some light on this?

Thanks in advance.

Aaron Patterson

unread,
Oct 4, 2010, 1:23:55 PM10/4/10
to nokogi...@googlegroups.com
Hi,

When you use `File.read`, it reads the file in to a string. Since the
file contents are now in a string, nokogiri has no knowledge about
what directory that file lived in when it was read.

If you pass an open file handle to nokogiri, it will figure out extra
information about the file.

Try like this:

doc = Nokogiri.XML(File.open('some_file.xml', 'rb'))
xslt = Nokogir.XSLT(File.open('some_transformer.xslt', 'rb'))

That way, nokogiri will have the info it needs to find relative files.

Hope that helps!

--
Aaron Patterson
http://tenderlovemaking.com/

Xin Zheng

unread,
Oct 6, 2010, 12:07:38 PM10/6/10
to nokogiri-talk
Thanks Aaron, that did the trick!

I was thinking along similar lines of Nokogiri not knowing where the
file sits but couldn't quite make the connection to pass it the file
handle. Now I know:)

Pablo Ruiz

unread,
Oct 7, 2010, 2:40:29 AM10/7/10
to nokogiri-talk
Hi,
I'm using Nokogiri version 1.4.3.1 and when trying do

xslt = Nokogiri::XSLT(File.read(XSLT_FILE)) //tried doing a File.open
as stated above

where XSLT_FILE is this stylesheet(http://www.sat.gob.mx/
sitio_internet/cfd/2/cadenaoriginal_2_0/cadenaoriginal_2_0.xslt) which
I have in a directory in my rails app,
I get the following error:

RuntimeError: compilation error: element stylesheet

from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.3.1/lib/nokogiri/
xslt.rb:25:in `parse_stylesheet_doc'
from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.3.1/lib/nokogiri/
xslt.rb:25:in `parse'
from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.3.1/lib/nokogiri/
xslt.rb:13:in `XSLT'

Any ideas on what can be wrong?

Thanks,
PRuiz

Pablo Ruiz

unread,
Oct 8, 2010, 1:13:38 PM10/8/10
to nokogiri-talk
It is worth noting that I checked this xslt to be working with other
applications...
xslt url: http://www.sat.gob.mx/sitio_internet/cfd/2/cadenaoriginal_2_0/cadenaoriginal_2_0.xslt
Reply all
Reply to author
Forward
0 new messages