--
You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nokogiri-talk/85da2c9f-92fe-4b59-a97e-301a39044f2an%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nokogiri-talk/d0ffe363-c448-442d-8481-c1a43d6095f2n%40googlegroups.com.
#! /usr/bin/env ruby require "nokogiri" xml = <<EOF <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <name>Document.kml</name> <Placemark> <name>Waypoint</name> <Point> <coordinates>-122.371,37.816,0</coordinates> </Point> </Placemark> <Placemark> <name>Track</name> <LineString> <coordinates>-0.376291,43.296237,199.75 -0.376299,43.296237,199.75</coordinates> </LineString> </Placemark> </Document> </kml> EOF doc = Nokogiri::XML(xml) # 1. insert/replace the document's name. # # First, let's find the name node. # For explanation of why the "xmlns" is needed, check out: # > https://nokogiri.org/tutorials/searching_a_xml_html_document.html#namespaces # name_node = doc.at_xpath("/xmlns:kml/xmlns:Document/xmlns:name") # # You could also use a CSS query which will (mostly) ignore namespaces. This is exactly the same search. # name_node = doc.at_css("kml > Document > name") # # Modify the contents of the <name/> node # name_node.content = "New Document Name" # 2. a. find all Placemark blocks that contain a LineString # # I can think of two ways to do this. The first way is to search for # LineString nodes within a Placemark node, and then get those nodes' parents: placemarks_with_linestring = doc.xpath("//xmlns:Placemark/xmlns:LineString").map(&:parent) # This first approach would work with CSS as well: placemarks_with_linestring = doc.css("Placemark > LineString").map(&:parent) # The second approach is to just use an XPath query to express that # you want Placemarks that contain a LineString: placemarks_with_linestring = doc.xpath("//xmlns:Placemark[xmlns:LineString]") # Then you can add a new child node to that Placemark: placemarks_with_linestring.each do |placemark| # the string passed into add_child is parsed just like any other XML fragment placemark.add_child "<some>blah</some>" end # The end result: puts doc.to_xml # >> <?xml version="1.0" encoding="UTF-8"?> # >> <kml xmlns="http://www.opengis.net/kml/2.2"> # >> <Document> # >> <name>New Document Name</name> # >> <Placemark> # >> <name>Waypoint</name> # >> <Point> # >> <coordinates>-122.371,37.816,0</coordinates> # >> </Point> # >> </Placemark> # >> <Placemark> # >> <name>Track</name> # >> <LineString> # >> <coordinates>-0.376291,43.296237,199.75 -0.376299,43.296237,199.75</coordinates> # >> </LineString> # >> <some>blah</some></Placemark> # >> </Document> # >> </kml> # 3. Write this to a file # # Use normal Ruby idioms for opening a file and writing to it, and use #to_xml to serialize the doc: File.open("output.kml", "w") do |file| file.write doc.to_xml end ```
To view this discussion on the web visit https://groups.google.com/d/msgid/nokogiri-talk/1975eef0-e68c-440f-bae9-bc4f29c81476n%40googlegroups.com.
doc = Nokogiri::XML(xml) do |config|
config.noblanks
end
--
You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nokogiri-talk/b12e3a5a-2d6e-447e-bff5-4eb0cd02c933n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nokogiri-talk/e176c485-7315-4b77-b6e2-cd9f42d1782cn%40googlegroups.com.