-- TshwaneLex / TshwaneTerm/tlTerm sample Lua script. -- http://tshwanedje.com/ -- -- This script moves all occurrences of an attribute from an element to an -- attribute with the same name on its parent, if the element is a child of -- the specified parent element. All given elements and attributes must -- exist in the DTD already. -- Move instances of attribute ELEMENTNAME::ATTRNAME to parent element PARENTNAME local ELEMENTNAME = "TE"; local PARENTNAME = "Sense"; local ATTRNAME = "TE"; -- Make sure we have a document open local Doc = tApp():GetCurrentDoc(); if Doc==nil then return "No document open"; end -- Find element/attribute descriptors in DTD local ELEMPARENT = Doc:GetDTD():FindElementByName(PARENTNAME); if ELEMPARENT==nil then return "Element not found"; end local ELEM = Doc:GetDTD():FindElementByName(ELEMENTNAME); if ELEM==nil then return "Element not found"; end local ATTRP = ELEMPARENT:FindAttributeByName(ATTRNAME); if ATTRP==nil then return "Attribute not found"; end local ATTR = ELEM:FindAttributeByName(ATTRNAME); if ATTR==nil then return "Attribute not found"; end function MoveAttribute(Node) local NUM = Node:GetNumChildren(); for i=0,NUM-1,1 do local Child = Node:GetChild(i) -- Recurse MoveAttribute(Child); -- Check if we have a parent->child occurrence of the given element types if Child:GetElement():GetID()==ELEM:GetID() then if Child:GetParent():GetElement():GetID()==ELEMPARENT:GetID() then local Value = Child:GetAttributeDisplayAsString(ATTR); Child:GetParent():SetAttributeDisplayByString(ATTRP,Value); Child:SetAttributeDisplayByString(ATTR,""); end end end end -- Iterate through all 'sections'/languages, and for each section, through all entries. local i, j; for i=0,Doc:GetDictionary():GetNumLanguages()-1,1 do local Section = Doc:GetDictionary():GetLanguage(i); for j=0,Section:GetNumChildren()-1,1 do local Entry = Section:GetChild(j); MoveAttribute(Entry); end end return "Completed OK";