[英]How to escape ' : ' using Ruby Gem Builder::XmlMarkup
I'm a ruby beginner and I would need to create an xml doc out of a csv to send it out to one my supplier.我是 ruby 初学者,我需要用 csv 创建一个 xml 文档,然后将其发送给我的供应商。 I found the gem Builder::XmlMarkup which is very useful.
我发现 gem Builder::XmlMarkup非常有用。 So far, my code looks like this:
到目前为止,我的代码如下所示:
require 'csv'
require 'builder'
File.open("testXML.xml","w"){|f|
builder = Builder::XmlMarkup.new(:target => f, :indent => 2)
builder.instruct! :xml, :standalone=>"yes"
csv = CSV.open('test.csv','rb',:headers=>true)
csv2 = CSV.open('test.csv','rb',:headers=>true)
builder.SupplierFile("xmlns:ns2"=>"http://impl.ws.tediber.com", "xmlns:ns1" =>
"http://obj.ws.tediber.com", "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance"){
builder.Credit{#, "upgnr"=>"12345"
builder.CreditList do |b|
csv2.each do |csv|
column = csv[0].split(",")
builder.Refund("id"=>"#{column[0]}") do |b|
builder.ns2:doCredit do |b|
b.ns2invoiceID("#{column[0]}")
builder.ns2:payment do |b|
b.ns1amount("#{column[2]}")
b.ns1currency("#{column[3]}")
b.ns1action("#{column[4]}")
b.ns1mode("#{column[5]}")
b.ns1contract("#{column[6]}")
end
b.ns2comment("#{column[7]}")
end
end
end
end
}
}
}
However I would need an xml format like that with <ns1:currency>
instead of <ns1currency>
但是我需要一个像
<ns1:currency>
而不是<ns1currency>
这样的 xml 格式
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SupplierFile xmlns:ns2="http://impl.ws.tediber.com" xmlns:ns1="http://obj.ws.tediber.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Credit>
<CreditList>
<Refund id="10006144909513">
<ns2:doCredit>
<ns2:invoiceID>10006144539513</ns2:transactionID>
<ns2:payment>
<ns1:amount>8900</ns1:amount>
<ns1:currency>978</ns1:currency>
<ns1:action>8900</ns1:action>
<ns1:mode>AAA</ns1:mode>
<ns1:contract>HZW_1202</ns1:contractNumber>
</ns2:payment>
<ns2:comment>Invoice_EUR_10006144909513</ns2:comment>
</ns2:doCredit>
</Refund>
</CreditList>
</CreditList>
</SupplierFile>
When I try the following code, it doesn't work due to ":"当我尝试以下代码时,由于“:”而不起作用
b.ns1:amount("#{column[2]}")
b.ns1:currency("#{column[3]}")
b.ns1:action("#{column[4]}")
b.ns1:mode("#{column[5]}")
b.ns1:contract("#{column[6]}")
I managed to get the xml but without the ":"我设法获得了 xml,但没有“:”
Is there a solution to add ":" into my code?是否有将“:”添加到我的代码中的解决方案?
According to its documentation , it looks like Builder::XmlMarkup
allows you to define tags with unusual symbols using the #tag!
根据其文档,看起来
Builder::XmlMarkup
允许您使用#tag!
定义带有不寻常符号的#tag!
method, which takes the tag name as a first argument.方法,它将标签名称作为第一个参数。
Please see if this works: b.tag!("ns1:amount", "#{column[2]}")
.请看看这是否有效:
b.tag!("ns1:amount", "#{column[2]}")
。
edit: there seem to also be a simpler way of creating XML namespaces.编辑:似乎还有一种更简单的方法来创建 XML 命名空间。 You also need to provide two arguments for it, but in this way:
您还需要为它提供两个参数,但以这种方式:
b.ns1(:amount, "#{column[2]}")
# or without parens:
b.ns1 :amount, "#{column[2]}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.