質問

I'm building an xml document using Nokogiri's XML Builder. I have a tag called

<InstAmt>

and I want it to be

<InstdAmt Ccy="Eur">

What do I need to add/configure to achieve this? I tried looking at the Nokogiri documentation but it only talks about attributes like class and id and that's not what I'm looking for.

The relevant portion of the builder looks like this:

            xml.PmtId{
              xml.EndToEndId "value"
              xml.InstdAmt "value"
              xml.ChrgBr "value"
            }

Any help at all would be greatly appreciated.

役に立ちましたか?

解決

If you pass a hash to the method creatig the tag it will be used to create attributes:

builder = Nokogiri::XML::Builder.new do |xml|
xml.PmtId{
  xml.EndToEndId "value"
  xml.InstdAmt "value", :Ccd => 'Eur'
  xml.ChrgBr "value"
}
end

puts builder.to_xml

produces:

<?xml version="1.0"?>
<PmtId>
  <EndToEndId>value</EndToEndId>
  <InstdAmt Ccd="Eur">value</InstdAmt>
  <ChrgBr>value</ChrgBr>
</PmtId>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top