Parent XML - Powershell

Hey

<root>
	<Unit id="1">
		<name>Firm</name>

		<parentUnit></parentUnit>

	</Unit>

	<Unit id="2">
		<name>Boston</name>

		<parentUnit>1</parentUnit>

	</Unit>
		

	<Unit id="3">
		<name>HR</name>

		<parentUnit>2</parentUnit>

	</Unit>
		
	<Unit id="4">
		<name>NY</name>

		<parentUnit>1</parentUnit>

	</Unit>

	<Unit id="5">
		<name>Internal</name>

		<parentUnit>3</parentUnit>

	</Unit>
</root>

Open in new window


Any easy way to return? (parent / child) - Powershell (XML)

Firm
Firm, Boston
Firm, Boston, HR
Firm, NY
Firm, Boston, HR, Internal
LVL 1
mikeydkAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
My suggestion is looking slightly different, and directly generates an array.
$parentPath = @{}
$xml.root.Unit | sort ParentUnit, ID |
  % { $parentpath[$_.ID] = @(if ($_.ParentUnit -gt 0) {$parentPath[$_.ParentUnit]} else {@()}) + $_.name }

$parentpath.GetEnumerator() | sort name

Open in new window

It also grants to process in the proper sequence, so the XML items do not need to be ordered correctly. Depending on how the XML is generated, that might be important or irrelevant.
0
 
aikimarkCommented:
$xml=[xml]'<root><Unit id="1"><name>Firm</name><parentUnit></parentUnit></Unit><Unit id="2"><name>Boston</name><parentUnit>1</parentUnit></Unit><Unit id="3"><name>HR</name><parentUnit>2</parentUnit></Unit><Unit id="4"><name>NY</name><parentUnit>1</parentUnit></Unit><Unit id="5"><name>Internal</name><parentUnit>3</parentUnit></Unit></root>'

$xml.SelectSingleNode('root').childnodes | % -begin{$parentpath = @{}} -Process{$x = ($parentpath[$_.Parentunit] + ', ' + $_.name) -replace '^, ', '';$parentpath.Add($_.id, $x)}

$parentpath.GetEnumerator() | sort -Property Name

Open in new window

0
 
mikeydkAuthor Commented:
Thanks ;)

Is it possible to return the values as an array? (and not a string?)

Thanks in advance

Mike
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
aikimarkCommented:
$xml=[xml]'<root><Unit id="1"><name>Firm</name><parentUnit></parentUnit></Unit><Unit id="2"><name>Boston</name><parentUnit>1</parentUnit></Unit><Unit id="3"><name>HR</name><parentUnit>2</parentUnit></Unit><Unit id="4"><name>NY</name><parentUnit>1</parentUnit></Unit><Unit id="5"><name>Internal</name><parentUnit>3</parentUnit></Unit></root>'

$xml.SelectSingleNode('root').childnodes | % -begin{$parentpath = @{}} -Process{$x = ($parentpath[$_.Parentunit] + ',' + $_.name) -replace '^,', '';$parentpath.Add($_.id, $x)}

$parentpath.GetEnumerator() | sort -Property Name | %{$a = ($_.value -split ","); $a}

Open in new window

If you want to prove that $a is an array/list, change the last line to this to see a pipe-delimited display of the elements
$parentpath.GetEnumerator() | sort -Property Name | %{$a = ($_.value -split ","); $a -join "|"}

Open in new window

0
 
mikeydkAuthor Commented:
aikimark> Seems to be working - just don't like the split-function
Qlemo> for some strange reason - some of the results return $null?
0
 
aikimarkCommented:
just don't like the split-function
Why?
Does it matter?
0
 
mikeydkAuthor Commented:
aikimark > Some of the names contains comma...
0
 
aikimarkCommented:
Please post a REPRESENTATIVE sample of the XML data.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Do you get $null with your example XML posted above? Can you provide another example, so I can test with more data?
0
 
aikimarkCommented:
two solutions to the problem
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.