powershell - Powershell在一个Select-Object中获取不同级别的节点值
问题描述
有没有办法做一个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看是否没有,如果是,则在文件中输出为尚未正确设置的Field。但我喜欢那个 xml 中的数百个字段
解决方案
您可以与查询Select-Xml
结合使用并获取所需的元素和属性值。我相信,您可以指定不同的 s 来访问不同的节点级别,包括元素属性。XPath
Select-Object
XPath
请参阅Microsoft 文档中的这些示例:
$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
如果你想同时访问不同的元素和属性,你可以这样做:
$path = "test.xml"
$xpathField = "/field"
$xpathGlobalFormat = "/field/globalFormat"
$field = Select-Xml -Path $path -XPath $xpathField | Select-Object -ExpandProperty Node
$globalFormat = Select-Xml -Path $path -XPath $xpathGlobalFormat | Select-Object -ExpandProperty Node
$field.tableName # this returns *SERVER
$globalFormat.categoryType # this returns Number
推荐阅读
- android-emulator - Android模拟器的“插入虚拟耳机插头”是什么意思?
- ios - 如何在 Xcode 10.1 中添加 Alamofire
- javascript - 现在更改了 .htaccess 和永久链接,除了主页 404s
- vue.js - 为什么在 vuejs 中使用“数据”会有所不同?
- django - 尝试在已部署的 Heroku 应用程序上访问 Django /admin 时,在生产页面未找到错误
- python - 将 Scipy 模块 0.18 版本更新为 1.2.1 版本
- c# - C# 非通用 ISet 接口
- c# - 漂亮地递归打印集合
- opencv - opencv 是否在高斯模糊上使用了一些加速技术(即 opencl)?
- c++ - 对 enum 和 typedef 结构进行正确的位打包