如果已经使用过,Nokogiri Builder将省略xmlns属性

时间:2013-09-13 08:57:32

标签: ruby xml ruby-on-rails-3 excel nokogiri

我正在尝试构建一个xml电子表格,其中包含将在Excel中打开的样式。

这是我的代码:

res = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
  xml.Workbook 'xmlns' => "urn:schemas-microsoft-com:office:spreadsheet", 
                'xmlns:o'    => "urn:schemas-microsoft-com:office:office",
                'xmlns:x'    => "urn:schemas-microsoft-com:office:excel",    
                'xmlns:html' => "http://www.w3.org/TR/REC-html40",
                'xmlns:ss'   => "urn:schemas-microsoft-com:office:spreadsheet" do

    xml.WorksheetOptions "xmlns" => "urn:schemas-microsoft-com:office:excel" do
      xml.PageSetup do
        xml.Layout "x:Orientation" => "Landscape"
        xml.Header "x:Data" => "&LLeft side&CCenter&R&D &T"
        xml.Footer "x:Data" => "&CPage: &P / &N"
      end

      xml.Unsynced
      xml.FitToPage

      xml.Print do
        xml.FitHeight 20
        xml.ValidPrinterInfo
        xml.Scale 90
        xml.HorizontalResolution -4
        xml.VerticalResolution -4
      end

      xml.Zoom 125
      xml.PageLayoutZoom 0
      xml.Selected
      xml.Panes do
        xml.Pane do
          xml.Number 3
          xml.ActiveRow 8
          xml.ActiveCol 4
        end
      end
      xml.ProtectObjects "False"
      xml.ProtectScenarios "False"

      xml.AllowFormatCells
      xml.AllowSizeCols
      xml.AllowSizeRows
      xml.AllowSort
      xml.AllowFilter
      xml.AllowUsePivotTables
    end
  end
end.to_xml
puts res

我多年来一直把它作为一个工作模板(我之前使用的是bunlder的构建器,现在它太慢了)现在我切换到Nokogiri它已经不再工作了。基本上这样:"xmlns" => "urn:schemas-microsoft-com:office:excel"标记中的WorksheetOptions被忽略,并且未添加到文档中。这是实际结果:

<?xml version="1.0" encoding="UTF-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
  <WorksheetOptions>
    <PageSetup>
      <Layout x:Orientation="Landscape"/>
      <Header x:Data="&amp;LLeft side&amp;CCenter&amp;R&amp;D &amp;T"/>
      <Footer x:Data="&amp;CPage: &amp;P / &amp;N"/>
    </PageSetup>
    <Unsynced/>
    <FitToPage/>
    <Print>
      <FitHeight>20</FitHeight>
      <ValidPrinterInfo/>
      <Scale>90</Scale>
      <HorizontalResolution>-4</HorizontalResolution>
      <VerticalResolution>-4</VerticalResolution>
    </Print>
    <Zoom>125</Zoom>
    <PageLayoutZoom>0</PageLayoutZoom>
    <Selected/>
    <Panes>
      <Pane>
        <Number>3</Number>
        <ActiveRow>8</ActiveRow>
        <ActiveCol>4</ActiveCol>
      </Pane>
    </Panes>
    <ProtectObjects>False</ProtectObjects>
    <ProtectScenarios>False</ProtectScenarios>
    <AllowFormatCells/>
    <AllowSizeCols/>
    <AllowSizeRows/>
    <AllowSort/>
    <AllowFilter/>
    <AllowUsePivotTables/>
  </WorksheetOptions>
</Workbook>

如果我在此行xmlns上写下xml.WorksheetOptions "xmlns" => "urn:schemas-microsoft-com:office:excel" do属性的其他内容,它将正常工作并正确添加到文档中。

这是错误的,显然excel如果缺少该属性则不会正确设置页面。这对Nokogiri来说是正确的行为吗?

如果是,是否还有其他方法可以将excel应用于正确的页面布局?

这与我未在示例中包含的另一个标记一起发生,否则它会太长。这是另一个:xml.DocumentProperties("xmlns" => "urn:schemas-microsoft-com:office:office") do

1 个答案:

答案 0 :(得分:0)

我不确定构建器界面,但您始终可以使用add_namespace nil命名空间将其直接添加到节点:

node.add_namespace(nil, "urn:schemas-microsoft-com:office:excel")

有关详细信息,请参阅Node#add_namespace上的文档。

相关问题