Inserting into XML tables at arbitrary points

Bill Humphries whump at apple.com
Tue Mar 19 13:02:04 PST 2002


Another question from the peanut gallery:

I'm working the problem of inserting a new XML table element into an 
existing XML table at any arbitrary position.

The verb xml.addTable(adrTable,elementName) appends a new element to the 
end of the table, so it addresses one corner case of the problem.

I see two ways to proceed:

In one, I break the interface to XML tables, which is a bad thing. That 
would be treating the table where I'm inserting as a plain-old Frontier 
table, ie someTable[5], changing the name by incrementing the serial 
numbers in the names of the sub tables:

000050000\tfoo -> 000060000\tfoo,
000060000\tbar -> 000070000\tbar,
etc.

Creating the new subtable for the new element for position 5, and renaming 
it to 000050000\tnewElement.

This breaks when Userland changes the interface to XML tables.

Another way to proceed would be to create a new table, and copy sub tables 
into it, adding the new element at the appropriate point, then copying the 
new table onto the old table. This one has the advantage of being written 
using Frontier's XML verbs.

local (newTable,insertionPoint = 3);
new (TableType, at newTable);
local (oldTable = xml.getAddress(@scratchpad.test,"page")^);
for i = 1 to sizeOf(oldTable) {
	newAdr = 
xml.addTable(@newTable,xml.convertToDisplayName(nameOf(oldTable[i])));
	table.assign(newAdr,oldTable[i]);
	if i == insertionPoint {
		adrOfNewElement = xml.addTable(@newTable,"newelement");
	};
};
table.assign(xml.getAddress(@scratchpad.test,"page"),newTable);

That's just a bare framework, you'd need to test to see if the insertion 
point is in bounds, handle the trivial case of adding to the end of the 
document, etc.

Anyway, I'm looking for feedback on this approach, and asking for yet 
another verb in the distribution. This one will be paired with the 
xml.getAddressFromXpath() verb I described yesterday to get to arbitrary 
location in the table using XPath, and create and destroy nodes at will.

----
Bill Humphries <whump at apple.com>
Webmaster, HR Systems
Apple Computer





More information about the Frontier-Users mailing list