2017-04-06 63 views
0

以下XML:XML定製的CSV轉換使用PowerShell

<references bit="-1" pointname="1-ANN-01_CMD"> 
    <function drop-id="1" function-id="01A7" number="103" originating="1" title="HR-1 ANNUNCIATION-4" unit-id="1" /> 
</references> 
<references bit="-1" pointname="1-ANN-01_KB"> 
    <function drop-id="2" function-id="01A7" number="104" originating="0" title="HR-1 ANNUNCIATION-5" unit-id="1" /> 
    <function drop-id="3" function-id="01B7" number="105" originating="0" title="HR-1 ANNUNCIATION-DUPLICATE" unit-id="1" /> 
</references> 
<references bit="-1" pointname="test"> 
    <function drop-id="5" function-id="01A7" number="107" originating="1" title="HR-1 ANNUNCIATION-4" unit-id="1" /> 
    <function drop-id="6" function-id="01A8" number="108" originating="0" title="HR-1 ANNUNCIATION-5" unit-id="1" /> 
</references> 

PowerShell腳本,我現在用的就是

$MainXmlFile=([xml]"<root>$(gc BigShtFile.xml)</root>").root.references | % { 
foreach ($item in $_) { 
    $Obj = New-Object Object 


    Add-Member -InputObject $Obj -MemberType NoteProperty -Name PName -Value $_.pointname | ? {originating -eq "0"} 
    if ($_.function.originating -eq 0) { 
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name Drop -Value ($_.function | ? -Property originating -EQ 0 | Select -Property "drop-id" -Exp "drop-id") 
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name SHEET -Value ($_.function | ? -Property originating -EQ 0 | Select -Property number -Exp number) 
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name DWG -Value ($_.function | ? -Property originating -EQ 0 | Select -Property "function-id" -Exp "function-id") 
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name TITLE -Value ($_.function | ? -Property originating -EQ 0 | Select -Property title -Exp title) 
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name UNIT -Value ($_.function | ? -Property originating -EQ 0 | Select -Property "unit-id" -Exp "unit-id") 
    } else {                
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name Drop -Value $_.function."drop-id" 
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name SHEET -Value $_.function.number 
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name DWG -Value $_.function."function-id" 
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name TITLE -Value $_.function.title 
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name UNIT -Value $_.function."unit-id" 
      } 
    #Add-Member -InputObject $Obj -MemberType NoteProperty -Name BIT -Value $_.bit 
    $Obj 
    } 
} 
$MainXmlFile | Format-Table -AutoSize | Export-Csv Output.csv -NoTypeInformation 

和我得到的輸出是: - output of the powershell script

而期望的輸出應該包含4行,其中條目1-ANN-01-KB應該重複兩次,並且隨後的信息在單行中。 請幫忙。

+0

「而所需的輸出應包含4行」。不。根據你的樣本,它不應該。 –

+0

您將需要遍歷'function'標籤,就像循環查看'reference'標籤一樣。目前它正在處理第二個'reference'標籤。 – gms0ulman

+0

@DavidBrabant我的意思是期望的輸出是,如果你看看輸出中的第二行,就像(1-ANN-01_KB {2,3} {104,105})。而期望的是1-ANN-01_KB 2 104和下一行1-ANN-01_KB 3 105 – n00b

回答

2

我無法將其添加到您的代碼;我不習慣那種方法。請參閱下文,瞭解使用.索引的方法,就像您在代碼的某些部分中使用的那樣。 如果需要,您可以替換$obj散列表中分配值的部分。

$MainXmlFile=([xml]"<root>$(gc BigShtFile.xml)</root>").root.references | % { 
    foreach ($item in $_) { 

     if($item.function.originating -eq 0){ 

      # the second loop, which is missing from your original code 
      foreach ($func in $item.function){ 

       # casting as an array is essential otherwise you will get an error: 
       # Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'. 

       [array]$obj += New-Object psobject -Property @{ 
        PName = $item.pointname 
        Drop = $func."drop-id" 
        SHEET = $func.number 
        DWG = $func."function-id" 
        TITLE = $func.title 
        UNIT = $func."unit-id" 
       } 
      } 

     }else{ 

      <# I'm not sure what behaviour you're expect this else to perform. 
      # So I'm leaving here for completeness for now 
      foreach ($func in $item.function){ 
       [array]$obj += New-Object psobject -Property @{ 
        PName = $item.pointname 
        Drop = $func."drop-id" 
        SHEET = $func.number 
        DWG = $func."function-id" 
        TITLE = $func.title 
        UNIT = $func."unit-id" 
       } 
      } 
      #> 
     } 

    } 
} 

# Use Format-Table to output to host. Don't use it or any of the Format- functions with Export-CSV; they won't do what you want 
$obj | Format-Table -AutoSize 

# A PSObject will export without order by default. TO get the order you want, use Select and the required columns 
$obj | Select-Object PName, Drop, SHEET, DWG, TITLE, UNIT | Export-Csv Output.csv -NoTypeInformation 

輸出:(到屏幕上,無序CSV是有序的) Output