Skip to content

Instantly share code, notes, and snippets.

@tobireif
Created May 4, 2009 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobireif/106373 to your computer and use it in GitHub Desktop.
Save tobireif/106373 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'nokogiri'
def xpath_values doc, for_each_xpath, value_xpath
xslt_str =
%{<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Not well-formed: -->
<xsl:template match="/"/>
<values>
<xsl:for-each select="#{for_each_xpath}">
<value>
<xsl:value-of select="#{value_xpath}"/>
</value>
</xsl:for-each>
</values>
</xsl:template>
</xsl:stylesheet>}
xslt = Nokogiri::XSLT.parse(xslt_str)
# I think this should throw an error for the well-formedness error
# in the XSLT stylesheet, but there should be no segfault.
result_doc = xslt.transform(doc)
end
doc = Nokogiri::XML('<foo id="a"><b id="b"/></foo>')
p xpath_values(doc,"//*", "@id")
# I get this:
=begin
$ ruby noko.rb
compilation error: element for-each
element for-each only allowed within a template, variable or param
compilation error: element value-of
element value-of only allowed within a template, variable or param
Found a top-level element values with null namespace URI
noko.rb:23: [BUG] Segmentation fault
ruby 1.8.6 (2007-03-13) [i686-linux]
Aborted
$ sudo gem list --local nokogiri
*** LOCAL GEMS ***
nokogiri (1.2.3)
$ dpkg -s libxml2-dev | grep Version
Version: 2.6.32.dfsg-5ubuntu4
$ dpkg -s libxslt1-dev | grep Version
Version: 1.1.24-2ubuntu2
$ uname -a
Linux thinkpad-ubuntu 2.6.28-11-generic #42-Ubuntu SMP [...] i686 GNU/Linux
$
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment