2017-04-06 2 views
0

für folgende xml:xml, um benutzerdefinierte CSV-Konvertierung mithilfe von 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> 

des Powershell-Skript, das ich verwende ist

$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 

und der Ausgang, i bin immer: - output of the powershell script

während die gewünschte Ausgabe 4 Zeilen enthalten sollte, in denen der Eintrag 1-ANN-01-KB zweimal mit den nachfolgenden Informationen in einer Zeile wiederholt werden sollte. Bitte helfen.

+0

"während der gewünschte Ausgang 4 Zeilen enthalten sollte". Nee. Laut Ihrer Probe sollte es nicht. –

+0

Sie müssen die "function" -Tags in ähnlicher Weise durchlaufen, wie Sie die "reference" -Tags durchlaufen. Im Moment behandelt es das 2. 'Referenz'-Tag auf einmal. – gms0ulman

+0

@DavidBrabant was ich meine von der gewünschten Ausgabe ist, wenn Sie die zweite Zeile in der Ausgabe betrachten, ist es wie (1-ANN-01_KB {2,3} {104,105}). während was gewünscht ist 1-ANN-01_KB 2 104 und in der nächsten Zeile 1-ANN-01_KB 3 105 – n00b

Antwort

2

Ich konnte dies nicht zu Ihrem Code hinzufügen; Ich bin diesen Ansatz nicht gewohnt. Im Folgenden finden Sie einen Ansatz mit . Indizierung wie in einigen Teilen Ihres Codes. Sie können die Teile innerhalb der $obj Hashtabelle, in der die Werte zugewiesen sind, ersetzen, wenn dies erforderlich ist.

$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 

Output: (. Zum Bildschirm, ungeordneter CSV ist bestellt) Output

Verwandte Themen