2012-01-16 43 views
0

我有這樣的Msbuild XML文件。如果元數據相同,如何合併爲單個屬性?

<?xml version="1.0" encoding="utf-8"?> 
    <Project xmlns="schemas.microsoft.com/developer/msbuild/2003"> 
     <ItemGroup> 
      <Notifications Include="[email protected]"/> 
     </ItemGroup> 
    <ItemGroup> 
     <Xcopy Include="..\..\..\Release\UtilitiesAssembly.dll"> 
      <Destination>"..\..\..\Mycomponent\depl\BOM\Folder1</Destination> 
     </Xcopy> 
     <Xcopy Include="..\..\..\Release\Core.assembly.dll"> 
      <Destination>"..\..\..\Mycomponent\depl\BOM\Folder1</Destination> 
     </Xcopy> 
     <Xcopy Include="..\..\..\Release\UIAssembly.dll"> 
      <Destination>"..\..\..\Anothercomponent\Folder1</Destination> 
     </Xcopy> 
     <Xcopy Include="..\..\..\Release\Core.assembly.dll"> 
      <Destination>"..\..\..\Anothercomponent\depl\BOM</Destination> 
     </Xcopy> 
    </Itemgroup> 
    </Project> 

其實我想組的XCopy的ItemGroup這樣

 <Xcopy Include="..\..\..\Release\UtilitiesAssembly.dll; 
        ..\..\..\Release\\Core.assembly.dll;"> 
     <Destination>"..\..\..\Mycomponent\depl\BOM\Folder1</Destination> 
    </Xcopy> 

    <Xcopy Include="..\..\..\Release\UIAssembly.dll;"> 
     <Destination>"..\..\..\Anothercomponent\Folder1</Destination> 
    </Xcopy> 

    <Xcopy Include="..\..\..\Release\Core.assembly.dll"> 
      <Destination>"..\..\..\Anothercomponent\depl\BOM</Destination> 
    </Xcopy> 

如何使用PowerShell或的MSBuild或其他一些機制

回答

2

這裏去實現它是一個例子,如何使用要做到這一點Group-Object cmdlet。它將通過Destination對xcopy元素進行分組,並將Include路徑合併爲分號分隔的字符串。輸出存儲在一個新的XML文檔中並保存到磁盤。這不會以編程方式更新原件。您可以將新的統一XML複製粘貼到原始XML上。

更新現在我看到了你的整個文件,我發現它有一個默認的命名空間,它導致了XPath的問題。有兩種方法可以解決這個問題。

  1. 使用namespace agnostic xpath
  2. 使用命名空間管理

下面是一個使用命名空間的XPath無關更新的例子。同樣爲了得到你正在尋找的縮進,我們需要在XML對象處理之後做一些文本文件處理。

$xml="Myinput.xml" 
$tempXmlFile = "C:\New.xml" 

$outXml = New-Object System.Xml.XmlDocument 
$outXml.AppendChild($outXml.CreateElement("root")) > $null 

$x = [xml] (Get-Content $xml) 
$x.SelectNodes("//*[local-name()='Xcopy']") | Group-Object Destination | % { 
    $includePaths = ($_.Group | Select-Object -ExpandProperty Include) -join ";" 
    $element = $outXml.CreateElement("Xcopy") 
    $element2 = $outXml.CreateElement("Destination") 
    $element2.InnerText = $_.Name 
    $element.AppendChild($element2) > $null 
    $element.SetAttribute("Include", $includePaths) 
    $outXml.DocumentElement.AppendChild($element) > $null 
} 
$outXml.Save($tempXmlFile) 

$data = Get-Content $tempXmlFile | % { 
    $search = 'Include="' 
    $index = $_.IndexOf('Include="') 
    if ($index -gt 0) { 
     $spaces = (' ' * ($index + $search.length)) 
    } 
    $_ -replace ';', ";`n${spaces}" 
} 
$data | Set-Content $tempXmlFile 
& notepad $tempXmlFile 

創建輸出:

<root> 
    <Xcopy Include="..\..\..\Release\UtilitiesAssembly.dll 
        ..\..\..\Release\Core.assembly.dll"> 
    <Destination>"..\..\..\Mycomponent\depl\BOM\Folder1</Destination> 
    </Xcopy> 
    <Xcopy Include="..\..\..\Release\UIAssembly.dll"> 
    <Destination>"..\..\..\Anothercomponent\Folder1</Destination> 
    </Xcopy> 
    <Xcopy Include="..\..\..\Release\Core.assembly.dll"> 
    <Destination>"..\..\..\Anothercomponent\depl\BOM</Destination> 
    </Xcopy> 
</root> 
+0

我得到以下問題哥們。無法將值「E:\ Powershellscripts \ Comp1.Targets」轉換爲鍵入「System.Xml.XmlDocument」。錯誤:「根級別的數據無效,行1,位置1」。 – Samselvaprabu 2012-01-16 11:31:21

+0

我的輸入文件如下所示:<?xml version =「1.0」encoding =「utf-8」?> < ComponentNotifications Include =「[email protected]」/> 「.. \ .. \ .. \ Mycomponent \ depl \ BOM \ Folder1 「.. \ .. \ .. \ Mycomponent \ depl \ BOM \ Folder1 Samselvaprabu 2012-01-16 11:42:00

+0

我編輯了我的問題以便更好地理解 – Samselvaprabu 2012-01-16 11:46:28

1

從的MSBuild 3.5開始,你可以使用ItemDefinitionGroup

<ItemDefinitionGroup> 
    <Xcopy> 
     <Destination>"..\..\..\Mycomponent\depl\BOM\Folder1</Destination> 
    </Xcopy> 
    </ItemDefinitionGroup> 
    <ItemGroup> 
    <Xcopy Include="..\..\..\Release\UtilitiesAssembly.dll" /> 
    <Xcopy Include="..\..\..\Release\Core.assembly.dll" /> 
    <Xcopy Include="..\..\..\Release\UIAssembly.dll;"> 
     <Destination>"..\..\..\Anothercomponent\Folder1</Destination>  
    </Xcopy> 
    </ItemGroup> 
+0

我認爲Itemdefinitiongroup會工作,如果它有所有的公共元數據。這是一個有用的信息。我進一步澄清了我的問題。它會適用於這種情況嗎? – Samselvaprabu 2012-01-16 12:34:45

+0

是的。看到我更新的答案。 – KMoraz 2012-01-16 13:51:48

相關問題