tags:

views:

7

answers:

0

Hi everyone,

We have this method in our Ruby project. It accepts an XML object (which consists of three different "trees") and is supposed to copy X elements from the end. For some reason, what it does is move those elements -- and then the original XML object (which we passed to it) gets cut off.

We tried .clone but it doesn't copy the attributes.

I'm not sure if the description is clear enough; I'd be happy to clarify. Here's the code:

def copy_elements_from_end(xml_object,number_from_end)
# This function copies the last X days (date, weight, and average) from one XML object (the "source") to another XML object (the "destination") for creating a "recent history" file

#SIDE EFFECT: This function causes xml_object (the source object) to be lopped off by number_from_end elements (meaning, it gets CUT!)

destination_xml = create_xml_structure()
last_xid = get_most_recent_xid(xml_object)
first_xid = last_xid-number_from_end
for i in first_xid..last_xid

 #Take the source elements
 source_date_element = xml_object.elements["chart/series/value[@xid="+i.to_s+"]"]
 source_graph1_element = xml_object.elements["chart/graphs/graph[@gid='1']/value[@xid="+i.to_s+"]"]
 source_graph2_element = xml_object.elements["chart/graphs/graph[@gid='2']/value[@xid="+i.to_s+"]"]

 #Create names for elements in destination XML
 dest_date_series = destination_xml.elements["chart/series/"]
 dest_graph1 = destination_xml.elements["chart/graphs/graph[@gid=1]/"]
 dest_graph2 = destination_xml.elements["chart/graphs/graph[@gid=2]/"]

 #Add the elements from the source into the destination in the proper places
 dest_date_series.add_element source_date_element
 dest_graph1.add_element source_graph1_element
 dest_graph2.add_element source_graph2_element
 end
 return destination_xml
 end