0

Related to PowerShell 5.1

I was playing around with XML to show how to handle conflicting namespaces. Here's the example I created:

<Employees> 
   <ms:Employee id='1' xmlns:ms="MicrosoftEmployees"> 
       <FirstName>Bill</FirstName>
       <LastName>Gates</LastName>
   </ms:Employee>
   <ms:Employee id='2' xmlns:ms="MicrosoftEmployees"> 
       <FirstName>Paul</FirstName>
       <LastName>Allen</LastName>
   </ms:Employee>
   <ap:Employee id='1' xmlns:ap="AppleEmployees"> 
       <Name>Steve Jobs</Name>
   </ap:Employee>
   <ap:Employee id='2' xmlns:ap="AppleEmployees"> 
       <Name>Steve Wozniak </Name>
   </ap:Employee>
</Employees>

The scenario might be combining data from two different companies.

PowerShell demonstration program:

cls 
$filename = "c:\XMLClass\IntroSamples\Sample05_Simpler_Namespace.xml"
[xml]$xmlDoc = Get-Content $filename 

$xmlDoc.Employees.Employee[0]
$xmlDoc.Employees.Employee[1]
$xmlDoc.Employees.Employee[2]
$xmlDoc.Employees.Employee[3]

Output:

id ms                 FirstName LastName
-- --                 --------- --------
1  MicrosoftEmployees Bill      Gates   
2  MicrosoftEmployees Paul      Allen   
1                                       
2                                       

Is there anyway to get a more logical output?

It seems like PowerShell locks into the first schema it sees for the Employee element, then cannot show the Name element of the Apple employees. This actually makes sense, but I was just checking to see if there is something fancier to handle this that I might be missing.

I know I could use SelectSingleNodes and XPath, but was just trying to see if and how PowerShell could handle this "out of the box".

If I reverse the code:

$xmlDoc.Employees.Employee[2]
$xmlDoc.Employees.Employee[3]
$xmlDoc.Employees.Employee[1]
$xmlDoc.Employees.Employee[0]

Then the output is:

id ap             Name          
-- --             ----          
1  AppleEmployees Steve Jobs    
2  AppleEmployees Steve Wozniak 
1                 ms:Employee   
2                 ms:Employee 

1 Answer 1

1

Use format list to see all the properties. Format-table doesn't handle different sets of properties well.

$xmldoc.employees.employee | format-list

id        : 1
ms        : MicrosoftEmployees
FirstName : Bill
LastName  : Gates

id        : 2
ms        : MicrosoftEmployees
FirstName : Paul
LastName  : Allen

id   : 1
ap   : AppleEmployees
Name : Steve Jobs

id   : 2
ap   : AppleEmployees
Name : Steve Wozniak 

Sign up to request clarification or add additional context in comments.

2 Comments

Cool, but any way to make them go across the page (horizontal instead of vertical)?
Interesting also, that even when I tried to print them independently, on 4 different write statements, the format-table somehow compares them and lumps them together with one common heading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.