Search code examples
powershellselect-object

Powershell getting node values of different levels in one Select-Object


Is there a way to do a Select-Object and get the values of nodes of different levels?

I need the name and a bunch of fields from the field level but also the globalFormat categoryType. Is there a way to have them in one 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>

I basically want to check dataFieldType, if it's 20 or 21, check globalFormat categoryType to see if it's none, if it is, output in a file as a Field that hasn't been set properly. but I have like hundreds of fields in that xml


Solution

  • To build on Alex's helpful answer I think you are looking for an XPath query you can leverage to get the information in one shot. I was thinking something like:

    (Select-Xml -Xml $Xml -XPath "/field/@* | /field/globalFormat/@categoryType").Node
    

    I'm not exactly sure what output you are looking for, but another option would be to simply select the field nodes and just use "." referencing to get at the sub-properties to construct custom objects.

    To get all of the elements and attributes under /field AND /field/globalFormat:

    (Select-Xml -Xml $Xml -XPath '/field | /field/globalFormat').Node