I'm trying to parse an xml file and can't seem to extract the pieces I want. The xml file is for a homegrown system and I don't have any control over its layout.
The file looks like this –
$doc = [xml]@'
<?xml version="1.0" encoding="utf-8"?>
<rbacx >
<namespace namespaceName="Team Name" namespaceShortName="ABC"/>
<attributeValues>
<attributeValue id="Role=Administrator">
<value><![CDATA[Administrator]]></value>
<attributes>
<attribute name="Glossary">
<attributeValues>
<attributeValue><value><![CDATA[Administrator (service accounts)]]></value></attributeValue>
</attributeValues>
</attribute>
</attributes>
</attributeValue>
<attributeValue id="Role=Operator">
<value><![CDATA[Operator]]></value>
<attributes>
<attribute name="Glossary">
<attributeValues>
<attributeValue><value><![CDATA[Operator (all accounts)]]></value></attributeValue>
</attributeValues>
</attribute>
</attributes>
</attributeValue>
</attributeValues>
<accounts>
<account id="FRED">
<name><![CDATA[FRED@xyz.com]]></name>
<endPoint>ABC</endPoint>
<domain>ABC</domain>
<comments/>
<attributes>
<attribute name="AppBoRID">
<attributeValues>
<attributeValue><value><![CDATA[FRED]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Role">
<attributeValues><attributeValueRef id="Role=Operator"/></attributeValues>
</attribute>
</attributes>
</account>
<account id="BARNEY">
<name><![CDATA[BARNEY@xyz.com]]></name>
<endPoint>ABC</endPoint>
<domain>ABC</domain>
<comments/>
<attributes>
<attribute name="AppBoRID">
<attributeValues>
<attributeValue><value><![CDATA[BARNEY]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Role">
<attributeValues><attributeValueRef id="Role=Administrator"/></attributeValues>
</attribute>
</attributes>
</account>
<account id="NonPeopleID_CC1234">
<name><![CDATA[WILMA@xyz.com]]></name>
<endPoint>ABC</endPoint>
<domain>ABC</domain>
<comments/>
<attributes>
<attribute name="appUserName">
<attributeValues>
<attributeValue><value><![CDATA[WILMA]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="CostCentre">
<attributeValues>
<attributeValue><value><![CDATA[1234]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Bank_Number">
<attributeValues>
<attributeValue><value><![CDATA[0000]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Directory">
<attributeValues>
<attributeValue><value><![CDATA[XYZ]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Role">
<attributeValues><attributeValueRef id="Role=Administrator"/></attributeValues>
</attribute>
</attributes>
</account>
<account id="NonPeopleID_CC1234">
<name><![CDATA[BETTY@xyz.com]]></name>
<endPoint>ABC</endPoint>
<domain>ABC</domain>
<comments/>
<attributes>
<attribute name="appUserName">
<attributeValues>
<attributeValue><value><![CDATA[BETTY]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="CostCentre">
<attributeValues>
<attributeValue><value><![CDATA[1234]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Bank_Number">
<attributeValues>
<attributeValue><value><![CDATA[0000]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Directory">
<attributeValues>
<attributeValue><value><![CDATA[XYZ]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Role">
<attributeValues><attributeValueRef id="Role=Operator"/></attributeValues>
</attribute>
</attributes>
</account>
</accounts>
</rbacx>
'@
I'd like to get back output looking like this –
Name Role
FRED Operator
BARNEY Administrator
WILMA Administrator
BETTY Operator
I'm able to get the name@domain with this –
$Doc.rbacx.accounts.account.name
where it returns -
#cdata-section
--------------
FRED@xyz.com
BARNEY@xyz.com
WILMA@xyz.com
BETTY@xyz.com
I can get all the value attributes with this -
$Doc.rbacx.accounts.account.attributes.attribute.attributevalues.attributevalue.value
where it returns -
#cdata-section
--------------
FRED
BARNEY
WILMA
1234
0000
XYZ
BETTY
1234
0000
XYZ
I can't seem to get the role associated with the user returned. I'm thinking it should be along the lines of this –
$Doc.rbacx.accounts.account.attributes.attribute.attributevalues.attributevalue.attributeValueRef
However that doesn't return anything.
Any thoughts on how I can get the User & its associated Role output here?