Home Forums General XMetaL Discussion Collapsing tags containging specific nodes Reply To: Collapsing tags containging specific nodes

Derek Read

Reply to: Collapsing tags containging specific nodes

Yes, you will need to convert to a selection and then set CollapsedContainerTags = true

One way (perhaps the best way) to do this would be to:
1. Create a Range object at the start of the script: var rng = ActiveDocument.Range;
2. Inside the for loop move rng to the node in question (before you remove the attribute): rng = nodelist.item(0).SelectContainerContents();
3. Then (also inside the for loop call): rng.CollapsedContainerTags = true;

Because this might cause a bunch of flickering as things get redrawn all over the place (this will be more noticeable on slower machines or larger docs) I would suppress any screen formatting updates until everything is done. Something like this:

[code]ActiveDocument.FormattingUpdating = false;
var nodelist = ActiveDocument.getNodesByXpath(“//*[@filter='filter5']”);
var nodecount = nodelist.length;
var rng = ActiveDocument.Range;
Application.Alert(“Fixing ” + nodecount + ” nodes…”);
for (i=0; i rng.SelectNodeContents(nodelist.item(0));
rng.CollapsedContainerTags = true;
nodelist.item(0).setAttribute(“filter”, “”);
ActiveDocument.FormattingUpdating = true;[/code]