我一直在使用Nokogiri来生成XML文件(特别是使用某些yEd名称空间的GraphML文档)。我正在生成的文件类型的示例:
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:java="http://www.yworks.com/xml/yfiles-common/1.0/java" xmlns:sys="http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0" xmlns:x="http://www.yworks.com/xml/yfiles-common/markup/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xmlns:yed="http://www.yworks.com/xml/yed/3" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key attr.name="Description" attr.type="string" for="graph" id="d0"/>
<key attr.name="description" attr.type="string" for="node" id="d5"/>
<key for="node" id="d6" yfiles.type="nodegraphics"/>
<key for="graphml" id="d7" yfiles.type="resources"/>
<key attr.name="description" attr.type="string" for="edge" id="d9"/>
<key for="edge" id="d10" yfiles.type="edgegraphics"/>
<graph edgedefault="directed" id="G">
<data key="d0"/>
<node id="n10">
<data key="d5"/>
<data key="d6">
<y:ShapeNode>
<y:Geometry width="42.42640687119285" height="42.42640687119285" x="30.0" y="0.0"/>
<y:Fill color="#DBDCDF" transparent="false"/>
<y:BorderStyle color="#303236" raised="false" type="line" width="1.0"/>
<y:NodeLabel alignment="center" fontFamily="Source Sans Pro Semibold" fontSize="17" fontStyle="plain" verticalTextPosition="bottom" horizontalTextPosition="center">DAY</y:NodeLabel>
<y:Shape type="ellipse"/>
</y:ShapeNode>
</data>
</node>
<node id="n11">
<data key="d5"/>
<data key="d6">
<y:ShapeNode>
<y:Geometry width="30.0" height="30.0" x="-14.999999999999993" y="25.980762113533164"/>
<y:Fill color="#DBDCDF" transparent="false"/>
<y:BorderStyle color="#303236" raised="false" type="line" width="1.0"/>
<y:NodeLabel alignment="center" fontFamily="Source Sans Pro Semibold" fontSize="12" fontStyle="plain" verticalTextPosition="bottom" horizontalTextPosition="center">STL</y:NodeLabel>
<y:Shape type="ellipse"/>
</y:ShapeNode>
</data>
</node>
<node id="n12">
<data key="d5"/>
<data key="d6">
<y:ShapeNode>
<y:Geometry width="42.42640687119285" height="42.42640687119285" x="-15.000000000000014" y="-25.980762113533153"/>
<y:Fill color="#DBDCDF" transparent="false"/>
<y:BorderStyle color="#303236" raised="false" type="line" width="1.0"/>
<y:NodeLabel alignment="center" fontFamily="Source Sans Pro Semibold" fontSize="17" fontStyle="plain" verticalTextPosition="bottom" horizontalTextPosition="center">DFW</y:NodeLabel>
<y:Shape type="ellipse"/>
</y:ShapeNode>
</data>
</node>
<edge id="e0" source="n10" target="n11">
<data key="d9"/>
<data key="d10">
<y:PolyLineEdge>
<y:LineStyle width="2.0" color="#ff99cc"/>
<y:Arrows source="none" target="standard"/>
<y:EdgeLabel visible="false">American Airlines</y:EdgeLabel>
</y:PolyLineEdge>
</data>
</edge>
<edge id="e1" source="n11" target="n12">
<data key="d9"/>
<data key="d10">
<y:PolyLineEdge>
<y:LineStyle width="2.0" color="#ff99cc"/>
<y:Arrows source="none" target="standard"/>
<y:EdgeLabel visible="false">American Airlines</y:EdgeLabel>
</y:PolyLineEdge>
</data>
</edge>
<edge id="e2" source="n12" target="n10">
<data key="d9"/>
<data key="d10">
<y:PolyLineEdge>
<y:LineStyle width="2.0" color="#ff99cc"/>
<y:Arrows source="none" target="standard"/>
<y:EdgeLabel visible="false">American Airlines</y:EdgeLabel>
</y:PolyLineEdge>
</data>
</edge>
</graph>
<data key="d7">
<y:Resources/>
</data>
</graphml>
文档的基本结构不会随文档的变化而变化,然后是每个文档特有的<node>
和<edge>
标签的集合。
我已经能够使用Nokogiri :: XML :: Builder以一种方法成功构建此XML。
但是,我现在也想根据不同类型的数据生成此文件-大多数文件将保持不变;只有我的遍历数据并生成<node>
和<edge>
标签的代码会更改。因此,我正在有效地尝试创建一个XML模板,可以从多个Ruby方法调用该模板,然后再插入其自己的变体。
我的想法是,我可以保存带有<node>
和<edge>
标记之外的所有内容的XML文件。然后,我将让每个不同的方法使用Nokogiri :: XML :: Builder创建<node>
和<edge>
标签的DocumentFragment,打开模板文件,并将DocumentFragment作为<graph>
标签的子元素插入:] >
YED_TEMPLATE = "#{Rails.root}/app/views/xml_templates/flights.yed.graphml" def self.yed_from_string(flight_string) airports = flight_string.split(/[,-]/).tally output = File.open(YED_TEMPLATE) {|f| Nokogiri::XML(f)} nodes = Nokogiri::XML::DocumentFragment.parse("") Nokogiri::XML::Builder.with(nodes) do |xml| airports.map{|airport, visits| yed_airport_node(xml, airport, airport, visits)} end # Write similar code for edges output.at("graph").add_child(nodes) return output.to_xml end private def self.yed_airport_node(xml, id, text, visits) xml.node(id: "n#{id}") do xml.data(key: "d5") xml.data(key: "d6") do xml[:y].ShapeNode do xml[:y].Geometry(circle_size(visits)) xml[:y].Fill(color: BASE_STYLES[:node_color_fill], transparent: false) xml[:y].BorderStyle(color: BASE_STYLES[:node_color_border], raised: false, type: "line", width: BASE_STYLES[:node_width_border]) xml[:y].NodeLabel(text, **font(visits)) xml[:y].Shape(type: "ellipse") end end end return nil end # Write similar method for edges
所以这段代码在很大程度上满足了我的期望。它成功地在YED_TEMPLATE上加载了模板XML文件,成功创建了一个DocumentFragment,并且成功地将DocumentFragment插入到模板XML中...
...,只要不包含y
名称空间标签(y:ShapeNode
,y:Geometry
等)。如果这样做,我会得到一个ArgumentError (Namespace y has not been defined)
。
这对我来说很有意义,因为DocumentFragment无法识别模板XML文件中的所有名称空间定义。但是我不知道如何实际为DocumentFragment提供名称空间,因为它没有真正的根标记可将它们添加到其中。真正的根在模板文件中。
我是否有办法将名称空间定义传递给DocumentFragment的Nokogiri :: XML :: Builder?另外,对于我来说,是否有更好的方法来创建带有名称空间的嵌套标签的集合,并将其插入到现有的XML文档中?
我一直在使用Nokogiri来生成XML文件(特别是使用某些yEd名称空间的GraphML文档)。我正在生成的文件类型的示例: