Skip to content

Instantly share code, notes, and snippets.

@gioele
Created August 10, 2013 07:35
Show Gist options
  • Save gioele/6199467 to your computer and use it in GitHub Desktop.
Save gioele/6199467 to your computer and use it in GitHub Desktop.
Problem with XLST identity and JRuby, https://github.com/sparklemotion/nokogiri/issues/954
#!/usr/bin/env ruby
require 'nokogiri'
doc = Nokogiri::XML(%q{
<doc>
<sp id="sp1">
<speaker>Second</speaker>
<l n='5'>a</l>
<l n='8'>a</l>
<pb n='2'/>
</sp>
<sp id="sp2">
<!-- if you remove this element no spurios text will be generated-->
<speaker>Third</speaker>
<l n='9'>a</l>
<l n='10'>a</l>
<pb n='3'>
</sp>
</doc>
})
xslt = Nokogiri::XSLT(DATA.read)
portion = xslt.transform(doc)
puts portion.to_xml
puts "is text empty? it should be..."
text = portion.text.strip
p text.empty? #=> should be true
p text #=> in JRuby it prints "doc>"
__END__
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="*">
<xsl:variable name="has" select='boolean(.//*[name()="pb" and @n="2"])'/>
<xsl:variable name="is" select='name()="pb" and @n="2"'/>
<xsl:if test="$has or $is">
<xsl:copy>
<xsl:apply-templates select="*|@*|text()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="@*|text()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment