Ruby 如果已经使用了xmlns属性,Nokogiri生成器将省略该属性
我正在尝试构建一个xml电子表格,其中包含将在excel中打开的样式 这是我的代码:Ruby 如果已经使用了xmlns属性,Nokogiri生成器将省略该属性,ruby,xml,ruby-on-rails-3,excel,nokogiri,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
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的builder,现在速度太慢了),现在我切换到Nokogiri,它不再工作了。基本上,工作表选项中的“”xmlns“=>”urn:schemas microsoft com:office:excel“
标记get被忽略,不会添加到文档中。以下是实际结果:
<?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="&LLeft side&CCenter&R&D &T"/>
<Footer x:Data="&CPage: &P / &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>
20
90
-4
-4
125
0
3.
8.
4.
假的
假的
如果我在这一行xml.WorksheetOptions“xmlns”=>“urn:schemas microsoft com:office:excel”中写入任何其他内容作为xmlns
属性,它将正常工作并正确添加到文档中
这是错误的,显然,如果缺少该属性,excel将无法正确设置页面。这对Nokogiri来说是正确的行为吗
如果是,是否有其他方法使excel对文档应用正确的页面布局
这发生在另一个我没有包含在示例中的标记上,否则它会太长。这是另一个:xml.DocumentProperties(“xmlns”=>“urn:schemas microsoft com:office:office”)确实
我不确定构建器界面,但您始终可以通过使用add_namespace
和nil
命名空间将其直接添加到节点:
node.add_namespace(nil, "urn:schemas-microsoft-com:office:excel")
有关详细信息,请参阅上的文档