I am currently trying to feed some XML documents into a script and initialize them as [PSCustomObject]
. The XML document needs to be broken into several objects and then added together.
Here is my script:
[xml]$CourseStructureIn = Get-Content .\Sample.xml
$data = foreach ($Instances in $CourseStructureIn.Node.instances.courseInstance) {
$instancearray = foreach ($instance in $instances) {
$hash = [ordered]@{ CourseInstanceID = $instance.courseInstanceID}
[PSCustomObject]@{
CourseCode = $instance.CourseCode
InstanceCode = $instance.instanceCode
session = $instance.session
quota = $instance.quota
}
}
$hash.Add("Instances", $instancearray)
$modules = $instance.L1Modules.L1Module
$modulearray = foreach ($module in $modules) {
[PSCustomObject]@{
moduleCode = $module.moduleCode
moduleTypeCode = $module.moduleTypeCode
moduleInstanceID = $module.moduleInstanceID
semester = $module.semester
credits = $module.credits
overallGradeWeighting = $module.overallGradeWeighting
fees = $module.fees
documents = $module.documents
}
}
$hash.Add("Modules", $modulearray)
$roles = $instance.L1Modules.L1Module.roles.role
$rolearray = foreach ($role in $roles) {
[PSCustomObject]@{
rolesGUID = $role.GUID
rolesIDNumber = $role.idnumber
roleFirstName = $role.firstname
roleSurname = $role.surname
}
}
$hash.Add("Roles", $rolearray)
This correctly imports the XML structure into 2 distinct instances of an array of objects - I should mention that the XML is originally from a normailzed database and so each XML document represents more or less a table - which ends up being a multidimensional array in PowerShell.
$data.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array
I can refer to the individual array objects
$data[0].roles | ft rolesGUID rolesIDNumber roleFirstName roleSurname --------- ------------- ------------- ----------- 55001420 55001420 R M 55001414 55001414 S C 55001234 55001234 C H 55001342 55001342 O C 55001414 55001414 S C 55001342 55001342 O C 55001445 55001445 M M 55001422 55001422 A H 55001001 55001001 P M 55001079 55001079 V S 55000770 55000770 A M 55000906 55000906 M B
I want to be able to ConverTo-Html
to make a report - but I have no idea how to enumerate this type of structure - what I should be left with is a table with one to many relationships (Is that a multidimensional array or jagged array?) Can someone give me some pointers about how to output these types of structures? Iterating through an array or object is fine when the output is some type of matrix - but what do we use when the structure is multiple rows for some columns and single rows for others.
For example my output with Format-Table
:
$data | ft CourseInstanceID Instances Modules ---------------- --------- ------- PGDDA_353650876 @{CourseCode=PGDDA; InstanceCode=PGDSP; session=2014; quota=999; Instances=; CourseInstanceID=PGDDA_353650876; Modules=System.Object[]; Roles=System.Object[]} {@{moduleCode=H... PGDDA_418403503 @{CourseCode=PGDDA; InstanceCode=PGDSP; session=2015; quota=999; Instances=; CourseInstanceID=PGDDA_418403503; Modules=System.Object[]; Roles=System.Object[]} {@{moduleCode=H...
I have tried expanding the properties and have been reading all over the web, so any pointers would be greatly appreciated.
Here are the members:
$data | gm TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() CourseInstanceID NoteProperty string CourseInstanceID=PGDDA_353650876 Instances NoteProperty Selected.System.Management.Automation.PSCustomObject Instances=@{CourseCode=PGDDA; InstanceCode=PGDSP; session=2014; quota=999; Instances=; CourseInstanceID=PGDD... Modules NoteProperty Object[] Modules=System.Object[] Roles NoteProperty Object[] Roles=System.Object[]
Thank you - I cannot share the XML document here but I will digress from my specific example to a general one.
Suppose we have many of the following XML documents
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<author>Tiny Tim</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>$44.95</price>
<price>€40.50</price>
<price>£35.99</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
<TableOfContents>
<chapter Title = "Introduction" Number = "1" Page = "1"></chapter>
<chapter Title = "XSD" Number = "2" Page = "14"></chapter>
<chapter Title = "XPATH" Number = "3" Page = "27"></chapter>
<chapter Title = "XQUERY" Number = "4" Page = "42"></chapter>
<chapter Title = "XHTML" Number = "5" Page = "58"></chapter>
<chapter Title = "XSLT" Number = "6" Page = "75"></chapter>
</TableOfContents>
</book>
</catalog>
I would like to convert this into a table such as this format (image sorry)
I should also mention that there could be many book nodes in an document so I would like a table for each book.