public
Created

Nokogiri conundrum

  • Download Gist
code.rb
Ruby
1 2 3 4 5 6 7 8
def to_ddoc
xmldata = Nokogiri::XML(ddoc_container)
xmldata.css('DataFile').each_with_index do |el, i|
frag = Nokogiri::XML.fragment(documents[i].datafile(i))
el.replace frag
end
xmldata.to_xml
end
sample.ddoc.xml
Liquid
1 2 3 4 5 6 7 8
<?xml version="1.0" encoding="UTF-8"?>
<SignedDoc format="DIGIDOC-XML" version="1.3" xmlns="http://www.sk.ee/DigiDoc/v1.3.0#">
<!-- Default namespace exists in Nokogiri::XML object, but it's missing in serialized form -->
<DataFile ContentType="EMBEDDED_BASE64" Filename="10.1.1.210.2072.pdf" Id="D0" MimeType="application/pdf" Size="684191">
<!-- BASE64 data truncated -->
</DataFile>
<!-- Extra metadata here -->
</SignedDoc>
xmldata_debug_fragment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# This is xmldata in pry before to_xml call
=> #(Document:0x3fdeceebf89c {
name = "document",
children = [
#(Element:0x3fdeceebf7c0 {
name = "SignedDoc",
namespace = #(Namespace:0x3fdeceebf798 {
href = "http://www.sk.ee/DigiDoc/v1.3.0#"
}),
attributes = [
#(Attr:0x3fdece969fb4 { name = "format", value = "DIGIDOC-XML" }),
#(Attr:0x3fdece969fa0 { name = "version", value = "1.3" })],
children = [
#(Text "\n"),
#(Element:0x3fdecf67ab04 {
name = "DataFile",
namespace = #(Namespace:0x3fded0e98a60 {
href = "http://www.sk.ee/DigiDoc/v1.3.0#"
}),
attributes = [
#(Attr:0x3fded0e989e8 {
name = "ContentType",
value = "EMBEDDED_BASE64"
}),
#(Attr:0x3fded0e989c0 {
name = "Filename",
value = "10.1.1.210.2072.pdf"
}),
#(Attr:0x3fded0e989ac { name = "Id", value = "D0" }),
#(Attr:0x3fded0e98998 {
name = "MimeType",
value = "application/pdf"
}),
#(Attr:0x3fded0e98984 { name = "Size", value = "684191" })],
children = [
#(Text "JVBERi0xLjQKJdDUxdgKNiAwIG9iaiA8PAovTGVuZ3RoIDQ2NTIgICAgICAKL0Zp\nbH
}),

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.