0

当字段中有数据时,我得到了这个不错的 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
4

1 回答 1

0

您是否尝试过检查 @isbnlist 是否为空,如果是,则使用条件语句遍历所有元素或仅打印出短/空版本?

于 2011-06-15T12:51:25.607 回答