简体   繁体   中英

Convert nodes in xml from array to list Powershell

I am trying to display nodes of an xml file into a table my issue is that each column shows as an array instead of list. I am not sure how to convert the nodes into a list..

My results should be :

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 

Instead its:

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

My xml file is

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

My code is

[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

I am doing it this way because I will eventually export my results to a csv file.

Currently you're creating two separate arrays. Instead, extract one array from the xml so that each element has both fields, then extract them as properties via 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM