Problems passing parameters into XSLT

P

Peter Hickman

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:eek:utput 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)
 
P

Peter Hickman

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
 
J

Jamie Orchard-hays

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top