【发布时间】:2021-01-02 07:16:29
【问题描述】:
我正在尝试解析一个 xml 文件,但似乎无法提取我想要的部分。该 xml 文件是针对一个自制系统的,我无法控制它的布局。
文件看起来像这样——
$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>
'@
我想得到这样的输出 –
Name Role
FRED Operator
BARNEY Administrator
WILMA Administrator
BETTY Operator
我可以通过这个获得 name@domain –
$Doc.rbacx.accounts.account.name
where it returns -
#cdata-section
--------------
FRED@xyz.com
BARNEY@xyz.com
WILMA@xyz.com
BETTY@xyz.com
我可以用这个得到所有的值属性-
$Doc.rbacx.accounts.account.attributes.attribute.attributevalues.attributevalue.value
where it returns -
#cdata-section
--------------
FRED
BARNEY
WILMA
1234
0000
XYZ
BETTY
1234
0000
XYZ
我似乎无法获得与返回的用户关联的角色。我想应该是这样的——
$Doc.rbacx.accounts.account.attributes.attribute.attributevalues.attributevalue.attributeValueRef
然而这并没有返回任何东西。
对如何在此处获取用户及其相关角色输出有何想法?
【问题讨论】:
标签: xml powershell parsing