2016-09-27 4 views
2

Я пытаюсь отображать узлы XML-файла в таблицу, моя проблема в том, что каждый столбец отображается как массив, а не список. Я не уверен, как преобразовать узлы в список ..Преобразование узлов в xml из массива в список Powershell

Мои результаты должны быть:

CCI, Description 
CCI-000001, BLAH BLAH BLAH BLAH BLAH BLAH 
CCI-000002, BLAH BLAH BLAH BLAH BLAH BLAH 
CCI-003391, BLAH BLAH BLAH BLAH BLAH BLAH 

Вместо его:

CCI           Description   
{CCI-000001, CCI-000002, CCI-003391}   {BLAH BLAH BLAH BLAH BLAH ...} 

Мой файл XML является

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type='text/xsl' href='cci2html.xsl'?> 
<cci_list xmlns="http://iase.disa.mil/cci"> 
<metadata> 
    <version>2013-10-08</version> 
    <publishdate>2013-10-08</publishdate> 
</metadata> 
<cci_items> 
    <cci_item id="CCI-000001"> 
    <status>draft</status> 
    <publishdate>2013-01-01</publishdate> 
    <contributor>DISA FSO</contributor> 
    <definition>BLAH BLAH BLAH BLAH BLAH BLAH</definition> 
    <type>policy</type> 
    <references> 
    <reference creator="NIST" title="NIST SP 800-53" version="3" location="NIST" index="AC-1 a" /> 
    <reference creator="NIST" title="NIST SP 800-53A" version="1" location="NIST" index="AC-1.1 (i&amp;ii)" /> 
    <reference creator="NIST" title="NIST SP 800-53 Revision 4" version="4" location="NIST" index="AC-1 a 1" /> 
    </references> 
</cci_item> 
<cci_item id="CCI-000002"> 
    <status>draft</status> 
    <publishdate>2013-01-01</publishdate> 
    <contributor>DISA FSO</contributor> 
    <definition>BLAH BLAH BLAH BLAH BLAH </definition> 
    <type>policy</type> 
    <references> 
    <reference creator="NIST" title="NIST SP 800-53" version="3" location="NIST" index="AC-1 a" /> 
    <reference creator="NIST" title="NIST SP 800-53A" version="1" location="NIST" index="AC-1.1 (iii)" /> 
    <reference creator="NIST" title="NIST SP 800-53 Revision 4" version="4" location="NIST" index="AC-1 a 1" /> 
    </references> 
</cci_item> 
<cci_item id="CCI-003391"> 
    <status>draft</status> 
    <publishdate>2013-01-01</publishdate> 
    <contributor>DISA FSO</contributor> 
    <definition>BLAH BLAH BLAH BLAH BLAH BLAH</definition> 
    <type>policy</type> 
    <references> 
    <reference creator="NIST" title="NIST SP 800-53 Revision 4" version="4" location="NIST" index="SA-19 (3)" /> 
    </references> 
</cci_item> 
</cci_items> 
</cci_list> 

Мой код:

[xml]$data = GC "file.xml" 

$CCInum = $data.cci_list.cci_items.cci_item.id 
$CCIdescription = $data.cci_list.cci_items.cci_item.definition 

$CCItable = New-Object PSobject -Property @{ 
    CCI = $CCInum 
    Description = $CCIdescription 
    } 

$CCItable | select CCI, Description 

Я делаю это так, потому что я в конечном итоге экспортирую результаты в файл csv.

ответ

2

В настоящее время вы создаете два отдельных массива. Вместо этого извлеките один массив из xml, чтобы каждый элемент имел оба поля, а затем извлекал их как свойства через Select-Object:

$xml = [xml](gc 'file.xml') 
$table = $xml.cci_list.cci_items.cci_item | 
     Select @{N='CCI'; E={$_.id}}, @{N='Description'; E={$_.definition}} 
$table | Export-Csv file.csv -NoTypeInformation 
Смежные вопросы