Reputation: 287
I've been using Nokogiri to generate an XML file (specifically, a GraphML document using some yEd namespaces). An example of the type of file I'm generating:
<?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>
The document has a basic structure that doesn't change from document to document, and then a collection of <node>
and <edge>
tags specific to each document.
I've been able to successfully build this XML in a single method using Nokogiri::XML::Builder.
However, I now want to generate this file based on a different type of data as well -- most of the file will be unchanged; only my code that loops over data and generates the <node>
and <edge>
tags will change. So I'm effectively trying to create an XML template that I can call from multiple Ruby methods, which will then insert their own variants.
My thought was that I could save an XML file with everything except the <node>
and <edge>
tags. I would then have each different method use Nokogiri::XML::Builder to create a DocumentFragment of <node>
and <edge>
tags, open the template file, and insert the DocumentFragment in as a child of the <graph>
tag:
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
So this code largely does what I want it to. It successfully loads the template XML file at YED_TEMPLATE, it successfully creates a DocumentFragment, and it successfully inserts the DocumentFragment into the template XML...
...as long as I don't include the y
namespace tags (y:ShapeNode
, y:Geometry
, etc.). If I do, I get an ArgumentError (Namespace y has not been defined)
.
That makes sense to me, since the DocumentFragment isn't aware of all of the namespace definitions in the template XML file. But I have no idea how to actually provide the namespaces to a DocumentFragment, since it doesn't have a true root tag to add them to; the real root is in the template file.
Is there a way for me to pass namespace definitions into a Nokogiri::XML::Builder for a DocumentFragment? Alternatively, is there a better way for me to create a collection of nested tags with namespaces, and insert them into an existing XML document?
Upvotes: 1
Views: 420
Reputation: 102218
A nifty little tick if you want to create a builder instance scoped to a namespace is to use Nokogiri::XML::Builder.with(doc.root)
:
doc = Nokogiri::XML('<?xml version="1.0"?>
<root xmlns:y="foo"></root>')
builder = Nokogiri::XML::Builder.with(doc.root) do |xml|
xml['y'].Shapenode do |sn|
sn.Foo
sn.Bar
end
end
builder.to_xml
outputs:
<?xml version="1.0"?>
<root xmlns:y="foo">
<y:Shapenode>
<y:Foo/>
<y:Bar/>
</y:Shapenode>
</root>
Worth noting though is that it mutates doc
. If I where to do this I would use Nokogiri::XML::Builder.with(doc.root.dup)
which prevents it from mutating the arguments.
You can also just create builders with any arbitrary root with:
builder = Nokogiri::XML::Builder.new do |xml|
xml.root('xmlns:y' => 'bar') do
xml['y'].Shapenode
end
end
builder.doc.xpath('/*').children
will slice out the node set.
Upvotes: 3