Rails 3 XML 构建器:没有数据时不返回 XML 元素

发布于 2024-11-15 16:13:16 字数 2636 浏览 0 评论 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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海之角 2024-11-22 16:13:16

您是否尝试过检查 @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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文