我如何使用 nokogiri xml builder 来完成这项工作

how would i make this work with nokogiri xml builder

所以从一个特定的行开始,它似乎会导致它下面的行出错,抱歉这个问题似乎含糊不清,我无法清楚地表达我在这一行上做得很好。

builder = Nokogiri::XML::Builder.new do |xml|
  xml.send('document-id' => '{some-fake-id}', 'type' => 'documentType', 'iso-code' => 'BP', 'training' => 'false', 'send-type' => 'ctype', 'exempt' => 'false', 'cout' => 'false', 'void' => 'false', 'void-stored' => 'false', 'auto' => 'false', 'detail' => 'false', 'regular' => 'true', 'close' => 'false', 'vo' => 'false', 'mod' => 'false') 
  {
    xml.send(:"t-id",  $ID)
    xml.send(:"v-id",  "0")
    xml.date  $todayis.to_s
    xml.send(:"time"('dts' => "+05:00", today.year + "-" + today.month + "-2009:37:55.000+01:00"))
    xml.send(:"b-date", $todayis)
    xml.send(:"ticket-open-time", ('dts' => "+01:00")_ today.year + "-" + today.month + "-20T09:37:55.000+01:00")

等等

发生的错误是:

syntax error, unexpected '\n', expecting =>

要获得 <time dst="+05:00">2014-05-2009:38:48.946+01:00</time> 格式的字符串,您应该使用以下 nokogiri 调用:

xml.send :"time" , :"dts" => "+05:00" do |xml|
  xml << today.year + "-" + today.month + "-2009:37:55.000+01:00"
end

简而言之,nokogiri 使用的语法如下:

xml.send :"tag", :key1=>"value1", :key2=>"value2" do |var| 
  var.nestedtag
  var<<"content" 
end

呈现为:

<tag key1="value1" key2="value2"><nestedtag /> content </tag>

查看 docs 和来源以获取更多信息(确实有 )。

因此您的生成器可能看起来像这样(为了便于阅读,我引用了您的变量):

    builder = Nokogiri::XML::Builder.new do |xml|
      xml.root :"document-id" => '{some-fake-id}',
               :"type" => 'documentType',
               :"iso-code" => 'BP',
               :"training" => 'false',
               :"send-type" => 'ctype',
               :"exempt" => 'false',
               :"cout" => 'false',
               :"void" => 'false',
               :"void-stored" => 'false',
               :"auto" => 'false',
               :"detail" => 'false',
               :"regular" => 'true',
               :"close" => 'false',
               :"vo" => 'false',
               :"mod" => 'false' do |xml|


      xml.send :"t-id", "$ID"
      xml.send :"v-id",  "0"
      xml.date  "$todayis".to_s
      xml.send :"time" , :"dts" => "+05:00" do |xml|
        xml << "today.year" + "-" + "today.month" + "-2009:37:55.000+01:00"
      end
      xml.send :"b-date", "$todayis"
      xml.send :"time" , :"dts" => "+05:00" do |xml|
        xml << "today.year" + "-" + "today.month" + "-2009:37:55.000+01:00"
      end
      xml.send :"ticket-open-time", :'dts' => "+01:00" do |xml|
        xml << "today.year" + "-" + "today.month" + "-20T09:37:55.000+01:00"
      end
    end

将渲染到:

<?xml version="1.0"?>
<root document-id="{some-fake-id}" type="documentType" iso-code="BP" training="false" send-type="ctype" exempt="false" cout="false" void="false" void-stored="false" auto="false" detail="false" regular="true" close="false" vo="false" mod="false">
<t-id>$ID</t-id>
<v-id>0</v-id>
<date>$todayis</date>
<time dts="+05:00">today.year-today.month-2009:37:55.000+01:00</time>
<b-date>$todayis</b-date>
<time dts="+05:00">today.year-today.month-2009:37:55.000+01:00</time>
<ticket-open-time dts="+01:00">today.year-today.month-20T09:37:55.000+01:00</ticket-open-time>
</root>