Forum: Ruby Problems passing parameters into XSLT

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6d9bf78ca49a017e9e3e6b0357b6c59e?d=identicon&s=25 Peter Hickman (Guest)
on 2008-11-17 11:49
(Received via mailing list)
I'm having some problems passing parameters into XSLT with ruby. The
following script, based on the example in the docs

----- run.rb

#!/usr/bin/ruby

require 'rubygems'

require 'xml'
require 'xslt'

# Create a new XSL Transform
stylesheet_doc = LibXML::XML::Document.file('browse.xsl')
stylesheet = LibXSLT::XSLT::Stylesheet.new(stylesheet_doc)

# Transform a xml document
xml_doc = LibXML::XML::Document.file('page.xml')
result = stylesheet.apply(xml_doc, {:root => "ROOT", :back => "BACK"})

puts result

Processes these two files

----- page.xml

<?xml version="1.0" encoding="UTF-8"?>
<doc />

----- browse.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output method="xml" indent="yes" />

  <xsl:param name="root" />
  <xsl:param name="back" />

  <xsl:template match="/">
    <doc>
      <p>This is the value of root is <xsl:value-of select="$root"
/></p>
      <p>This is the value of back is <xsl:value-of select="$back"
/></p>
  </doc>
  </xsl:template>
</xsl:stylesheet>

When run produces the following output:

$ ./run.rb
<?xml version="1.0"?>
<doc>
  <p>This is the value of root is </p>
  <p>This is the value of back is </p>
</doc>
$

As you can see the parameters 'root' and 'back' have not been filled in.
When I run the data with xsltproc it works ok which would suggest that
it is not a problem with the data or the stylesheet.

$ xsltproc --stringparam root 'ROOT' --stringparam back 'BACK'
browse.xsl page.xml
<?xml version="1.0"?>
<doc>
  <p>This is the value of root is ROOT</p>
  <p>This is the value of back is BACK</p>
</doc>
$

Here is what I have:

$ ruby -v
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
$ gem list

*** LOCAL GEMS ***

actionmailer (2.1.2)
actionpack (2.1.2, 1.13.6)
actionwebservice (1.2.6)
activerecord (2.1.2, 1.15.6)
activesupport (2.1.2, 1.4.4)
acts_as_ferret (0.4.3)
capistrano (2.0.0)
cgi_multipart_eof_fix (2.5.0)
daemons (1.0.10)
dnssd (0.7.0)
fastthread (1.0.1)
fcgi (0.8.7)
ferret (0.11.6)
gem_plugin (0.2.3)
highline (1.5.0)
hpricot (0.6.164)
json (1.1.3)
libxml-ruby (0.8.3)
libxslt-ruby (0.8.2)
mongrel (1.1.5)
needle (1.3.0)
net-sftp (2.0.1)
net-ssh (2.0.4)
rails (2.1.2)
rake (0.8.3)
RedCloth (3.0.4)
ruby-openid (2.1.2)
ruby-yadis (0.3.4)
rubynode (0.1.5)
sqlite3-ruby (1.2.4)
termios (0.9.4)
6d9bf78ca49a017e9e3e6b0357b6c59e?d=identicon&s=25 Peter Hickman (Guest)
on 2008-11-19 10:11
(Received via mailing list)
Answering myself here. This is the problem line:

result = stylesheet.apply(xml_doc, {:root => "ROOT", :back => "BACK"})

and this is the corrected line.

result = stylesheet.apply(xml_doc, {:root => "'ROOT'", :back =>
"'BACK'"})

Note that "ROOT" has become "'ROOT'". That is the word ROOT wrapped in
's and
then wrapped in "s
7a966bb1534f0ea863bc9ffe65ae449f?d=identicon&s=25 Jamie Orchard-hays (jamieorc)
on 2009-01-15 05:30
I just spent an inordinate amount of time trying to figure out why my
params were empty. Thank god you posted this. The RDoc just shows simple
quotes, not quotes in quotes.
This topic is locked and can not be replied to.