Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.