mhopkins321
mhopkins321

Reputation: 3083

Find node in xml, despite how many nodes deep it might be

This is a segment of an extremely large xml. I'm super confused as to how to get the results that I need from it.

<Task ID="269930118" Name="Name1" Active="0" NextEID="65" TT="COS" AR="0">
    <For ID="12">
        <If ID="13">
            <When>
                <Criteria>
                    <comp a="[OrigName]" test="MASK" b="53_Inbound_Daily_Account.txt"/>
                </Criteria>
                <UpdOrig Action="d" ID="45"/>
            </When>
        </If>
    </For>
    <If ID="37">
        <When>
            <Criteria>
                <comp a="[TaskStatus]" test="MASK" b="Success"/>
            </Criteria>
            <Email HostID="282198113" Subject="[TaskName] [TaskStatus]" Message="[TaskName] [TaskStatus]" AddressTo="[email protected]" UseDefRetryCount="1" UseDefRetryTimeoutSecs="1" ID="41" Type="SMTP"/>
            <RunTask TaskID="326261818" Wait="0" ID="51"/>
        </When>
    </If>
    <If ID="39">
        <When>
            <Criteria>
                <comp a="[TaskStatus]" test="MASK" b="Failure"/>
            </Criteria>
            <Email HostID="282198113" Subject="[TaskName] [TaskStatus]" Message="[TaskName] [TaskStatus]" AddressTo="[email protected]" UseDefRetryCount="1" UseDefRetryTimeoutSecs="1" ID="40" Type="SMTP"/>
        </When>
    </If>
</Task>

I need to add the task id to an arary if the following are true

comp.a -eq "[TaskStatus]"
comp.b -eq "Failure"
The email node exists right after the comp node. #pseudo code obviously

So the comp and email need to be within the same when node, but the when node may be any number of nodes deep into the xml in a given task. I'm super confused how to find these. Thoughts?

Upvotes: 0

Views: 80

Answers (1)

Frode F.
Frode F.

Reputation: 54911

Try this:

$ids = @()

$xml = [xml](Get-Content .\Desktop\test.xml)

$xml.Task | ForEach-Object {

    if (@($_.SelectNodes('./*/When[Criteria/comp[@a = "[TaskStatus]" and @b = "Failure"] and (Email)]')).Count -gt 0) {
        #Criterias met. Add Task ID
        $ids += $_.ID
    }
}

Upvotes: 2

Related Questions