Problems passing parameters into XSLT


#1

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"?>

----- 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="/">

This is the value of root is <xsl:value-of select="$root"
/>


This is the value of back is <xsl:value-of select="$back"
/>



</xsl:template>
</xsl:stylesheet>

When run produces the following output:

$ ./run.rb

<?xml version="1.0"?>

This is the value of root is

This is the value of back is

$

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"?>

This is the value of root is ROOT

This is the value of back is BACK

$

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)


#2

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


#3

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.