问题描述
有没有办法做一个Select-Object
,得到不同层次节点的值?
我需要字段级别的名称和一堆字段,还需要 globalFormat categoryType。
有没有办法将它们放在一个 Select-Object
中?
XML
<field name="FLD0000001" tableAlias="*SERVER" tableName="*SERVER">
<order>0</order>
<version></version>
<afterEditEvent></afterEditEvent>
<format></format>
<globalFormat categoryType="Number" countrySymbolCode="" dateFormat="" timeFormat="" imageFormat="None" decimalPlaces="2" leadingZero="0" isCentury="false" negativeFormat="NegativeSymbol" />
<columnGroupheading></columnGroupheading>
<datafieldType>20</datafieldType>
<columnheading><![CDATA[FLD0000001]]></columnheading>
<hideHeader>False</hideHeader>
<groupable>0</groupable>
<subTotal>0</subTotal>
<calculation></calculation>
<jScriptCalculation />
<editCalculation scriptContent="" scriptdisplayContent="">
<tokens />
<triggers />
</editCalculation>
<oncalculationEvent></oncalculationEvent>
<onValidationEvent></onValidationEvent>
<editableAdvanced></editableAdvanced>
<addrequired>0</addrequired>
<splitByRationOn></splitByRationOn>
<defaultValue></defaultValue>
<isCalculatable>0</isCalculatable>
<isUpdatable>0</isUpdatable>
<visibleAdvanced></visibleAdvanced>
<editableLevel>0</editableLevel>
<visibleLevel>10</visibleLevel>
<fullTypeName></fullTypeName>
<dataType>NUMERIC</dataType>
<dataLength>5</dataLength>
<description source="" format="4"></description>
<dimensionTitle></dimensionTitle>
<deFinition></deFinition>
<parameterName></parameterName>
<cubedimensionorder>0</cubedimensionorder>
<subTotalRestrictions></subTotalRestrictions>
<subTotalExceptions></subTotalExceptions>
<matrixHeaderValue></matrixHeaderValue>
<promptRestricted>0</promptRestricted>
<prompt processId="" />
<promptOption></promptOption>
<sortOrderForPrompt>NONE</sortOrderForPrompt>
<filterForPrompt></filterForPrompt>
<relatedFields></relatedFields>
<validateAgainstPrompt>False</validateAgainstPrompt>
<subGroupId></subGroupId>
<indexInGroup>0</indexInGroup>
<widthInFieldArea>2000</widthInFieldArea>
</field>
我基本上是想检查datafieldType,如果是20或21,检查globalFormat categoryType看是否为none,如果是,则在文件中作为未正确设置的Field输出。但我在那个 xml 中有数百个字段
解决方法
您可以将 (https:\/\/www.|http:\/\/www.)
与 Select-Xml
查询和 XPath
结合使用来获取您需要的元素和属性值。我相信,您可以指定不同的 Select-Object
以到达不同的节点级别,包括元素属性。
查看来自 Microsoft docs 的这些示例:
XPath
例如,如果您想获取 $Path = "$Pshome\Types.ps1xml"
$XPath = "/Types/Type/Members/AliasProperty"
Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node
Name ReferencedMemberName
---- --------------------
Count Length
Name Key
Name ServiceName
RequiredServices ServicesDependedOn
ProcessName Name
Handles Handlecount
VM VirtualSize
WS WorkingSetSize
Name ProcessName
Handles Handlecount
VM VirtualMemorySize
WS WorkingSet
PM PagedMemorySize
NPM NonpagedSystemMemorySize
Name __Class
Namespace ModuleName
元素的属性,您可以这样做:
globalFormat
如果你想同时访问不同的元素和属性,你可以这样做:
$path = "\test.xml"
$xpath = "/field/globalFormat"
Select-Xml -Path $path -XPath $xpath | Select-Object -ExpandProperty Node
categoryType : Number
countrySymbolCode :
dateFormat :
timeFormat :
imageFormat : None
decimalPlaces : 2
leadingZero : 0
isCentury : false
negativeFormat : NegativeSymbol
,
为了建立在 Alex's helpful answer 之上,我认为您正在寻找一种 xPath 查询,您可以利用它一次性获取信息。我在想:
(Select-Xml -Xml $Xml -XPath "/field/@* | /field/globalFormat/@categoryType").Node
我不确定您要查找的输出是什么,但另一种选择是简单地选择字段节点并使用“。”引用以获取子属性以构建自定义对象。
要获取 /field
AND /field/globalFormat
下的所有元素和属性:
(Select-Xml -xml $xml -XPath '/field | /field/globalFormat').Node