2012-01-16 5 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> 

実際に私がここで

答えて

2

をPowerShellのか、MSBuildのを使用して、または他のメカニズムによって、それを達成するためには使用してこれを行う方法の例でどのようにこの

 <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> 

のようなグループXCopyのItemgroupしたいと思いますGroup-Objectコマンドレットxcopy要素をグループ化してDestinationとし、Includeのパスをセミコロンで区切った文字列に結合します。出力は新しいXMLドキュメントに格納され、ディスクに保存されます。これは、元のプログラムでは更新されません。新しい統合XMLを元のXMLに貼り付けるだけでコピーできます。

更新ファイル全体が表示されているので、XPathに問題を引き起こすデフォルトの名前空間があります。この問題を回避するには2つの方法があります。

  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

+0

入力ファイルは以下のようになります。 "<?xml version =" 1.0 "encoding =" utf-8 "?> < ComponentNotifications Include = "[email protected]" /> ".. \ .. \ .. \ Mycomponent \ depl \ BOM \ Folder1 ".. \ .. \ .. \ Mycomponent \ depl \ BOM \ Folder1 Samselvaprabu

+0

私は理解のために私の質問を編集しました – Samselvaprabu

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

+0

はい。私の更新された答えを見てください。 – KMoraz

関連する問題