General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › Insert DITA XML fragment into the DOM
-
pwnolan October 21, 2013 at 11:12 pm
Insert DITA XML fragment into the DOM
October 21, 2013 at 11:12 pmParticipants 3Replies 4Last Activity 9 years, 3 months agoHello,
I'm trying to solve the following problem but I'm hitting some brick walls and could use some help. Here's what I'm trying to do:
I have a macro that inserts a complex HTML table as a fragment into a DITA document using Selection.PasteStringWithInterpret. This is working as expected. My next requirement is for a macro to parse the document and add a new element with the number of the table based on it's position in the document. To do this I iterate over an array of tables retuned from ActiveDocument.getNodesByXPath(“//table”) and append the new element using insertBefore(). This macro works perfectly for tables that exist in the document when it is first opened. The issue I am having is that when I use Selection.PasteStringWithInterpret to insert a new table fragment into the document, the macro to update the table number does not see the new table in the DOM. I'm assuming this is because the paste function does not actually update the DOM.
I've tried a couple of things to get this to work. 1) I called ActiveDocument.Reload after the paste assuming that it would reload the ActiveDocument with the new table but this fails with an error 'Could not open _temp.xml'. 2) I tried to append the XML table fragment into the DOM using several iterations of the following code but I keep getting a 'Type mismatch' error. e.g.
[code]
var doc=new ActiveXObject('Microsoft.XMLDOM');
doc.async='false';
doc.loadXML(tableString);
var node = Selection.ContainerNode;
node.appendChild(doc.firstChild); // i also tried doc.documentElement[/code]
I would really appreciate any help I can get with this. Thanks in advance.
Regards.
Derek Read October 21, 2013 at 11:25 pm
Reply to: Insert DITA XML fragment into the DOM
October 21, 2013 at 11:25 pmIs the code that inserts the table and the code that does the counting in the same macro?
pwnolan October 22, 2013 at 9:16 am
Reply to: Insert DITA XML fragment into the DOM
October 22, 2013 at 9:16 amHi Derek,
No they are two separate macros. I have a toolbar that lets the user insert several different predefined table structures plus the option to add the table number elements.
Thanks
Derek Read October 22, 2013 at 9:15 pm
Reply to: Insert DITA XML fragment into the DOM
October 22, 2013 at 9:15 pmOnce the table (or any set of elements) is successfully inserted it should be in the DOM, so this is a little odd.
However, you might want to try calling formatGraphicTable() — see the Programmers Guide for details — to see if that helps. I suspect it will not, given that it really should only affect the rendering of what is already in the DOM.Derek Read October 22, 2013 at 9:42 pm
Reply to: Insert DITA XML fragment into the DOM
October 22, 2013 at 9:42 pmOK, I've just programmatically inserted a table and I have no trouble calling getNodesByXPath inside the same macro and in a second macro. It tells me my table is there, it also doesn't matter if it is currently rendered as a table or not.
I'm testing with the current release of XMetaL Author Enterprise which is 8.0.1.041. I don't think we've worked much on this area of the product recently, but perhaps your specific release has some limitations.
Here are two of the simplest possible tests I can think of.
For a DITA document this one will make the document invalid and the table cannot even be rendered as one due to all the missing bits:
[code=test 1]// XMetaL Script Language JSCRIPT:
if(Selection.FindInsertLocation(“table”)) {
Selection.InsertElement(“table”);
var tables = ActiveDocument.getNodesByXPath(“//table”);
Application.Alert(“# of tables: ” + tables.length);
}
else {
Application.Alert(“Unable to insert a table element.”);
}[/code]This one inserts an entire valid table from a string:
[code=test 2]// XMetaL Script Language JSCRIPT:
if(Selection.FindInsertLocation(“table”)) {
Selection.PasteStringWithInterpret('');
var tables = ActiveDocument.getNodesByXPath(“//table”);
Application.Alert(“# of tables: ” + tables.length);
}
else {
Application.Alert(“Unable to insert a table element.”);
}[/code]In both cases the Alert should say that there is at least one table after insertion (more if the document already contains some).
-
AuthorPosts
- You must be logged in to reply to this topic.