2

Nokogiri を使用して XML ドキュメントを JSON に変換しています。コードは簡単です:

@document = Nokogiri::XML(entry.data)
xslt = Nokogiri::XSLT(File.read("#{File.dirname(__FILE__)}/../../xslt/my.xslt"))
transform = xslt.transform(@document)

entryこの場合は Mongoid ベースのモデルであり、データは MongoDB に文字列として格納された XML BLOB 属性です。

の内容をダンプするとtransform、JSON がそこにあります。問題は、ノコギリがドキュメントの上部に次のタグを付けていることです。

<?xml version="1.0"?>

それに対処する正しい方法は何ですか?

4

1 に答える 1

2

#apply_to以下の方法を試してください( Source):

require 'nokogiri'

doc  = Nokogiri::XML('<?xml version="1.0"><root />')
xslt = Nokogiri::XSLT("<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'/>")
puts xslt.transform(doc)
puts "######"
puts xslt.apply_to(doc)

# >> <?xml version="1.0"?>
# >> ######
# >> 
于 2013-08-19T19:34:47.840 に答える