Rails 3 XML 构建器:没有数据时不返回 XML 元素
当字段中有数据时,我得到这个漂亮的 XML:
<DescriptiveDetail>
<ProductComposition>00</ProductComposition>
<ProductForm>01</ProductForm>
<Measure>
<MeasureType>01</MeasureType>
<MeasureType>198</MeasureType>
<MeasureType>mm</MeasureType>
</Measure>
</DescriptiveDetail>
当没有数据时,我想得到这个:
<DescriptiveDetail>
<ProductComposition>00</ProductComposition>
</DescriptiveDetail>
但相反,我得到这个:
<DescriptiveDetail>
<ProductComposition></ProductComposition>
<ProductForm></ProductForm>
<Measure>
<MeasureType></MeasureType>
<MeasureType></MeasureType>
<MeasureType></MeasureType>
</Measure>
</DescriptiveDetail>
当没有数据时,如何返回没有 XML 元素?
(只是出于兴趣,如果您知道,如何返回自结束标记,例如
<Measure/>
)
这是我的 XML 构建器代码(完整的,相关位位于中间):
xml.instruct!(:xml, :encoding => "ISO-8859-1")
xml.ONIXMessage(:release=>"3.0") do
xml.Header do
xml.Sender
xml.SenderName
end
@isbnlist.each do |isbn|
xml.Product do
xml.RecordReference isbn.id
xml.NotificationType isbn.notificationtype
isbn.productcodes.each do |productcode|
xml.ProductIdentifier do
xml.ProductIDType productcode.idtype
xml.IDValue productcode.idvalue
end
end
xml.DescriptiveDetail do
xml.ProductComposition isbn.descriptivedetail_productcomposition_productcomposition
xml.ProductForm isbn.descriptivedetail_productcomposition_productform
isbn.measurements.each do |measurement|
xml.Measure do
xml.MeasureType measurement.descriptivedetail_measure_measuretype
xml.MeasureType measurement.descriptivedetail_measure_measurement
xml.MeasureType measurement.descriptivedetail_measure_measureunitcode
end
end
xml.TitleDetail do
xml.TitleType isbn.descriptivedetail_titledetail_titletype
xml.TitleElement do
xml.TitleElementLevel isbn.descriptivedetail_titledetail_titleelement_titleelementlevel
xml.TitlePrefix isbn.descriptivedetail_titledetail_titleelement_titleprefix
xml.TitleWithoutPrefix isbn.descriptivedetail_titledetail_titleelement_titlewithoutprefix
end
xml
end
end
end
end
end
I get this nice XML when there's data in the fields:
<DescriptiveDetail>
<ProductComposition>00</ProductComposition>
<ProductForm>01</ProductForm>
<Measure>
<MeasureType>01</MeasureType>
<MeasureType>198</MeasureType>
<MeasureType>mm</MeasureType>
</Measure>
</DescriptiveDetail>
And when there isn't data, I'd like to get this:
<DescriptiveDetail>
<ProductComposition>00</ProductComposition>
</DescriptiveDetail>
But instead I'm getting this:
<DescriptiveDetail>
<ProductComposition></ProductComposition>
<ProductForm></ProductForm>
<Measure>
<MeasureType></MeasureType>
<MeasureType></MeasureType>
<MeasureType></MeasureType>
</Measure>
</DescriptiveDetail>
How do I return no XML element when there's no data?
(And just out of interest, if you know, how do you return a self closing tag, e.g.
<Measure/>
)
Here's my XML builder code (in full, the relevant bit is in the middle):
xml.instruct!(:xml, :encoding => "ISO-8859-1")
xml.ONIXMessage(:release=>"3.0") do
xml.Header do
xml.Sender
xml.SenderName
end
@isbnlist.each do |isbn|
xml.Product do
xml.RecordReference isbn.id
xml.NotificationType isbn.notificationtype
isbn.productcodes.each do |productcode|
xml.ProductIdentifier do
xml.ProductIDType productcode.idtype
xml.IDValue productcode.idvalue
end
end
xml.DescriptiveDetail do
xml.ProductComposition isbn.descriptivedetail_productcomposition_productcomposition
xml.ProductForm isbn.descriptivedetail_productcomposition_productform
isbn.measurements.each do |measurement|
xml.Measure do
xml.MeasureType measurement.descriptivedetail_measure_measuretype
xml.MeasureType measurement.descriptivedetail_measure_measurement
xml.MeasureType measurement.descriptivedetail_measure_measureunitcode
end
end
xml.TitleDetail do
xml.TitleType isbn.descriptivedetail_titledetail_titletype
xml.TitleElement do
xml.TitleElementLevel isbn.descriptivedetail_titledetail_titleelement_titleelementlevel
xml.TitlePrefix isbn.descriptivedetail_titledetail_titleelement_titleprefix
xml.TitleWithoutPrefix isbn.descriptivedetail_titledetail_titleelement_titlewithoutprefix
end
xml
end
end
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过检查 @isbnlist 是否为空,如果是,则使用条件语句迭代所有元素或仅打印出短/空版本?
Have you tried just to check if @isbnlist is empty, and if so, use a conditional statement to either iterate through all the elements or just print out the short / empty version?