param( [string] $filepath = $(throw (Write-Host "Enter the path to manifest.xsf (do not include the filename)!")) ) #loads the InfoPath form manifest, prepares namespace navigators [System.Xml.XmlDocument]$manifestDocument = new-object System.Xml.XmlDocument; $manifestDocument.set_PreserveWhiteSpace( $true ); $manifestPath = resolve-path ($filepath + "manifest.xsf"); $manifestDocument.Load($manifestPath.path); $nsmgr = New-Object System.Xml.XmlNamespaceManager($manifestDocument.NameTable) $nsmgr.AddNamespace("xsf", "http://schemas.microsoft.com/office/infopath/2003/solutionDefinition") #find all the names of XSL views (which probably reference the variables) $viewNames = $manifestDocument.SelectNodes("//xsf:mainpane", $nsmgr); $views = new-object System.Collections.ArrayList; #loads all views into memory foreach ( $viewname in $viewNames) { [System.Xml.XmlDocument]$viewDoc = new-object System.Xml.XmlDocument; $viewDoc.set_PreserveWhiteSpace( $true ); $viewpath = resolve-path ($filepath + $viewname.GetAttribute("transform")); $viewDoc.Load($viewpath.path); $vnsmgr = New-Object System.Xml.XmlNamespaceManager($viewDoc.NameTable) $vnsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform") $vnsmgr.AddNamespace("xd", "http://schemas.microsoft.com/office/infopath/2003"); $n = $views.add($viewDoc) ; } #list of all potentially unused variables $nodes = $manifestDocument.SelectNodes("//xsf:xmlToEdit", $nsmgr); foreach ($node in $nodes) { $nodeName = $node.GetAttribute("name"); $select = ("//*[@xd:xmlToEdit='" + $nodeName + "']"); $currNodeUse = 0; #looks for every variable name in every XSL view foreach ($view in $views) { $nodeuses = $view.SelectNodes( $select, $vnsmgr); if ($nodeuses.count -gt 0) { $currNodeUse = 1; } } #the xmlToEdit might be referenced in the manifest.xsf file as well $select = ("//*[@xmlToEdit='" + $nodeName + "']"); $nodeuses = $manifestDocument.SelectNodes( $select, $vnsmgr); if ($nodeuses.count -gt 0) { $currNodeUse = 1; } #the variable was not referenced in XSL views as well as in manifest.xsf - we may delete it if ($currNodeUse -eq 0) { $parent = $node.parentNode; $temp = $parent.removeChild($node); } } #saving back to manifest.xsf Set-Content -Path ($manifestPath.path) -Value $manifestDocument.OuterXml -encoding UTF8