Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tylerd/2226766 to your computer and use it in GitHub Desktop.
Save tylerd/2226766 to your computer and use it in GitHub Desktop.
Strip elements and attributes added to a TFS 2010 Build Process Template by the designer for a cleaner change history
#requires -version 2.0
[CmdletBinding()]
param(
[parameter(Mandatory=$true)]
[ValidateScript({ $_ | Test-Path -PathType Leaf })]
[string]
$Path
)
function New-XmlNamespaceManager ($XmlDocument, $DefaultNamespacePrefix) {
$NsMgr = New-Object -TypeName System.Xml.XmlNamespaceManager -ArgumentList $XmlDocument.NameTable
$DefaultNamespace = $XmlDocument.DocumentElement.GetAttribute('xmlns')
if ($DefaultNamespace -and $DefaultNamespacePrefix) {
$NsMgr.AddNamespace($DefaultNamespacePrefix, $DefaultNamespace)
}
$XmlDocument.DocumentElement.Attributes |
Where-Object { $_.Prefix -eq 'xmlns' } |
ForEach-Object {
$NsMgr.AddNamespace($_.LocalName, $_.Value)
}
return ,$NsMgr # unary comma wraps $NsMgr so it isn't unrolled
}
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$x = [xml](Get-Content -Path $Path)
$nsmgr = New-XmlNamespaceManager -XmlDocument $x
'http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation',
'http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation',
'clr-namespace:System.Activities.Debugger;assembly=System.Activities' |
ForEach-Object {
$Namespace = $_
$prefix = $nsmgr.LookupPrefix($namespace)
if ($prefix) {
$x.SelectNodes("//$($prefix):*", $nsmgr) |
ForEach-Object {
$_.ParentNode.RemoveChild($_) | Out-Null
}
$x.SelectNodes("//@*[namespace-uri()='$namespace']") |
ForEach-Object {
$_.OwnerElement.RemoveAttributeNode($_) | Out-Null
}
}
}
$x.Save($Path + '.clean')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment